simulavr  1.1.0
atmega16_32.cpp
Go to the documentation of this file.
1  /*
2  ****************************************************************************
3  *
4  * simulavr - A simulator for the Atmel AVR family of microcontrollers.
5  * Copyright (C) 2001, 2002, 2003 Klaus Rudolph
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  ****************************************************************************
22  *
23  * $Id$
24  */
25 
26 #include "atmega16_32.h"
27 
28 #include "hardware.h"
29 #include "irqsystem.h"
30 #include "hwport.h"
31 #include "hwstack.h"
32 #include "hwspi.h"
33 #include "hweeprom.h"
34 #include "hwwado.h"
35 #include "hwsreg.h"
36 #include "flashprog.h"
37 
38 #include "avrfactory.h"
39 
42 
44  delete acomp;
45  delete timer2;
46  delete timer1;
47  delete inputCapture1;
48  delete timer0;
49  delete timer012irq;
50  delete usart;
51  delete wado;
52  delete extirq;
53  delete mcucsr_reg;
54  delete mcucr_reg;
55  delete gifr_reg;
56  delete gicr_reg;
57  delete spi;
58  delete ad;
59  delete aref;
60  delete admux;
61  delete spmRegister;
62  delete osccal_reg;
63  delete stack;
64  delete eeprom;
65  delete irqSystem;
66 }
67 
69  unsigned flash_bytes,
70  unsigned ee_bytes,
71  unsigned nrww_start,
72  bool atmega16):
73  AvrDevice(64 , // I/O space above General Purpose Registers
74  ram_bytes, // RAM size
75  0, // External RAM size
76  flash_bytes), // Flash Size
77  porta(this, "A"),
78  portb(this, "B"),
79  portc(this, "C"),
80  portd(this, "D"),
81  assr_reg(&coreTraceGroup, "ASSR"),
82  sfior_reg(&coreTraceGroup, "SFIOR"),
83  prescaler01(this, "01", &sfior_reg, 0),
84  prescaler2(this, "2", PinAtPort(&portc, 6), &assr_reg, 3, &sfior_reg, 1),
85  premux0(&prescaler01, PinAtPort(&portb, 0)),
86  premux1(&prescaler01, PinAtPort(&portb, 1)),
87  premux2(&prescaler2)
88 {
89  fuses->SetFuseConfiguration(16, 0x99e1);
90  fuses->SetBootloaderConfig(nrww_start, (flash_bytes >> 1) - nrww_start, 9, 8);
91  irqSystem = new HWIrqSystem(this, 4, 21); //4 bytes per vector, 21 vectors
92  eeprom = new HWEeprom(this, irqSystem, ee_bytes, atmega16 ? 15 : 17);
93  stack = new HWStackSram(this, atmega16 ? 11 : 12);
96 
97  // TWI/I2C not implemented yet, vectors 17/19
98  // Analog Comparator not implemented yet, vectors 16/18
99  // Store Program Memory Ready not implemented yet, vectors 21/21
100 
101  admux = new HWAdmuxM16(this, &porta.GetPin(0), &porta.GetPin(1), &porta.GetPin(2),
102  &porta.GetPin(3), &porta.GetPin(4), &porta.GetPin(5),
103  &porta.GetPin(6), &porta.GetPin(7));
104  aref = new HWARef4(this, HWARef4::REFTYPE_NOBG);
105  ad = new HWAd_SFIOR(this, HWAd::AD_M16, irqSystem, atmega16 ? 14 : 16, admux, aref, &sfior_reg);
106 
107  spi = new HWSpi(this, irqSystem,
108  PinAtPort(&portb, 5), PinAtPort(&portb, 6), PinAtPort(&portb, 7),
109  PinAtPort(&portb, 4),/*irqvec*/ atmega16 ? 10 : 12, true);
110 
111  gicr_reg = new IOSpecialReg(&coreTraceGroup, "GICR");
112  gifr_reg = new IOSpecialReg(&coreTraceGroup, "GIFR");
113  mcucr_reg = new IOSpecialReg(&coreTraceGroup, "MCUCR");
114  mcucsr_reg = new IOSpecialReg(&coreTraceGroup, "MCUCSR");
116  extirq->registerIrq(1, 6, new ExternalIRQSingle(mcucr_reg, 0, 2, GetPin("D2"))); // INT0
117  extirq->registerIrq(2, 7, new ExternalIRQSingle(mcucr_reg, 2, 2, GetPin("D3"))); // INT1
118  extirq->registerIrq(atmega16 ? 18 : 3, 5, new ExternalIRQSingle(mcucsr_reg, 6, 1, GetPin("B2"))); // INT2
119 
120  wado = new HWWado(this);
121 
122  usart = new HWUsart(this, irqSystem,
123  PinAtPort(&portd,1), PinAtPort(&portd,0), PinAtPort(&portb, 0),
124  atmega16 ? 11 : 13, atmega16 ? 12 : 14, atmega16 ? 13 : 15);
125 
127  timer012irq->registerLine(0, IRQLine("TOV0", atmega16 ? 9 : 11)); // Timer/Counter0 Overflow
128  timer012irq->registerLine(1, IRQLine("OCF0", atmega16 ? 19 : 10)); // Timer/Counter0 Compare Match
129  timer012irq->registerLine(2, IRQLine("TOV1", atmega16 ? 8 : 9)); // Timer/Counter1 Overflow
130  timer012irq->registerLine(3, IRQLine("OCF1B", atmega16 ? 7 : 8)); // Timer/Counter1 Compare Match B
131  timer012irq->registerLine(4, IRQLine("OCF1A", atmega16 ? 6 : 7)); // Timer/Counter1 Compare Match A
132  timer012irq->registerLine(5, IRQLine("ICF1", atmega16 ? 5 : 6)); // Timer/Counter1 Capture Even
133  timer012irq->registerLine(6, IRQLine("TOV2", atmega16 ? 4 : 5)); // Timer/Counter2 Overflow
134  timer012irq->registerLine(7, IRQLine("OCF2", atmega16 ? 3 : 4)); // Timer/Counter2 Compare Match
135 
136  timer0 = new HWTimer8_1C(this,
137  &premux0,
138  0,
139  timer012irq->getLine("TOV0"),
140  timer012irq->getLine("OCF0"),
141  PinAtPort(&portb, 3));
143  timer1 = new HWTimer16_2C2(this,
144  &premux1,
145  1,
146  timer012irq->getLine("TOV1"),
147  timer012irq->getLine("OCF1A"),
148  PinAtPort(&portd, 5),
149  timer012irq->getLine("OCF1B"),
150  PinAtPort(&portd, 4),
151  timer012irq->getLine("ICF1"),
153  false);
154  timer2 = new HWTimer8_1C(this,
155  &premux2,
156  2,
157  timer012irq->getLine("TOV2"),
158  timer012irq->getLine("OCF2"),
159  PinAtPort(&portd, 7));
160 
161  acomp = new HWAcomp(this, irqSystem, PinAtPort(&portb, 2), PinAtPort(&portb, 3), atmega16 ? 16 : 18, ad, timer1, &sfior_reg);
162 
163  rw[0x5f]= statusRegister;
164  rw[0x5e]= & ((HWStackSram *)stack)->sph_reg;
165  rw[0x5d]= & ((HWStackSram *)stack)->spl_reg;
166  rw[0x5c]= & timer0->ocra_reg;
167  rw[0x5b]= gicr_reg;
168  rw[0x5a]= gifr_reg;
169  rw[0x59]= & timer012irq->timsk_reg;
170  rw[0x58]= & timer012irq->tifr_reg;
171  rw[0x57]= & spmRegister->spmcr_reg;
172  //rw[0x56] TWCR
173  rw[0x55] = mcucr_reg;
174  rw[0x54] = mcucsr_reg;
175  rw[0x53]= & timer0->tccr_reg;
176  rw[0x52]= & timer0->tcnt_reg;
177  rw[0x51]= osccal_reg; // Attention! OCDR register isn't simulated!
178  rw[0x50]= & sfior_reg;
179  rw[0x4f]= & timer1->tccra_reg;
180  rw[0x4e]= & timer1->tccrb_reg;
181  rw[0x4d]= & timer1->tcnt_h_reg;
182  rw[0x4c]= & timer1->tcnt_l_reg;
183  rw[0x4b]= & timer1->ocra_h_reg;
184  rw[0x4a]= & timer1->ocra_l_reg;
185  rw[0x49]= & timer1->ocrb_h_reg;
186  rw[0x48]= & timer1->ocrb_l_reg;
187  rw[0x47]= & timer1->icr_h_reg;
188  rw[0x46]= & timer1->icr_l_reg;
189  rw[0x45]= & timer2->tccr_reg;
190  rw[0x44]= & timer2->tcnt_reg;
191  rw[0x43]= & timer2->ocra_reg;
192  rw[0x42]= & assr_reg;
193  rw[0x41]= & wado->wdtcr_reg;
194  rw[0x40]= & usart->ucsrc_ubrrh_reg;
195  rw[0x3f]= & eeprom->eearh_reg;
196  rw[0x3e]= & eeprom->eearl_reg;
197  rw[0x3d]= & eeprom->eedr_reg;
198  rw[0x3c]= & eeprom->eecr_reg;
199  rw[0x3b]= & porta.port_reg;
200  rw[0x3a]= & porta.ddr_reg;
201  rw[0x39]= & porta.pin_reg;
202  rw[0x38]= & portb.port_reg;
203  rw[0x37]= & portb.ddr_reg;
204  rw[0x36]= & portb.pin_reg;
205  rw[0x35]= & portc.port_reg;
206  rw[0x34]= & portc.ddr_reg;
207  rw[0x33]= & portc.pin_reg;
208  rw[0x32]= & portd.port_reg;
209  rw[0x31]= & portd.ddr_reg;
210  rw[0x30]= & portd.pin_reg;
211  rw[0x2f]= & spi->spdr_reg;
212  rw[0x2e]= & spi->spsr_reg;
213  rw[0x2d]= & spi->spcr_reg;
214  rw[0x2c]= & usart->udr_reg;
215  rw[0x2b]= & usart->ucsra_reg;
216  rw[0x2a]= & usart->ucsrb_reg;
217  rw[0x29]= & usart->ubrr_reg;
218  rw[0x28]= & acomp->acsr_reg;
219  rw[0x27]= & ad->admux_reg;
220  rw[0x26]= & ad->adcsra_reg;
221  rw[0x25]= & ad->adch_reg;
222  rw[0x24]= & ad->adcl_reg;
223  //rw[0x23] TWDR
224  //rw[0x22] TWAR
225  //rw[0x21] TWSR
226  //rw[0x20] TWBR
227 
228  Reset();
229 }
230 
231 /* EOF */
oscillator version 3.x and older, 8bit, one range
Definition: rwmem.h:179
ADC reference is selected on 3 or 4 different sources: Vcc, aref pin, bandgap or 2.56V reference.
Definition: hwad.h:60
Basic AVR device, contains the core functionality.
Definition: avrdevice.h:66
0:aref, 1:vcc, 2:-, 3:2.56V
Definition: hwad.h:67
AVR device class for ATMega16, see AvrDevice_atmega16_32.
Definition: atmega16_32.h:86
AvrFuses * fuses
Definition: avrdevice.h:100
AVR device class for ATMega32, see AvrDevice_atmega16_32.
Definition: atmega16_32.h:93
HWAd * ad
adc unit
Definition: atmega16_32.h:59
PrescalerMultiplexer premux2
prescaler multiplexer for timer 2
Definition: atmega16_32.h:68
TimerIRQRegister * timer012irq
timer interrupt unit for timer 0 to 2
Definition: atmega16_32.h:73
IOSpecialReg assr_reg
ASSR IO register.
Definition: atmega16_32.h:62
HWARef * aref
adc reference unit
Definition: atmega16_32.h:58
HWIrqSystem * irqSystem
Definition: avrdevice.h:104
IOReg< HWAd > admux_reg
Definition: hwad.h:269
Implements a stack with stack register using RAM as stackarea.
Definition: hwstack.h:131
IOSpecialReg sfior_reg
SFIOR IO register.
Definition: atmega16_32.h:63
IOReg< HWEeprom > eedr_reg
Definition: hweeprom.h:101
IOReg< HWTimer8_1C > tccr_reg
control register
Definition: hwtimer.h:388
void registerIrq(int vector, int irqBit, ExternalIRQ *extirq)
Definition: externalirq.cpp:53
IOReg< HWTimer16 > ocra_h_reg
output compare A register, high byte
Definition: hwtimer.h:312
HWAdmux * admux
adc multiplexer unit
Definition: atmega16_32.h:57
Pin & GetPin(unsigned char pinNo)
returns a pin reference of pin with pin number
Definition: hwport.cpp:87
Timer unit with 8Bit counter and one output compare unit.
Definition: hwtimer.h:377
IOReg< HWTimer16 > ocra_l_reg
output compare A register, low byte
Definition: hwtimer.h:313
IOReg< HWTimer8 > tcnt_reg
counter register
Definition: hwtimer.h:229
Definition: hwwado.h:38
IOReg< HWTimer16 > tcnt_h_reg
counter register, high byte
Definition: hwtimer.h:310
OSCCALRegister * osccal_reg
OSCCAL IO register.
Definition: atmega16_32.h:55
Handler for external IRQ&#39;s to communicate with IRQ system and mask/flag registers.
Definition: externalirq.h:41
Represents a timer interrupt line, Frontend for timer interrupts.
Definition: timerirq.h:42
AvrDevice_atmega16_32(unsigned ram_bytes, unsigned flash_bytes, unsigned ee_bytes, unsigned nrww_start, bool atmega16)
Definition: atmega16_32.cpp:68
void SetFuseConfiguration(int size, unsigned long defvalue)
Configure fuses.
Definition: flashprog.cpp:246
HWUsart * usart
usart unit
Definition: atmega16_32.h:75
IOReg< HWUart > ubrr_reg
IO register "UBRRxL" - baudrate.
Definition: hwuart.h:136
ADC type M16: ADC on atmega16 and atmega32.
Definition: hwad.h:260
AVRDevice class for ATMega16 and ATMega32.
Definition: atmega16_32.h:43
HWTimer8_1C * timer2
timer 2 unit
Definition: atmega16_32.h:72
IOSpecialReg * gifr_reg
GIFR IO register.
Definition: atmega16_32.h:52
IOSpecialReg tifr_reg
the TIFRx register
Definition: timerirq.h:74
IOReg< HWSpi > spdr_reg
Definition: hwspi.h:121
Implements the I/O hardware necessary to do USART transfers.
Definition: hwuart.h:149
IOReg< HWPort > port_reg
Definition: hwport.h:84
IOReg< HWUart > ucsra_reg
Definition: hwuart.h:136
void Reset()
Definition: avrdevice.cpp:390
IOReg< HWPort > pin_reg
Definition: hwport.h:84
IOSpecialReg * mcucr_reg
MCUCR IO register.
Definition: atmega16_32.h:53
IOReg< HWTimer16 > icr_h_reg
input capture register, high byte
Definition: hwtimer.h:318
HWSpi * spi
spi unit
Definition: atmega16_32.h:74
IOReg< HWSpi > spcr_reg
Definition: hwspi.h:121
IOSpecialReg * gicr_reg
GICR IO register.
Definition: atmega16_32.h:51
HWPort portc
port C
Definition: atmega16_32.h:48
HWTimer16_2C2 * timer1
timer 1 unit
Definition: atmega16_32.h:71
TraceValueCoreRegister coreTraceGroup
Definition: avrdevice.h:108
IOReg< HWUart > ucsrb_reg
Definition: hwuart.h:136
IOReg< HWTimer16 > icr_l_reg
input capture register, low byte
Definition: hwtimer.h:319
Timer unit with 16Bit counter and 2 output compare units and 2 config registers.
Definition: hwtimer.h:528
IOReg< HWAd > adcsra_reg
Definition: hwad.h:269
HWPort porta
port A
Definition: atmega16_32.h:46
IOReg< HWEeprom > eearh_reg
Definition: hweeprom.h:101
IOReg< HWTimer16 > tcnt_l_reg
counter register, low byte
Definition: hwtimer.h:311
Pin * GetPin(const char *name)
Definition: avrdevice.cpp:76
IOReg< HWTimer16 > ocrb_l_reg
output compare B register, low byte
Definition: hwtimer.h:315
Provices flag and mask register for timer interrupts and connects irq lines to irqsystem.
Definition: timerirq.h:61
IOReg< HWEeprom > eecr_reg
Definition: hweeprom.h:101
HWEeprom * eeprom
Definition: avrdevice.h:102
IOReg< HWUsart > ucsrc_ubrrh_reg
Definition: hwuart.h:173
Definition: hwspi.h:38
void registerLine(int idx, IRQLine *irq)
Definition: timerirq.cpp:88
#define AVR_REGISTER(name, class)
Definition: avrfactory.h:69
RWMemoryMember ** rw
The whole memory: R0-R31, IO, Internal RAM.
Definition: avrdevice.h:129
HWAcomp * acomp
analog compare unit
Definition: atmega16_32.h:60
HWTimer8_1C * timer0
timer 0 unit
Definition: atmega16_32.h:70
PrescalerMultiplexerExt premux0
prescaler multiplexer for timer 0
Definition: atmega16_32.h:66
Provides the programming engine for flash self programming.
Definition: flashprog.h:38
void SetBootloaderConfig(unsigned addr, int size, int bPosBOOTSZ, int bPosBOOTRST)
Set bootloader support configuration.
Definition: flashprog.cpp:274
IOReg< HWAd > adch_reg
Definition: hwad.h:269
IOReg< HWTimer16 > ocrb_h_reg
output compare B register, high byte
Definition: hwtimer.h:314
IOReg< HWSpi > spsr_reg
Definition: hwspi.h:121
IOReg< HWEeprom > eearl_reg
Definition: hweeprom.h:101
IOReg< HWAd > adcl_reg
Definition: hwad.h:269
IOReg< HWTimer8 > ocra_reg
output compare A register
Definition: hwtimer.h:230
HWStack * stack
Definition: avrdevice.h:131
HWPort portd
port D
Definition: atmega16_32.h:49
IOSpecialReg * mcucsr_reg
MCUCSR IO register.
Definition: atmega16_32.h:54
HWPort portb
port B
Definition: atmega16_32.h:47
Class, which provides input capture source for 16bit timers.
Definition: icapturesrc.h:34
RWSreg * statusRegister
the memory interface for status
Definition: avrdevice.h:133
IOReg< HWUart > udr_reg
Definition: hwuart.h:136
IRQLine * getLine(const std::string &name)
Definition: timerirq.cpp:109
PrescalerMultiplexerExt premux1
prescaler multiplexer for timer 1
Definition: atmega16_32.h:67
HWWado * wado
WDT timer.
Definition: avrdevice.h:134
IOReg< HWTimer16_2C2 > tccrb_reg
control register B
Definition: hwtimer.h:553
ExternalIRQHandler * extirq
external interrupt support
Definition: atmega16_32.h:50
FlashProgramming * spmRegister
Definition: avrdevice.h:99
IOReg< HWPort > ddr_reg
Definition: hwport.h:84
IOReg< HWWado > wdtcr_reg
Definition: hwwado.h:54
IOSpecialReg timsk_reg
the TIMSKx register
Definition: timerirq.h:73
Analog comparator peripheral.
Definition: hwacomp.h:42
IOReg< FlashProgramming > spmcr_reg
Definition: flashprog.h:95
Implement OSCCAL register.
Definition: rwmem.h:174
External interrupt (INT0, INT1...) on a single pin, one and 2 bit configuration.
Definition: externalirq.h:110
ICaptureSource * inputCapture1
input capture source for timer1
Definition: atmega16_32.h:69
IOReg< HWTimer16_2C2 > tccra_reg
control register A
Definition: hwtimer.h:552