GDB working, some more source massaging
[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 DEFINE_FIFO(uint8_t, uart_fifo, 128);
30
31 static void avr_uart_run(avr_t * avr, avr_io_t * port)
32 {
33         avr_uart_t * p = (avr_uart_t *)port;
34         if (p->input_cycle_timer) {
35                 p->input_cycle_timer--;
36                 if (p->input_cycle_timer == 0) {
37                         if (avr_regbit_get(avr, p->rxen))
38                                 avr_raise_interrupt(avr, &p->rxc);
39                 }
40         }
41 }
42
43 static uint8_t avr_uart_read(struct avr_t * avr, uint8_t addr, void * param)
44 {
45         avr_uart_t * p = (avr_uart_t *)param;
46
47         if (!avr_regbit_get(avr, p->rxen)) {
48                 avr->data[addr] = 0;
49                 // made to trigger potential watchpoints
50                 avr_core_watch_read(avr, addr);
51                 return 0;
52         }
53         uint8_t v = uart_fifo_read(&p->input);
54
55         avr->data[addr] = v;
56         // made to trigger potential watchpoints
57         v = avr_core_watch_read(avr, addr);
58         p->input_cycle_timer = uart_fifo_isempty(&p->input) ? 0 : 10;
59         return v;
60 }
61
62 static void avr_uart_write(struct avr_t * avr, uint8_t addr, uint8_t v, void * param)
63 {
64         avr_uart_t * p = (avr_uart_t *)param;
65
66         if (addr == p->r_udr) {
67         //      printf("UDR%c(%02x) = %02x\n", p->name, addr, v);
68                 avr_core_watch_write(avr, addr, v);
69
70                 // if the interrupts are not used, still raised the UDRE and TXC flaga
71                 avr_raise_interrupt(avr, &p->udrc);
72                 avr_raise_interrupt(avr, &p->txc);
73
74                 static char buf[128];
75                 static int l = 0;
76                 buf[l++] = v < ' ' ? '.' : v;
77                 buf[l] = 0;
78                 if (v == '\n' || l == 127) {
79                         l = 0;
80                         printf("\e[32m%s\e[0m\n", buf);
81                 }
82                 // tell other modules we are "outputing" a byte
83                 if (avr_regbit_get(avr, p->txen))
84                         avr_raise_irq(avr, p->io.irq + UART_IRQ_OUTPUT, v);
85         } else {
86                 // get the bits before the write
87                 uint8_t udre = avr_regbit_get(avr, p->udrc.raised);
88                 uint8_t txc = avr_regbit_get(avr, p->txc.raised);
89
90                 avr_core_watch_write(avr, addr, v);
91
92                 // if writing one to a one, clear bit
93                 if (udre && avr_regbit_get(avr, p->udrc.raised))
94                         avr_regbit_clear(avr, p->udrc.raised);
95                 if (txc && avr_regbit_get(avr, p->txc.raised))
96                         avr_regbit_clear(avr, p->txc.raised);
97         }
98 }
99
100 static void avr_uart_irq_input(avr_t * avr, struct avr_irq_t * irq, uint32_t value, void * param)
101 {
102         avr_uart_t * p = (avr_uart_t *)param;
103
104         // check to see fi receiver is enabled
105         if (!avr_regbit_get(avr, p->rxen))
106                 return;
107
108         uart_fifo_write(&p->input, value); // add to fifo
109         // raise interrupt, if it was not there
110         if (p->input_cycle_timer == 0)
111                 p->input_cycle_timer = 10;      // random number, should be proportional to speed
112 }
113
114
115 void avr_uart_reset(avr_t * avr, struct avr_io_t *io)
116 {
117         avr_uart_t * p = (avr_uart_t *)io;
118         avr_regbit_set(avr, p->udrc.raised);
119         avr_irq_register_notify(avr, p->io.irq + UART_IRQ_INPUT, avr_uart_irq_input, p);
120         p->input_cycle_timer = 0;
121         uart_fifo_reset(&p->input);
122 }
123
124 static  avr_io_t        _io = {
125         .kind = "uart",
126         .run = avr_uart_run,
127         .reset = avr_uart_reset,
128 };
129
130 void avr_uart_init(avr_t * avr, avr_uart_t * p)
131 {
132         p->io = _io;
133         avr_register_io(avr, &p->io);
134
135 //      printf("%s UART%c UDR=%02x\n", __FUNCTION__, p->name, p->r_udr);
136
137         // allocate this module's IRQ
138         p->io.irq_count = UART_IRQ_COUNT;
139         p->io.irq = avr_alloc_irq(avr, 0, p->io.irq_count);
140         p->io.irq_ioctl_get = AVR_IOCTL_UART_GETIRQ(p->name);
141
142         avr_register_io_write(avr, p->r_udr, avr_uart_write, p);
143         avr_register_io_read(avr, p->r_udr, avr_uart_read, p);
144
145         avr_register_io_write(avr, p->r_ucsra, avr_uart_write, p);
146 }
147