Cores, decoder, uart, ioports - lots of changes
[simavr] / simavr / cores / sim_tiny85.c
1 /*
2         sim_tiny85.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 "simavr.h"
24 #include "sim_core_declare.h"
25 #include "avr_eeprom.h"
26 #include "avr_ioport.h"
27 #include "avr_timer8.h"
28
29 #define _AVR_IO_H_
30 #define __ASSEMBLER__
31 #include "avr/iotn85.h"
32
33 static void init(struct avr_t * avr);
34 static void reset(struct avr_t * avr);
35
36
37 static struct mcu_t {
38         avr_t core;
39         avr_eeprom_t    eeprom;
40         avr_ioport_t    portb;
41         avr_timer8_t    timer0, timer1;
42 } mcu = {
43         .core = {
44                 .mmcu = "attiny85",
45                 DEFAULT_CORE(2),
46
47                 .init = init,
48                 .reset = reset,
49         },
50         AVR_EEPROM_DECLARE(EE_RDY_vect),
51         .portb = {
52                 .name = 'B',  .r_port = PORTB, .r_ddr = DDRB, .r_pin = PINB,
53                 .pcint = {
54                         .enable = AVR_IO_REGBIT(GIMSK, PCIE),
55                         .raised = AVR_IO_REGBIT(GIFR, PCIF),
56                         .vector = PCINT0_vect,
57                 },
58                 .r_pcint = PCMSK,
59         },
60         .timer0 = {
61                 .name = '0',
62                 .wgm = { AVR_IO_REGBIT(TCCR0A, WGM00), AVR_IO_REGBIT(TCCR0A, WGM01), AVR_IO_REGBIT(TCCR0B, WGM02) },
63                 .cs = { AVR_IO_REGBIT(TCCR0B, CS00), AVR_IO_REGBIT(TCCR0B, CS01), AVR_IO_REGBIT(TCCR0B, CS02) },
64                 .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ },
65
66                 .r_ocra = OCR0A,
67                 .r_ocrb = OCR0B,
68                 .r_tcnt = TCNT0,
69
70                 .overflow = {
71                         .enable = AVR_IO_REGBIT(TIMSK, TOIE0),
72                         .raised = AVR_IO_REGBIT(TIFR, TOV0),
73                         .vector = TIMER0_OVF_vect,
74                 },
75                 .compa = {
76                         .enable = AVR_IO_REGBIT(TIMSK, OCIE0A),
77                         .raised = AVR_IO_REGBIT(TIFR, OCF0A),
78                         .vector = TIMER0_COMPA_vect,
79                 },
80                 .compb = {
81                         .enable = AVR_IO_REGBIT(TIMSK, OCIE0B),
82                         .raised = AVR_IO_REGBIT(TIFR, OCF0B),
83                         .vector = TIMER0_COMPB_vect,
84                 },
85         },
86         .timer1 = {
87                 .name = '1',
88                 // no wgm bits
89                 .cs = { AVR_IO_REGBIT(TCCR1, CS10), AVR_IO_REGBIT(TCCR1, CS11), AVR_IO_REGBIT(TCCR1, CS12), AVR_IO_REGBIT(TCCR1, CS13) },
90                 .cs_div = { 0, 0, 1 /* 2 */, 2 /* 4 */, 3 /* 8 */, 4 /* 16 */ },
91
92                 .r_ocra = OCR1A,
93                 .r_ocrb = OCR1B,
94                 .r_ocrc = OCR1C,
95                 .r_tcnt = TCNT1,
96
97                 .overflow = {
98                         .enable = AVR_IO_REGBIT(TIMSK, TOIE1),
99                         .raised = AVR_IO_REGBIT(TIFR, TOV1),
100                         .vector = TIMER1_OVF_vect,
101                 },
102                 .compa = {
103                         .enable = AVR_IO_REGBIT(TIMSK, OCIE1A),
104                         .raised = AVR_IO_REGBIT(TIFR, OCF1A),
105                         .vector = TIMER1_COMPA_vect,
106                 },
107                 .compb = {
108                         .enable = AVR_IO_REGBIT(TIMSK, OCIE1B),
109                         .raised = AVR_IO_REGBIT(TIFR, OCF1B),
110                         .vector = TIMER1_COMPB_vect,
111                 },
112         },
113
114
115 };
116
117 static avr_t * make()
118 {
119         return &mcu.core;
120 }
121
122 avr_kind_t tiny85 = {
123         .make = make
124 };
125
126 static void init(struct avr_t * avr)
127 {
128         struct mcu_t * mcu = (struct mcu_t*)avr;
129         
130         printf("%s init\n", avr->mmcu);
131         
132         avr_eeprom_init(avr, &mcu->eeprom);
133         avr_ioport_init(avr, &mcu->portb);
134         avr_timer8_init(avr, &mcu->timer0);
135         avr_timer8_init(avr, &mcu->timer1);
136 }
137
138 static void reset(struct avr_t * avr)
139 {
140 //      struct mcu_t * mcu = (struct mcu_t*)avr;
141 }