GDB working, some more source massaging
[simavr] / simavr / cores / sim_mega644.c
1 /*
2         sim_mega644.c
3
4         Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
5
6         This file is part of simavr.
7
8         simavr is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 3 of the License, or
11         (at your option) any later version.
12
13         simavr is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include </usr/include/stdio.h>
23 #include "sim_avr.h"
24 #include "sim_core_declare.h"
25 #include "avr_eeprom.h"
26 #include "avr_ioport.h"
27 #include "avr_uart.h"
28 #include "avr_timer8.h"
29
30 #define _AVR_IO_H_
31 #define __ASSEMBLER__
32 #include "avr/iom644.h"
33
34 static void init(struct avr_t * avr);
35 static void reset(struct avr_t * avr);
36
37
38 static struct mcu_t {
39         avr_t core;
40         avr_eeprom_t    eeprom;
41         avr_ioport_t    porta, portb, portc, portd;
42         avr_uart_t              uart0,uart1;
43         avr_timer8_t    timer0,timer2;
44 } mcu = {
45         .core = {
46                 .mmcu = "atmega644",
47                 DEFAULT_CORE(4),
48
49                 .init = init,
50                 .reset = reset,
51         },
52         AVR_EEPROM_DECLARE(EE_READY_vect),
53         .porta = {
54                 .name = 'A', .r_port = PORTA, .r_ddr = DDRA, .r_pin = PINA,
55                 .pcint = {
56                         .enable = AVR_IO_REGBIT(PCICR, PCIE0),
57                         .raised = AVR_IO_REGBIT(PCIFR, PCIF0),
58                         .vector = PCINT0_vect,
59                 },
60                 .r_pcint = PCMSK0,
61         },
62         .portb = {
63                 .name = 'B', .r_port = PORTB, .r_ddr = DDRB, .r_pin = PINB,
64                 .pcint = {
65                         .enable = AVR_IO_REGBIT(PCICR, PCIE1),
66                         .raised = AVR_IO_REGBIT(PCIFR, PCIF1),
67                         .vector = PCINT1_vect,
68                 },
69                 .r_pcint = PCMSK1,
70         },
71         .portc = {
72                 .name = 'C', .r_port = PORTC, .r_ddr = DDRC, .r_pin = PINC,
73                 .pcint = {
74                         .enable = AVR_IO_REGBIT(PCICR, PCIE2),
75                         .raised = AVR_IO_REGBIT(PCIFR, PCIF2),
76                         .vector = PCINT2_vect,
77                 },
78                 .r_pcint = PCMSK2,
79         },
80         .portd = {
81                 .name = 'D', .r_port = PORTD, .r_ddr = DDRD, .r_pin = PIND,
82                 .pcint = {
83                         .enable = AVR_IO_REGBIT(PCICR, PCIE3),
84                         .raised = AVR_IO_REGBIT(PCIFR, PCIF3),
85                         .vector = PCINT3_vect,
86                 },
87                 .r_pcint = PCMSK3,
88         },
89
90         .uart0 = {
91                 .disabled = AVR_IO_REGBIT(PRR,PRUSART0),
92                 .name = '0',
93                 .r_udr = UDR0,
94
95                 .txen = AVR_IO_REGBIT(UCSR0B, TXEN0),
96                 .rxen = AVR_IO_REGBIT(UCSR0B, RXEN0),
97
98                 .r_ucsra = UCSR0A,
99                 .r_ucsrb = UCSR0B,
100                 .r_ucsrc = UCSR0C,
101                 .r_ubrrl = UBRR0L,
102                 .r_ubrrh = UBRR0H,
103                 .rxc = {
104                         .enable = AVR_IO_REGBIT(UCSR0B, RXCIE0),
105                         .raised = AVR_IO_REGBIT(UCSR0A, RXC0),
106                         .vector = USART0_RX_vect,
107                 },
108                 .txc = {
109                         .enable = AVR_IO_REGBIT(UCSR0B, TXCIE0),
110                         .raised = AVR_IO_REGBIT(UCSR0A, TXC0),
111                         .vector = USART0_TX_vect,
112                 },
113                 .udrc = {
114                         .enable = AVR_IO_REGBIT(UCSR0B, UDRIE0),
115                         .raised = AVR_IO_REGBIT(UCSR0A, UDRE0),
116                         .vector = USART0_UDRE_vect,
117                 },
118         },
119         .uart1 = {
120                 .disabled = AVR_IO_REGBIT(PRR,PRUSART1),
121                 .name = '1',
122                 .r_udr = UDR1,
123
124                 .txen = AVR_IO_REGBIT(UCSR1B, TXEN1),
125                 .rxen = AVR_IO_REGBIT(UCSR1B, RXEN1),
126
127                 .r_ucsra = UCSR1A,
128                 .r_ucsrb = UCSR1B,
129                 .r_ucsrc = UCSR1C,
130                 .r_ubrrl = UBRR1L,
131                 .r_ubrrh = UBRR1H,
132                 .rxc = {
133                         .enable = AVR_IO_REGBIT(UCSR1B, RXCIE1),
134                         .raised = AVR_IO_REGBIT(UCSR1A, RXC1),
135                         .vector = USART1_RX_vect,
136                 },
137                 .txc = {
138                         .enable = AVR_IO_REGBIT(UCSR1B, TXCIE1),
139                         .raised = AVR_IO_REGBIT(UCSR1A, TXC1),
140                         .vector = USART1_TX_vect,
141                 },
142                 .udrc = {
143                         .enable = AVR_IO_REGBIT(UCSR1B, UDRIE1),
144                         .raised = AVR_IO_REGBIT(UCSR1A, UDRE1),
145                         .vector = USART1_UDRE_vect,
146                 },
147         },
148
149         .timer0 = {
150                 .name = '0',
151                 .wgm = { AVR_IO_REGBIT(TCCR0A, WGM00), AVR_IO_REGBIT(TCCR0A, WGM01), AVR_IO_REGBIT(TCCR0B, WGM02) },
152                 .cs = { AVR_IO_REGBIT(TCCR0B, CS00), AVR_IO_REGBIT(TCCR0B, CS01), AVR_IO_REGBIT(TCCR0B, CS02) },
153                 .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ },
154
155                 .r_ocra = OCR0A,
156                 .r_ocrb = OCR0B,
157                 .r_tcnt = TCNT0,
158
159                 .overflow = {
160                         .enable = AVR_IO_REGBIT(TIMSK0, TOIE0),
161                         .raised = AVR_IO_REGBIT(TIFR0, TOV0),
162                         .vector = TIMER0_OVF_vect,
163                 },
164                 .compa = {
165                         .enable = AVR_IO_REGBIT(TIMSK0, OCIE0A),
166                         .raised = AVR_IO_REGBIT(TIFR0, OCF0A),
167                         .vector = TIMER0_COMPA_vect,
168                 },
169                 .compb = {
170                         .enable = AVR_IO_REGBIT(TIMSK0, OCIE0B),
171                         .raised = AVR_IO_REGBIT(TIFR0, OCF0B),
172                         .vector = TIMER0_COMPB_vect,
173                 },
174         },
175         .timer2 = {
176                 .name = '2',
177                 .wgm = { AVR_IO_REGBIT(TCCR2A, WGM20), AVR_IO_REGBIT(TCCR2A, WGM21), AVR_IO_REGBIT(TCCR2B, WGM22) },
178                 .cs = { AVR_IO_REGBIT(TCCR2B, CS20), AVR_IO_REGBIT(TCCR2B, CS21), AVR_IO_REGBIT(TCCR2B, CS22) },
179                 .cs_div = { 0, 0, 3 /* 8 */, 5 /* 32 */, 6 /* 64 */, 7 /* 128 */, 8 /* 256 */, 10 /* 1024 */ },
180
181                 .r_ocra = OCR2A,
182                 .r_ocrb = OCR2B,
183                 .r_tcnt = TCNT2,
184                 
185                 // asynchronous timer source bit.. if set, use 32khz frequency
186                 .as2 = AVR_IO_REGBIT(ASSR, AS2),
187                 
188                 .overflow = {
189                         .enable = AVR_IO_REGBIT(TIMSK2, TOIE2),
190                         .raised = AVR_IO_REGBIT(TIFR2, TOV2),
191                         .vector = TIMER2_OVF_vect,
192                 },
193                 .compa = {
194                         .enable = AVR_IO_REGBIT(TIMSK2, OCIE2A),
195                         .raised = AVR_IO_REGBIT(TIFR2, OCF2A),
196                         .vector = TIMER2_COMPA_vect,
197                 },
198                 .compb = {
199                         .enable = AVR_IO_REGBIT(TIMSK2, OCIE2B),
200                         .raised = AVR_IO_REGBIT(TIFR2, OCF2B),
201                         .vector = TIMER2_COMPB_vect,
202                 },
203         },
204 };
205
206 static avr_t * make()
207 {
208         return &mcu.core;
209 }
210
211 avr_kind_t mega644 = {
212         .names = { "atmega644", "atmega644p" },
213         .make = make
214 };
215
216 static void init(struct avr_t * avr)
217 {
218         struct mcu_t * mcu = (struct mcu_t*)avr;
219
220         printf("%s init\n", avr->mmcu);
221         
222         avr_eeprom_init(avr, &mcu->eeprom);
223         avr_ioport_init(avr, &mcu->porta);
224         avr_ioport_init(avr, &mcu->portb);
225         avr_ioport_init(avr, &mcu->portc);
226         avr_ioport_init(avr, &mcu->portd);
227         avr_uart_init(avr, &mcu->uart0);
228         avr_uart_init(avr, &mcu->uart1);
229         avr_timer8_init(avr, &mcu->timer0);
230         avr_timer8_init(avr, &mcu->timer2);
231 }
232
233 static void reset(struct avr_t * avr)
234 {
235 //      struct mcu_t * mcu = (struct mcu_t*)avr;
236 }