]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/z80-if.c
2 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
4 * SPDX-License-Identifier: GPL-2.0
11 * | Z180-Sig | AVR-Port | Dir |
12 * +------------+---------------+-------+
44 * | BUSREQ | PD 7 | O |
45 * | BUSACK | PD 6 | I |
48 * +------------------------------------+
57 #include <util/atomic.h>
63 //#define P_ZCLK PORTB
65 //#define DDR_ZCLK DDRB
73 #define P_BUSREQ PORTD
75 #define DDR_BUSREQ DDRD
76 #define P_BUSACK PORTD
77 #define PIN_BUSACK PIND
79 #define DDR_BUSACK DDRD
100 //#define ADB_PORT PORTE
103 //#define Z80_O_ZCLK SBIT(P_ZCLK, 5)
104 #define Z80_O_MREQ SBIT(P_MREQ, 4)
105 #define Z80_O_RD SBIT(P_RD, 3)
106 #define Z80_O_WR SBIT(P_WR, 2)
107 #define Z80_O_BUSREQ SBIT(P_BUSREQ, 7)
108 //#define Z80_O_NMI SBIT(P_NMI, )
109 #define Z80_O_RST SBIT(P_RST, 5)
110 #define Z80_I_RST SBIT(PIN_RST, 5)
111 #define Z80_I_BUSACK SBIT(PIN_BUSACK, 6)
112 //#define Z80_I_HALT SBIT(P_HALT, )
120 #define DDR_STEP DDRG
123 #define DDR_WAIT DDRG
124 /* All three signals are on the same Port (PortG) */
125 #define PORT_SS PORTG
128 #define Z80_O_RUN SBIT(PORT_SS, RUN)
129 #define Z80_O_STEP SBIT(PORT_SS, STEP)
130 #define Z80_I_WAIT SBIT(PORT_SS, WAIT)
136 #define MASK(n) ((1<<(n))-1)
137 #define SMASK(w,s) (MASK(w) << (s))
139 void z80_bus_request_or_exit(void)
141 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
))
142 cmd_error(CMD_RET_FAILURE
, EBUSTO
, NULL
);
145 static zstate_t zstate
;
146 static volatile uint8_t timer
; /* used for bus timeout */
147 static bool reset_polarity
;
149 /*---------------------------------------------------------*/
150 /* 10Hz timer interrupt generated by OC4A */
151 /*---------------------------------------------------------*/
153 ISR(TIMER5_COMPA_vect
)
162 /*--------------------------------------------------------------------------*/
165 static void z80_addrbus_set_in(void)
167 /* /MREQ, /RD, /WR: Input, no pullup */
168 DDR_MREQ
&= ~(_BV(MREQ
) | _BV(RD
) | _BV(WR
));
177 PIN_ADB
= P_ADB
& (MASK(ADB_WIDTH
) << ADB_SHIFT
);
178 DDR_ADB
= DDR_ADB
& ~(MASK(ADB_WIDTH
) << ADB_SHIFT
);
182 static void z80_addrbus_set_out(void)
184 /* /MREQ, /RD, /WR: Output and high */
188 DDR_MREQ
|= _BV(MREQ
) | _BV(RD
) | _BV(WR
);
192 DDR_ADB
= DDR_ADB
| (MASK(ADB_WIDTH
) << ADB_SHIFT
);
196 static void z80_dbus_set_in(void)
203 static void z80_dbus_set_out(void)
208 static void z80_reset_active(void)
216 static void z80_reset_inactive(void)
224 static void z80_reset_pulse(void)
228 z80_reset_inactive();
232 void z80_setup_bus(void)
234 ATOMIC_BLOCK(ATOMIC_RESTORESTATE
) {
236 /* /ZRESET: Input, no pullup */
237 DDR_RST
&= ~_BV(RST
);
240 /* /BUSREQ: Output and high */
242 DDR_BUSREQ
|= _BV(BUSREQ
);
244 /* /BUSACK: Input, no pullup */
245 DDR_BUSACK
&= ~_BV(BUSACK
);
246 P_BUSACK
&= ~_BV(BUSACK
);
248 z80_addrbus_set_in();
251 if (getenv_yesno(PSTR(ENV_SINGLESTEP
))) {
252 /* /RUN & /STEP: output, /WAIT: input */
254 PORT_SS
= (PORT_SS
& ~_BV(RUN
)) | _BV(STEP
);
255 DDR_SS
= (DDR_SS
& ~_BV(WAIT
)) | _BV(RUN
) | _BV(STEP
);
258 reset_polarity
= Z80_I_RST
;
266 PRR1
&= ~_BV(PRTIM5
);
267 OCR5A
= F_CPU
/ 1024 / 10 - 1; /* Timer: 10Hz interval (OC4A) */
268 TCCR5B
= (0b01<<WGM52
)|(0b101<<CS40
); /* CTC Mode, Prescaler 1024 */
269 TIMSK5
= _BV(OCIE5A
); /* Enable oca interrupt */
274 zstate_t
z80_bus_state(void)
280 static void z80_busreq_hpulse(void)
283 z80_addrbus_set_in();
286 ATOMIC_BLOCK(ATOMIC_FORCEON
) {
288 Z80_O_BUSREQ
= 1; /* 2 AVR clock cycles */
289 Z80_O_BUSREQ
= 0; /* 2 AVR clock cycles */
294 ATOMIC_BLOCK(ATOMIC_FORCEON
) {
298 if (Z80_I_BUSACK
== 1) {
306 if (zstate
& ZST_ACQUIRED
) {
308 while (Z80_I_BUSACK
== 1 && timer
)
310 if (Z80_I_BUSACK
== 0)
311 z80_addrbus_set_out();
319 + State | RESET | RESET_AQRD | RUNNING | RUNNING_AQRD |
323 ----------------+---------------+---------------+---------------+---------------+
325 Reset | 0 | 0 | 0 | 0 |
328 Request | 1 | | 3 | |
331 Release | | 0 | | 2 |
337 Restart | | | 2 | 3 |
345 zstate_t
z80_bus_cmd(bus_cmd_t cmd
)
351 z80_addrbus_set_in();
361 z80_reset_inactive();
363 while (Z80_I_BUSACK
== 1 && timer
)
365 if (Z80_I_BUSACK
== 0) {
366 z80_addrbus_set_out();
377 while (Z80_I_BUSACK
== 1 && timer
)
379 if (Z80_I_BUSACK
== 0) {
380 z80_addrbus_set_out();
381 zstate
= RUNNING_AQRD
;
396 z80_addrbus_set_in();
403 z80_addrbus_set_in();
415 z80_reset_inactive();
421 z80_addrbus_set_in();
423 z80_addrbus_set_out();
424 zstate
= RUNNING_AQRD
;
445 z80_busreq_hpulse(); /* TODO: */
455 /*--------------------------------------------------------------------------*/
458 //inline __attribute__ ((always_inline))
459 void z80_setaddress(uint32_t addr
)
462 P_ADH
= (addr
& 0xff00) >> 8;
463 PIN_ADB
= (((addr
>> 16) << ADB_SHIFT
) ^ P_ADB
) & MASK(ADB_WIDTH
) << ADB_SHIFT
;
466 void z80_write(uint32_t addr
, uint8_t data
)
468 z80_setaddress(addr
);
479 uint8_t z80_read(uint32_t addr
)
483 z80_setaddress(addr
);
497 void z80_memset(uint32_t addr
, uint8_t data
, uint32_t length
)
503 z80_setaddress(addr
++);
511 void z80_write_block_P(const FLASH
uint8_t *src
, uint32_t dest
, uint32_t length
)
518 z80_setaddress(dest
++);
529 void z80_write_block(const uint8_t *src
, uint32_t dest
, uint32_t length
)
536 z80_setaddress(dest
++);
547 void z80_read_block (uint8_t *dest
, uint32_t src
, size_t length
)
554 z80_setaddress(src
++);
567 0179' rx.bs_mask: ds 1 ; (buf_len - 1)
568 017A' rx.in_idx: ds 1 ;
569 017B' rx.out_idx: ds 1 ;
570 017C' rx.buf: ds rx.buf_len ;
571 018B' rx.buf_end equ $-1 ; last byte (start+len-1)
573 018C' tx.bs_mask: ds 1 ; (buf_len - 1)
574 018D' tx.in_idx: ds 1 ;
575 018E' tx.out_idx: ds 1 ;
576 018F' tx.buf: ds tx.buf_len ;
577 019E' tx.buf_end equ $-1 ; last byte
581 typedef struct __attribute__((packed
)) {
590 #define FIFO_BUFSIZE_MASK -3
591 #define FIFO_INDEX_IN -2
592 #define FIFO_INDEX_OUT -1
600 } fifo_dsc
[NUM_FIFOS
];
603 void z80_memfifo_init(const fifo_t f
, uint32_t addr
)
605 fifo_dsc
[f
].base
= addr
;
609 z80_bus_cmd(Request
);
610 fifo_dsc
[f
].mask
= z80_read(addr
+ FIFO_BUFSIZE_MASK
);
611 fifo_dsc
[f
].idx_in
= z80_read(addr
+ FIFO_INDEX_IN
);
612 fifo_dsc
[f
].idx_out
= z80_read(addr
+ FIFO_INDEX_OUT
);
613 z80_bus_cmd(Release
);
615 if (fifo_dsc
[f
].idx_in
!= 0 || fifo_dsc
[f
].idx_out
!= 0) {
616 DBG_P(1, "## z80_memfifo_init: %i, %lx, in: %.2x, out: %.2x, mask: %.2x\n",
617 f
, addr
, fifo_dsc
[f
].idx_in
, fifo_dsc
[f
].idx_out
, fifo_dsc
[f
].mask
);
623 int z80_memfifo_is_empty(const fifo_t f
)
627 if (fifo_dsc
[f
].base
!= 0) {
629 uint32_t adr
= fifo_dsc
[f
].base
+ FIFO_INDEX_IN
;
632 z80_bus_cmd(Request
);
634 z80_bus_cmd(Release
);
635 rc
= idx
== fifo_dsc
[f
].idx_out
;
641 int z80_memfifo_is_full(const fifo_t f
)
645 if (fifo_dsc
[f
].base
!= 0) {
646 z80_bus_cmd(Request
);
647 rc
= ((fifo_dsc
[f
].idx_in
+ 1) & fifo_dsc
[f
].mask
)
648 == z80_read(fifo_dsc
[f
].base
+FIFO_INDEX_OUT
);
649 z80_bus_cmd(Release
);
655 uint8_t z80_memfifo_getc_wait(const fifo_t f
)
659 while (z80_memfifo_is_empty(f
))
662 z80_bus_cmd(Request
);
663 idx
= fifo_dsc
[f
].idx_out
;
664 rc
= z80_read(fifo_dsc
[f
].base
+idx
);
665 fifo_dsc
[f
].idx_out
= ++idx
& fifo_dsc
[f
].mask
;
666 z80_write(fifo_dsc
[f
].base
+FIFO_INDEX_OUT
, fifo_dsc
[f
].idx_out
);
667 z80_bus_cmd(Release
);
672 int z80_memfifo_getc(const fifo_t f
)
676 if (fifo_dsc
[f
].base
!= 0) {
677 uint8_t idx
= fifo_dsc
[f
].idx_out
;
678 z80_bus_cmd(Request
);
679 if (idx
!= z80_read(fifo_dsc
[f
].base
+ FIFO_INDEX_IN
)) {
680 rc
= z80_read(fifo_dsc
[f
].base
+idx
);
681 fifo_dsc
[f
].idx_out
= ++idx
& fifo_dsc
[f
].mask
;
682 z80_write(fifo_dsc
[f
].base
+FIFO_INDEX_OUT
, fifo_dsc
[f
].idx_out
);
684 z80_bus_cmd(Release
);
691 void z80_memfifo_putc(fifo_t f
, uint8_t val
)
695 while (z80_memfifo_is_full(f
))
698 z80_bus_cmd(Request
);
699 idx
= fifo_dsc
[f
].idx_in
;
700 z80_write(fifo_dsc
[f
].base
+idx
, val
);
701 fifo_dsc
[f
].idx_in
= ++idx
& fifo_dsc
[f
].mask
;
702 z80_write(fifo_dsc
[f
].base
+FIFO_INDEX_IN
, fifo_dsc
[f
].idx_in
);
703 z80_bus_cmd(Release
);