avr: initial commit
[z80.git] / avr / main.c
1 /* Z80 Mainboard Controller */
2
3 #include <stdio.h>
4
5 #include <avr/io.h>
6 #include <avr/interrupt.h>
7 #include <avr/pgmspace.h>
8 #include <util/delay.h>
9
10 #include "uart.h"
11 #include "bus.h"
12 #include "i2c.h"
13 #include "mem.h"
14 #include "io.h"
15
16 #include "monitor.h"
17
18 uint8_t xorshift(uint8_t new_seed)
19 {
20         static uint8_t seed = 0xA5;
21         if(new_seed) seed=new_seed;
22
23         seed ^= (seed << 1);
24         seed ^= (seed >> 1);
25         seed ^= (seed << 2);
26         return(seed);
27 }
28
29 int main(void)
30 {
31         /* initialize serial port and stdin/stdout */
32         uart_init();
33         stdout = &uart_out;
34         stdin  = &uart_in;
35         sei();
36
37         /* initialize hardware and monitor */
38         printf_P(PSTR("\nAVR INIT "));
39         printf_P(PSTR("[BUS")); bus_init(); printf_P(PSTR("] "));
40         printf_P(PSTR("[I2C")); i2c_init(); printf_P(PSTR("] "));
41         printf_P(PSTR("[MEM")); mem_init(); printf_P(PSTR("] "));
42         printf_P(PSTR("[I/O"));  io_init(); printf_P(PSTR("] "));
43         printf_P(PSTR("[MON] ")); monitor_init();
44         printf_P(PSTR("DONE.\nSend BREAK to enter monitor.\n"));
45
46         /* load initial program into memory */
47         menu_ihex();
48
49 #if 0
50         mem_start();
51         for(uint8_t i = 0; i < 8; i++) printf("%02x ", mem_read(i));
52         mem_stop();
53 #endif
54
55         /* reset Z80 */
56         printf_P(PSTR("Go!\n"));
57         bus_reset();
58         bus_unlock();
59         bus_wait();
60
61         while(1) {
62                 if(!(MEMPIN & (1 << SEL))) {    /* got I/O request */
63
64                         /* answer the I/O request */
65                         if(!(MEMPIN & (1 << RD))) {
66 //                              printf_P(PSTR("<IORD>"));
67                                 io_read();
68                         } else if(!(MEMPIN & (1 << WR))) {
69 //                              printf_P(PSTR("<IOWR>"));
70                                 io_write();
71                         } else {
72                                 printf_P(PSTR("<IOFAIL>"));
73                                 while(1);
74                         }
75
76                         while(!(MEMPIN & (1 << SEL)));
77                 }
78
79                 if(uart_break) monitor();       /* enter monitor on break */
80         }
81
82         printf_P(PSTR("\nAVR END\n"));
83         while(1);
84 }