avr: initial commit
[z80.git] / avr / bus.c
1 /* BUS arbitration */
2
3 #include <avr/io.h>
4 #include <util/delay.h>
5
6 #include "bus.h"
7
8 /* initialize bus arbitration pins */
9 void bus_init(void)
10 {
11         /* /INT, /RESET, /BUSREQ, /WAIT, CLOCK as HIGH outputs */
12         BUSDDR  |= (1 << INT) | (1 << RESET) | (1 << BUSREQ) | (1 << WAIT) | (1 << CLOCK);
13         BUSPORT |= (1 << INT) | (1 << RESET) | (1 << BUSREQ) | (1 << WAIT) | (1 << CLOCK);
14
15         /* /BUSACK as input with pullups */
16         BUSDDR  &= ~(1 << BUSACK);
17         BUSPORT |=  (1 << BUSACK);
18
19         /* initialize clock on OC0B */
20         TCNT0  = 0;
21 //      OCR0A  = 2;
22         OCR0A  = 15;
23 //      OCR0A  = 254;
24         TCCR0A = (1 << COM0B0) | (1 << WGM01);
25         TCCR0B = (1 << CS00);
26
27         /* reset Z80, get bus, wait until we got it */
28         BUSPORT &= ~((1 << RESET) | (1 << BUSREQ));
29         _delay_ms(50);
30         BUSPORT |= (1 << RESET);
31         while(BUSPIN & (1 << BUSACK));
32 }
33
34 /* acquire memory bus from Z80, blocking */
35 /* NOTE: disables DRAM refresh! */
36 void bus_lock(void)
37 {
38         BUSPORT &= ~(1 << BUSREQ);      /* request bus */
39         while(BUSPIN & (1 << BUSACK));  /* wait until acquired */
40 }
41
42 /* return memory bus to Z80, blocking */
43 void bus_unlock(void)
44 {
45         BUSPORT |= (1 << BUSREQ);       /* release bus */
46         while(!(BUSPIN & (1 << BUSACK))); /* wait until released */
47 }
48
49 /* activate autowait: /WAIT activates on /SEL0 */
50 void bus_wait(void)
51 {
52         BUSPORT |= (1 << WAIT);
53         return;
54 }
55
56 /* release autowait: /WAIT gets released immediately */
57 void bus_nowait(void)
58 {
59         BUSPORT &= ~(1 << WAIT);
60 }
61
62 /* reset Z80 and devices */
63 void bus_reset(void)
64 {
65         BUSPORT &= ~(1 << RESET);
66         _delay_ms(50);
67         BUSPORT |= (1 << RESET);
68 }