Cores, decoder, uart, ioports - lots of changes
[simavr] / simavr / sim / avr_uart.c
1 /*
2         avr_uart.c
3
4         Handles UART access
5         Right now just handle "write" to the serial port at any speed
6         and printf to the console when '\n' is written.
7
8         Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
9
10         This file is part of simavr.
11
12         simavr is free software: you can redistribute it and/or modify
13         it under the terms of the GNU General Public License as published by
14         the Free Software Foundation, either version 3 of the License, or
15         (at your option) any later version.
16
17         simavr is distributed in the hope that it will be useful,
18         but WITHOUT ANY WARRANTY; without even the implied warranty of
19         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20         GNU General Public License for more details.
21
22         You should have received a copy of the GNU General Public License
23         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
24  */
25
26 #include <stdio.h>
27 #include "avr_uart.h"
28
29 static void avr_uart_run(avr_t * avr, avr_io_t * port)
30 {
31 //      printf("%s\n", __FUNCTION__);
32 }
33
34 static uint8_t avr_uart_read(struct avr_t * avr, uint8_t addr, void * param)
35 {
36 //      avr_uart_t * p = (avr_uart_t *)param;
37         uint8_t v = avr->data[addr];
38 //      printf("** PIN%c = %02x\n", p->name, v);
39         return v;
40 }
41
42 static void avr_uart_write(struct avr_t * avr, uint8_t addr, uint8_t v, void * param)
43 {
44         avr_uart_t * p = (avr_uart_t *)param;
45
46         if (addr == p->r_udr) {
47         //      printf("UDR%c(%02x) = %02x\n", p->name, addr, v);
48                 avr_core_watch_write(avr, addr, v);
49
50                 // if the interupts are not used, still raised the UDRE and TXC flaga
51                 avr_raise_interupt(avr, &p->udrc);
52                 avr_raise_interupt(avr, &p->txc);
53
54                 static char buf[128];
55                 static int l = 0;
56                 buf[l++] = v < ' ' ? '.' : v;
57                 buf[l] = 0;
58                 if (v == '\n' || l == 127) {
59                         l = 0;
60                         printf("\e[32m%s\e[0m\n", buf);
61                 }
62         }
63         if (addr == p->r_ucsra) {
64                 // get the bits before the write
65                 uint8_t udre = avr_regbit_get(avr, p->udrc.raised);
66                 uint8_t txc = avr_regbit_get(avr, p->txc.raised);
67                 
68                 avr_core_watch_write(avr, addr, v);
69
70                 // if writing one to a one, clear bit
71                 if (udre && avr_regbit_get(avr, p->udrc.raised))
72                         avr_regbit_clear(avr, p->udrc.raised);
73                 if (txc && avr_regbit_get(avr, p->txc.raised))
74                         avr_regbit_clear(avr, p->txc.raised);
75         }
76 }
77
78 void avr_uart_reset(avr_t * avr, struct avr_io_t *io)
79 {
80         avr_uart_t * p = (avr_uart_t *)io;
81         avr_regbit_set(avr, p->udrc.raised);
82 }
83
84 static  avr_io_t        _io = {
85         .kind = "uart",
86         .run = avr_uart_run,
87         .reset = avr_uart_reset,
88 };
89
90 void avr_uart_init(avr_t * avr, avr_uart_t * p)
91 {
92         p->io = _io;
93         avr_register_io(avr, &p->io);
94
95         printf("%s UART%c UDR=%02x\n", __FUNCTION__, p->name, p->r_udr);
96
97         avr_register_io_write(avr, p->r_udr, avr_uart_write, p);
98         avr_register_io_write(avr, p->r_ucsra, avr_uart_write, p);
99         avr_register_io_read(avr, p->r_udr, avr_uart_read, p);
100
101 }
102