]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/z80-if.c
5 * | Z180-Sig | AVR-Port | Dir | Special Function |
6 * +------------+---------------+-------+-----------------------+
17 * | A10 | PC 2 | O | |
18 * | A11 | PC 3 | O | |
19 * | A12 | PC 4 | O | |
20 * | A13 | PC 5 | O | |
21 * | A14 | PC 6 | O | |
22 * | A15 | PC 7 | O | |
23 * | A16 | PE 2 | O | |
24 * | A17 | PE 3 | O | |
25 * | A18 | PE 4 | O | |
26 * | D0 | PF 0 | I/O | |
27 * | D1 | PF 1 | I/O | |
28 * | D2 | PF 2 | I/O | |
29 * | D3 | PF 3 | I/O | |
30 * | D4 | PF 4 | I/O | |
31 * | D5 | PF 5 | I/O | |
32 * | D6 | PF 6 | I/O | |
33 * | D7 | PF 7 | I/O | |
36 * | MREQ | PD 4 | O | |
37 * | RST | PD 5 | O | |
38 * | BUSREQ | PD 7 | O | |
39 * | BUSACK | PD 6 | I | |
40 * | IOCS1 | PE 5 | I | |
44 * | | P | | af1 USART1_TX |
45 * | | P | | af1 USART1_RX |
46 * | | P |JTDI | remap SPI1_NSS' |
47 * | | P |JTDO | remap SPI1_SCK' |
48 * | | P |JTRST | remap SPI1_MISO' |
49 * | | P | | remap SPI1_MOSI' |
50 * | | P | | af1 OSC32 |
51 * | | P | | af1 OSC32 |
57 #include <util/delay.h>
58 #include <util/atomic.h>
64 /* Number of array elements */
65 #define NELEMS(x) (sizeof x/sizeof *x)
68 #define CONCAT(x,y) x ## y
69 #define EVALUATOR(x,y) CONCAT(x,y)
71 #define GPIO_(X) CONCAT(GPIO, X)
82 } __attribute__((__packed__
));
84 #define SBIT(port,pin) ((*(volatile struct bits*)&port).b##pin)
97 #define P_BUSREQ PORTD
99 #define DDR_BUSREQ DDRD
100 #define P_BUSACK PORTD
101 #define PIN_BUSACK PIND
103 #define DDR_BUSACK DDRD
104 //#define P_HALT PORTA
106 #define P_IOCS1 PORTE
108 #define DDR_IOCS1 DDRE
109 //#define P_NMI PORTB
130 //#define ADB_PORT PORTE
133 #define Z80_O_ZCLK SBIT(P_ZCLK, 7)
134 #define Z80_O_MREQ SBIT(P_MREQ, 4)
135 #define Z80_O_RD SBIT(P_RD, 3)
136 #define Z80_O_WR SBIT(P_WR, 2)
137 #define Z80_O_BUSREQ SBIT(P_BUSREQ, 7)
138 //#define Z80_O_NMI SBIT(P_NMI, )
139 #define Z80_O_RST SBIT(P_RST, 5)
140 #define Z80_I_BUSACK SBIT(PIN_BUSACK, 6)
141 //#define Z80_I_HALT SBIT(P_HALT, )
144 #define MASK(n) ((1<<(n))-1)
145 #define SMASK(w,s) (MASK(w) << (s))
156 static zstate_t zstate
;
158 /*--------------------------------------------------------------------------*/
161 static void z80_setup_addrbus_tristate(void)
163 /* /MREQ, /RD, /WR: Input, no pullup */
164 DDR_MREQ
&= ~(_BV(MREQ
) | _BV(RD
) | _BV(WR
));
173 PIN_ADB
= P_ADB
& ~(MASK(ADB_WIDTH
) << ADB_SHIFT
);
174 DDR_ADB
= DDR_ADB
& ~(MASK(ADB_WIDTH
) << ADB_SHIFT
);
178 static void z80_setup_addrbus_active(void)
180 /* /MREQ, /RD, /WR: Output and high */
184 DDR_MREQ
|= _BV(MREQ
) | _BV(RD
) | _BV(WR
);
188 DDR_ADB
= DDR_ADB
| (MASK(ADB_WIDTH
) << ADB_SHIFT
);
192 static void z80_setup_dbus_in(void)
199 static void z80_setup_dbus_out(void)
205 static void z80_reset_pulse(void)
213 void z80_setup_clock(void)
215 /* ZCLK: Output and low */
216 DDR_ZCLK
|= _BV(ZCLK
);
219 /* Timer0: CTC: Toggle OC0A on compare match */
220 PRR0
&= ~_BV(PRTIM0
);
221 TCCR0A
= _BV(COM0A0
) | _BV(WGM01
);
223 TCCR0B
= (0x01 << CS00
);
226 void z80_setup_bus(void)
230 /* /ZRESET: Output and low */
234 /* /BUSREQ: Output and high */
236 DDR_BUSREQ
|= _BV(BUSREQ
);
238 /* /BUSACK: Input, no pullup */
239 DDR_BUSACK
&= ~_BV(BUSACK
);
240 P_BUSACK
&= ~_BV(BUSACK
);
242 /* /IOCS1: Input, no pullup */
243 DDR_IOCS1
&= ~_BV(IOCS1
);
244 P_IOCS1
&= ~_BV(IOCS1
);
246 z80_setup_addrbus_tristate();
253 zstate_t
z80_bus_state(void)
259 static void z80_busreq_hpulse(void)
262 z80_setup_addrbus_tristate();
264 ATOMIC_BLOCK(ATOMIC_RESTORESTATE
) {
266 Z80_O_BUSREQ
= 1; /* 2 AVR clock cycles */
267 Z80_O_BUSREQ
= 0; /* 2 AVR clock cycles */
270 if (zstate
& ZST_ACQUIRED
) {
271 while(Z80_I_BUSACK
== 1)
273 z80_setup_addrbus_active();
281 + State | RESET | RESET_AQRD | RUNNING | RUNNING_AQRD |
285 ----------------+---------------+---------------+---------------+---------------+
287 Reset | 0 | 0 | 0 | 0 |
290 Request | 1 | | 3 | |
293 Release | | 0 | | 2 |
299 Restart | | | 2 | 3 |
307 zstate_t
z80_bus_cmd(bus_cmd_t cmd
)
313 z80_setup_addrbus_tristate();
324 while(Z80_I_BUSACK
== 1)
326 z80_setup_addrbus_active();
332 while(Z80_I_BUSACK
== 1)
334 z80_setup_addrbus_active();
335 zstate
= RUNNING_AQRD
;
347 z80_setup_addrbus_tristate();
354 z80_setup_addrbus_tristate();
372 z80_setup_addrbus_tristate();
374 z80_setup_addrbus_active();
375 zstate
= RUNNING_AQRD
;
406 /*--------------------------------------------------------------------------*/
409 //inline __attribute__ ((always_inline))
410 void z80_setaddress(uint32_t addr
)
412 addr_t x
; x
.l
= addr
;
416 PIN_ADB
= ((x
.b
[2] << ADB_SHIFT
) ^ P_ADB
) & MASK(ADB_WIDTH
) << ADB_SHIFT
;
419 void z80_write(uint32_t addr
, uint8_t data
)
421 z80_setaddress(addr
);
423 z80_setup_dbus_out();
432 uint8_t z80_read(uint32_t addr
)
436 z80_setaddress(addr
);
450 void z80_memset(uint32_t addr
, uint8_t data
, uint32_t length
)
452 z80_setup_dbus_out();
455 z80_setaddress(addr
++);
465 void z80_write_block(const __flash
uint8_t *src
, uint32_t dest
, uint32_t length
)
469 z80_setup_dbus_out();
472 z80_setaddress(dest
++);
484 0179' rx.bs_mask: ds 1 ; (buf_len - 1)
485 017A' rx.in_idx: ds 1 ;
486 017B' rx.out_idx: ds 1 ;
487 017C' rx.buf: ds rx.buf_len ;
488 018B' rx.buf_end equ $-1 ; last byte (start+len-1)
490 018C' tx.bs_mask: ds 1 ; (buf_len - 1)
491 018D' tx.in_idx: ds 1 ;
492 018E' tx.out_idx: ds 1 ;
493 018F' tx.buf: ds tx.buf_len ;
494 019E' tx.buf_end equ $-1 ; last byte
498 typedef struct __attribute__((packed
)) {
507 #define FIFO_BUFSIZE_MASK -3
508 #define FIFO_INDEX_IN -2
509 #define FIFO_INDEX_OUT -1
517 } fifo_dsc
[NUM_FIFOS
];
520 void z80_memfifo_init(const fifo_t f
, uint32_t adr
)
523 DBG_P(2, "z80_memfifo_init: %i, %lx\n", f
, adr
);
525 fifo_dsc
[f
].base
= adr
;
527 z80_bus_cmd(Request
);
529 fifo_dsc
[f
].mask
= z80_read(adr
+ FIFO_BUFSIZE_MASK
);
530 fifo_dsc
[f
].idx_in
= z80_read(adr
+ FIFO_INDEX_IN
);
531 fifo_dsc
[f
].idx_out
= z80_read(adr
+ FIFO_INDEX_OUT
);
533 z80_bus_cmd(Release
);
537 int z80_memfifo_is_empty(const fifo_t f
)
541 if (fifo_dsc
[f
].base
!= 0) {
543 uint32_t adr
= fifo_dsc
[f
].base
+ FIFO_INDEX_IN
;
546 z80_bus_cmd(Request
);
548 z80_bus_cmd(Release
);
549 rc
= idx
== fifo_dsc
[f
].idx_out
;
555 int z80_memfifo_is_full(const fifo_t f
)
559 if (fifo_dsc
[f
].base
!= 0) {
560 z80_bus_cmd(Request
);
561 rc
= ((fifo_dsc
[f
].idx_in
+ 1) & fifo_dsc
[f
].mask
)
562 == z80_read(fifo_dsc
[f
].base
+FIFO_INDEX_OUT
);
563 z80_bus_cmd(Release
);
568 uint8_t z80_memfifo_getc(const fifo_t f
)
572 while (z80_memfifo_is_empty(f
))
575 z80_bus_cmd(Request
);
576 idx
= fifo_dsc
[f
].idx_out
;
577 rc
= z80_read(fifo_dsc
[f
].base
+idx
);
578 fifo_dsc
[f
].idx_out
= ++idx
& fifo_dsc
[f
].mask
;
579 z80_write(fifo_dsc
[f
].base
+FIFO_INDEX_OUT
, fifo_dsc
[f
].idx_out
);
580 z80_bus_cmd(Release
);
586 void z80_memfifo_putc(fifo_t f
, uint8_t val
)
590 while (z80_memfifo_is_full(f
))
593 z80_bus_cmd(Request
);
594 idx
= fifo_dsc
[f
].idx_in
;
595 z80_write(fifo_dsc
[f
].base
+idx
, val
);
596 fifo_dsc
[f
].idx_in
= ++idx
& fifo_dsc
[f
].mask
;
597 z80_write(fifo_dsc
[f
].base
+FIFO_INDEX_IN
, fifo_dsc
[f
].idx_in
);
598 z80_bus_cmd(Release
);
601 /*--------------------------------------------------------------------------*/
603 TODO: Rewrite msg_fifo routines for AVR
608 //uint8_t idx_out, idx_in;
613 /*--------------------------------------------------------------------------*/
617 static void tim1_setup(void)
619 RCC_APB2RSTR
|= RCC_APB2RSTR_TIM1RST
;
620 RCC_APB2RSTR
&= ~RCC_APB2RSTR_TIM1RST
;
626 /* | TIM_SMCR_ETF_CK_INT_N_2 */
631 TIM1_DIER
= TIM_DIER_TDE
;
635 | TIM_CCMR1_OC1M_FORCE_LOW
636 | TIM_CCMR1_CC1S_OUT
;
638 TIM1_SMCR
|= TIM_SMCR_SMS_TM
;
643 /*--------------------------------------------------------------------------*/
645 void z80_setup_msg_fifo(void)
647 // gpio_set_mode(P_BUSACK, GPIO_MODE_INPUT,
648 // GPIO_CNF_INPUT_FLOAT, GPIO_BUSACK | GPIO_IOCS1);
652 // msg_fifo.count = NELEMS(msg_fifo.buf);
659 void z80_init_msg_fifo(uint32_t addr
)
662 DBG_P(1, "z80_init_msg_fifo: %lx\n", addr
);
664 z80_bus_cmd(Request
);
665 z80_write(addr
+FIFO_INDEX_OUT
, z80_read(addr
+FIFO_INDEX_IN
));
666 z80_bus_cmd(Release
);
667 msg_fifo
.base
= addr
;
671 int z80_msg_fifo_getc(void)
676 if (msg_fifo
.count
!= (NELEMS(msg_fifo
.buf
) /*- DMA1_CNDTR4 */ )) {
677 c
= msg_fifo
.buf
[msg_fifo
.count
];
678 if (++msg_fifo
.count
== NELEMS(msg_fifo
.buf
))
681 if (msg_fifo
.base
!= 0) {
682 z80_bus_cmd(Request
);
683 z80_write(msg_fifo
.base
+FIFO_INDEX_OUT
, msg_fifo
.count
);
684 z80_bus_cmd(Release
);