]>
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)
95 #define P_BUSREQ PORTD
97 #define DDR_BUSREQ DDRD
98 #define P_BUSACK PORTD
99 #define PIN_BUSACK PIND
101 #define DDR_BUSACK DDRD
102 //#define P_HALT PORTA
104 #define P_IOCS1 PORTE
106 #define DDR_IOCS1 DDRE
107 //#define P_NMI PORTB
128 //#define ADB_PORT PORTE
131 #define Z80_O_MREQ SBIT(P_MREQ, 4)
132 #define Z80_O_RD SBIT(P_RD, 3)
133 #define Z80_O_WR SBIT(P_WR, 2)
134 #define Z80_O_BUSREQ SBIT(P_BUSREQ, 7)
135 //#define Z80_O_NMI SBIT(P_NMI, )
136 #define Z80_O_RST SBIT(P_RST, 5)
137 #define Z80_I_BUSACK SBIT(PIN_BUSACK, 6)
138 //#define Z80_I_HALT SBIT(P_HALT, )
141 #define MASK(n) ((1<<(n))-1)
142 #define SMASK(w,s) (MASK(w) << (s))
153 static zstate_t zstate
;
155 /*--------------------------------------------------------------------------*/
158 static void z80_setup_addrbus_tristate(void)
160 /* /MREQ, /RD, /WR: Input, no pullup */
161 DDR_MREQ
&= ~(_BV(MREQ
) | _BV(RD
) | _BV(WR
));
170 PIN_ADB
= P_ADB
& ~(MASK(ADB_WIDTH
) << ADB_SHIFT
);
171 DDR_ADB
= DDR_ADB
& ~(MASK(ADB_WIDTH
) << ADB_SHIFT
);
175 static void z80_setup_addrbus_active(void)
177 /* /MREQ, /RD, /WR: Output and high */
181 DDR_MREQ
|= _BV(MREQ
) | _BV(RD
) | _BV(WR
);
185 DDR_ADB
= DDR_ADB
| (MASK(ADB_WIDTH
) << ADB_SHIFT
);
189 static void z80_setup_dbus_in(void)
196 static void z80_setup_dbus_out(void)
202 static void z80_reset_pulse(void)
210 void z80_setup_bus(void)
212 /* /ZRESET: Output and low */
216 /* /BUSREQ: Output and high */
218 DDR_BUSREQ
|= _BV(BUSREQ
);
220 /* /BUSACK: Input, no pullup */
221 DDR_BUSACK
&= ~_BV(BUSACK
);
222 P_BUSACK
&= ~_BV(BUSACK
);
224 /* /IOCS1: Input, no pullup */
225 DDR_IOCS1
&= ~_BV(IOCS1
);
226 P_IOCS1
&= ~_BV(IOCS1
);
228 z80_setup_addrbus_tristate();
235 zstate_t
z80_bus_state(void)
241 static void z80_busreq_hpulse(void)
244 z80_setup_addrbus_tristate();
246 ATOMIC_BLOCK(ATOMIC_RESTORESTATE
) {
248 Z80_O_BUSREQ
= 1; /* 2 AVR clock cycles */
249 Z80_O_BUSREQ
= 0; /* 2 AVR clock cycles */
252 if (zstate
& ZST_ACQUIRED
) {
253 while(Z80_I_BUSACK
== 1)
255 z80_setup_addrbus_active();
263 + State | RESET | RESET_AQRD | RUNNING | RUNNING_AQRD |
267 ----------------+---------------+---------------+---------------+---------------+
269 Reset | 0 | 0 | 0 | 0 |
272 Request | 1 | | 3 | |
275 Release | | 0 | | 2 |
281 Restart | | | 2 | 3 |
289 zstate_t
z80_bus_cmd(bus_cmd_t cmd
)
295 z80_setup_addrbus_tristate();
306 while(Z80_I_BUSACK
== 1)
308 z80_setup_addrbus_active();
314 while(Z80_I_BUSACK
== 1)
316 z80_setup_addrbus_active();
317 zstate
= RUNNING_AQRD
;
329 z80_setup_addrbus_tristate();
336 z80_setup_addrbus_tristate();
354 z80_setup_addrbus_tristate();
356 z80_setup_addrbus_active();
357 zstate
= RUNNING_AQRD
;
388 /*--------------------------------------------------------------------------*/
391 //inline __attribute__ ((always_inline))
392 void z80_setaddress(uint32_t addr
)
394 addr_t x
; x
.l
= addr
;
398 PIN_ADB
= ((x
.b
[2] << ADB_SHIFT
) ^ P_ADB
) & MASK(ADB_WIDTH
) << ADB_SHIFT
;
401 void z80_write(uint32_t addr
, uint8_t data
)
403 z80_setaddress(addr
);
405 z80_setup_dbus_out();
414 uint8_t z80_read(uint32_t addr
)
418 z80_setaddress(addr
);
432 void z80_memset(uint32_t addr
, uint8_t data
, uint32_t length
)
434 z80_setup_dbus_out();
437 z80_setaddress(addr
++);
447 void z80_write_block(const __flash
uint8_t *src
, uint32_t dest
, uint32_t length
)
451 z80_setup_dbus_out();
454 z80_setaddress(dest
++);
466 0179' rx.bs_mask: ds 1 ; (buf_len - 1)
467 017A' rx.in_idx: ds 1 ;
468 017B' rx.out_idx: ds 1 ;
469 017C' rx.buf: ds rx.buf_len ;
470 018B' rx.buf_end equ $-1 ; last byte (start+len-1)
472 018C' tx.bs_mask: ds 1 ; (buf_len - 1)
473 018D' tx.in_idx: ds 1 ;
474 018E' tx.out_idx: ds 1 ;
475 018F' tx.buf: ds tx.buf_len ;
476 019E' tx.buf_end equ $-1 ; last byte
480 typedef struct __attribute__((packed
)) {
489 #define FIFO_BUFSIZE_MASK -3
490 #define FIFO_INDEX_IN -2
491 #define FIFO_INDEX_OUT -1
499 } fifo_dsc
[NUM_FIFOS
];
502 void z80_memfifo_init(const fifo_t f
, uint32_t adr
)
505 DBG_P(2, "z80_memfifo_init: %i, %lx\n", f
, adr
);
507 fifo_dsc
[f
].base
= adr
;
509 z80_bus_cmd(Request
);
511 fifo_dsc
[f
].mask
= z80_read(adr
+ FIFO_BUFSIZE_MASK
);
512 fifo_dsc
[f
].idx_in
= z80_read(adr
+ FIFO_INDEX_IN
);
513 fifo_dsc
[f
].idx_out
= z80_read(adr
+ FIFO_INDEX_OUT
);
515 z80_bus_cmd(Release
);
519 int z80_memfifo_is_empty(const fifo_t f
)
523 if (fifo_dsc
[f
].base
!= 0) {
525 uint32_t adr
= fifo_dsc
[f
].base
+ FIFO_INDEX_IN
;
528 z80_bus_cmd(Request
);
530 z80_bus_cmd(Release
);
531 rc
= idx
== fifo_dsc
[f
].idx_out
;
537 int z80_memfifo_is_full(const fifo_t f
)
541 if (fifo_dsc
[f
].base
!= 0) {
542 z80_bus_cmd(Request
);
543 rc
= ((fifo_dsc
[f
].idx_in
+ 1) & fifo_dsc
[f
].mask
)
544 == z80_read(fifo_dsc
[f
].base
+FIFO_INDEX_OUT
);
545 z80_bus_cmd(Release
);
550 uint8_t z80_memfifo_getc(const fifo_t f
)
554 while (z80_memfifo_is_empty(f
))
557 z80_bus_cmd(Request
);
558 idx
= fifo_dsc
[f
].idx_out
;
559 rc
= z80_read(fifo_dsc
[f
].base
+idx
);
560 fifo_dsc
[f
].idx_out
= ++idx
& fifo_dsc
[f
].mask
;
561 z80_write(fifo_dsc
[f
].base
+FIFO_INDEX_OUT
, fifo_dsc
[f
].idx_out
);
562 z80_bus_cmd(Release
);
568 void z80_memfifo_putc(fifo_t f
, uint8_t val
)
572 while (z80_memfifo_is_full(f
))
575 z80_bus_cmd(Request
);
576 idx
= fifo_dsc
[f
].idx_in
;
577 z80_write(fifo_dsc
[f
].base
+idx
, val
);
578 fifo_dsc
[f
].idx_in
= ++idx
& fifo_dsc
[f
].mask
;
579 z80_write(fifo_dsc
[f
].base
+FIFO_INDEX_IN
, fifo_dsc
[f
].idx_in
);
580 z80_bus_cmd(Release
);
583 /*--------------------------------------------------------------------------*/
585 TODO: Rewrite msg_fifo routines for AVR
590 //uint8_t idx_out, idx_in;
595 /*--------------------------------------------------------------------------*/
599 static void tim1_setup(void)
601 RCC_APB2RSTR
|= RCC_APB2RSTR_TIM1RST
;
602 RCC_APB2RSTR
&= ~RCC_APB2RSTR_TIM1RST
;
608 /* | TIM_SMCR_ETF_CK_INT_N_2 */
613 TIM1_DIER
= TIM_DIER_TDE
;
617 | TIM_CCMR1_OC1M_FORCE_LOW
618 | TIM_CCMR1_CC1S_OUT
;
620 TIM1_SMCR
|= TIM_SMCR_SMS_TM
;
625 /*--------------------------------------------------------------------------*/
627 void z80_setup_msg_fifo(void)
629 // gpio_set_mode(P_BUSACK, GPIO_MODE_INPUT,
630 // GPIO_CNF_INPUT_FLOAT, GPIO_BUSACK | GPIO_IOCS1);
634 // msg_fifo.count = NELEMS(msg_fifo.buf);
641 void z80_init_msg_fifo(uint32_t addr
)
644 DBG_P(1, "z80_init_msg_fifo: %lx\n", addr
);
646 z80_bus_cmd(Request
);
647 z80_write(addr
+FIFO_INDEX_OUT
, z80_read(addr
+FIFO_INDEX_IN
));
648 z80_bus_cmd(Release
);
649 msg_fifo
.base
= addr
;
653 int z80_msg_fifo_getc(void)
658 if (msg_fifo
.count
!= (NELEMS(msg_fifo
.buf
) /*- DMA1_CNDTR4 */ )) {
659 c
= msg_fifo
.buf
[msg_fifo
.count
];
660 if (++msg_fifo
.count
== NELEMS(msg_fifo
.buf
))
663 if (msg_fifo
.base
!= 0) {
664 z80_bus_cmd(Request
);
665 z80_write(msg_fifo
.base
+FIFO_INDEX_OUT
, msg_fifo
.count
);
666 z80_bus_cmd(Release
);