/* * (C) Copyright 2014 Leo C. * * SPDX-License-Identifier: GPL-2.0 */ /** * * Pin assignments * * | Z180-Sig | AVR-Port | Dir | * +------------+---------------+-------+ * | A0 | PA 0 | O | * | A1 | PA 1 | O | * | A2 | PA 2 | O | * | A3 | PA 3 | O | * | A4 | PA 4 | O | * | A5 | PA 5 | O | * | A6 | PA 6 | O | * | A7 | PA 7 | O | * | A8 | PC 0 | O | * | A9 | PC 1 | O | * | A10 | PC 2 | O | * | A11 | PC 3 | O | * | A12 | PC 4 | O | * | A13 | PC 5 | O | * | A14 | PC 6 | O | * | A15 | PC 7 | O | * | A16 | PE 2 | O | * | A17 | PE 3 | O | * | A18 | PE 4 | O | * | D0 | PF 0 | I/O | * | D1 | PF 1 | I/O | * | D2 | PF 2 | I/O | * | D3 | PF 3 | I/O | * | D4 | PF 4 | I/O | * | D5 | PF 5 | I/O | * | D6 | PF 6 | I/O | * | D7 | PF 7 | I/O | * | RD | PD 3 | O | * | WR | PD 2 | O | * | MREQ | PD 4 | O | * | RST | PD 5 | O | * | BUSREQ | PD 7 | O | * | BUSACK | PD 6 | I | * | * | Optional * +------------------------------------+ * | STEP | PG 0 | O | * | RUN | PG 1 | O | * | WAIT | PG 2 | I | */ #include "z80-if.h" #include #include "debug.h" #include "config.h" #include "env.h" //#define P_ZCLK PORTB //#define ZCLK 5 //#define DDR_ZCLK DDRB #define P_MREQ PORTD #define MREQ 4 #define DDR_MREQ DDRD #define P_RD PORTD #define RD 3 #define P_WR PORTD #define WR 2 #define P_BUSREQ PORTD #define BUSREQ 7 #define DDR_BUSREQ DDRD #define P_BUSACK PORTD #define PIN_BUSACK PIND #define BUSACK 6 #define DDR_BUSACK DDRD #define P_RST PORTD #define PIN_RST PIND #define DDR_RST DDRD #define RST 5 #define P_DB PORTF #define PIN_DB PINF #define DDR_DB DDRF #define P_ADL PORTA #define P_ADH PORTC #define P_ADB PORTE #define PIN_ADB PINE #define DDR_ADL DDRA #define DDR_ADH DDRC #define DDR_ADB DDRE #define ADB_WIDTH 3 #define ADB_SHIFT 2 //#define ADB_PORT PORTE //#define Z80_O_ZCLK SBIT(P_ZCLK, 5) #define Z80_O_MREQ SBIT(P_MREQ, 4) #define Z80_O_RD SBIT(P_RD, 3) #define Z80_O_WR SBIT(P_WR, 2) #define Z80_O_BUSREQ SBIT(P_BUSREQ, 7) //#define Z80_O_NMI SBIT(P_NMI, ) #define Z80_O_RST SBIT(P_RST, 5) #define Z80_I_RST SBIT(PIN_RST, 5) #define Z80_I_BUSACK SBIT(PIN_BUSACK, 6) //#define Z80_I_HALT SBIT(P_HALT, ) /* Optional */ #define P_RUN PORTG #define RUN 1 #define DDR_RUN DDRG #define P_STEP PORTG #define STEP 0 #define DDR_STEP DDRG #define P_WAIT PORTG #define WAIT 2 #define DDR_WAIT DDRG /* All three signals are on the same Port (PortG) */ #define PORT_SS PORTG #define DDR_SS DDRG #define PIN_SS PING #define Z80_O_RUN SBIT(PORT_SS, RUN) #define Z80_O_STEP SBIT(PORT_SS, STEP) #define Z80_I_WAIT SBIT(PORT_SS, WAIT) #define BUS_TO 20 #define MASK(n) ((1<<(n))-1) #define SMASK(w,s) (MASK(w) << (s)) static zstate_t zstate; static volatile uint8_t timer; /* used for bus timeout */ static bool reset_polarity; /*---------------------------------------------------------*/ /* 10Hz timer interrupt generated by OC4A */ /*---------------------------------------------------------*/ ISR(TIMER5_COMPA_vect) { uint8_t i = timer; if (i) timer = i - 1; } /*--------------------------------------------------------------------------*/ static void z80_addrbus_set_in(void) { /* /MREQ, /RD, /WR: Input, no pullup */ DDR_MREQ &= ~(_BV(MREQ) | _BV(RD) | _BV(WR)); Z80_O_MREQ = 0; Z80_O_RD = 0; Z80_O_WR = 0; P_ADL = 0; DDR_ADL = 0; P_ADH = 0; DDR_ADH = 0; PIN_ADB = P_ADB & (MASK(ADB_WIDTH) << ADB_SHIFT); DDR_ADB = DDR_ADB & ~(MASK(ADB_WIDTH) << ADB_SHIFT); } static void z80_addrbus_set_out(void) { /* /MREQ, /RD, /WR: Output and high */ Z80_O_MREQ = 1; Z80_O_RD = 1; Z80_O_WR = 1; DDR_MREQ |= _BV(MREQ) | _BV(RD) | _BV(WR); DDR_ADL = 0xff; DDR_ADH = 0xff; DDR_ADB = DDR_ADB | (MASK(ADB_WIDTH) << ADB_SHIFT); } static void z80_dbus_set_in(void) { DDR_DB = 0; P_DB = 0; } static void z80_dbus_set_out(void) { DDR_DB = 0xff; } static void z80_reset_active(void) { if (reset_polarity) Z80_O_RST = 1; else Z80_O_RST = 0; } static void z80_reset_inactive(void) { if (reset_polarity) Z80_O_RST = 0; else Z80_O_RST = 1; } static void z80_reset_pulse(void) { z80_reset_active(); _delay_us(10); z80_reset_inactive(); } void z80_setup_bus(void) { ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { /* /ZRESET: Input, no pullup */ DDR_RST &= ~_BV(RST); Z80_O_RST = 0; /* /BUSREQ: Output and high */ Z80_O_BUSREQ = 1; DDR_BUSREQ |= _BV(BUSREQ); /* /BUSACK: Input, no pullup */ DDR_BUSACK &= ~_BV(BUSACK); P_BUSACK &= ~_BV(BUSACK); z80_addrbus_set_in(); z80_dbus_set_in(); if (getenv_yesno(PSTR(ENV_SINGLESTEP))) { /* /RUN & /STEP: output, /WAIT: input */ PORT_SS = (PORT_SS & ~_BV(RUN)) | _BV(STEP); DDR_SS = (DDR_SS & ~_BV(WAIT)) | _BV(RUN) | _BV(STEP); } reset_polarity = Z80_I_RST; z80_reset_active(); DDR_RST |= _BV(RST); zstate = RESET; } /* Timer 5 */ PRR1 &= ~_BV(PRTIM5); OCR5A = F_CPU / 1024 / 10 - 1; /* Timer: 10Hz interval (OC4A) */ TCCR5B = (0b01<> 8; PIN_ADB = (((addr >> 16) << ADB_SHIFT) ^ P_ADB) & MASK(ADB_WIDTH) << ADB_SHIFT; } void z80_write(uint32_t addr, uint8_t data) { z80_setaddress(addr); Z80_O_MREQ = 0; z80_dbus_set_out(); P_DB = data; P_DB = data; Z80_O_WR = 0; Z80_O_WR = 0; Z80_O_WR = 1; Z80_O_MREQ = 1; } uint8_t z80_read(uint32_t addr) { uint8_t data; z80_setaddress(addr); Z80_O_MREQ = 0; z80_dbus_set_in(); Z80_O_RD = 0; Z80_O_RD = 0; Z80_O_RD = 0; data = PIN_DB; Z80_O_RD = 1; Z80_O_MREQ = 1; return data; } void z80_memset(uint32_t addr, uint8_t data, uint32_t length) { z80_dbus_set_out(); Z80_O_MREQ = 0; P_DB = data; while(length--) { z80_setaddress(addr++); Z80_O_WR = 0; Z80_O_WR = 0; Z80_O_WR = 1; } Z80_O_MREQ = 1; } void z80_write_block_P(const FLASH uint8_t *src, uint32_t dest, uint32_t length) { uint8_t data; z80_dbus_set_out(); Z80_O_MREQ = 0; while(length--) { z80_setaddress(dest++); data = *src++; P_DB = data; P_DB = data; Z80_O_WR = 0; Z80_O_WR = 0; Z80_O_WR = 1; } Z80_O_MREQ = 1; } void z80_write_block(const uint8_t *src, uint32_t dest, uint32_t length) { uint8_t data; z80_dbus_set_out(); Z80_O_MREQ = 0; while(length--) { z80_setaddress(dest++); data = *src++; P_DB = data; P_DB = data; Z80_O_WR = 0; Z80_O_WR = 0; Z80_O_WR = 1; } Z80_O_MREQ = 1; } void z80_read_block (uint8_t *dest, uint32_t src, size_t length) { uint8_t data; Z80_O_MREQ = 0; z80_dbus_set_in(); while(length--) { z80_setaddress(src++); Z80_O_RD = 0; Z80_O_RD = 0; Z80_O_RD = 0; data = PIN_DB; Z80_O_RD = 1; *dest++ = data; } Z80_O_MREQ = 1; } /* 0179' rx.bs_mask: ds 1 ; (buf_len - 1) 017A' rx.in_idx: ds 1 ; 017B' rx.out_idx: ds 1 ; 017C' rx.buf: ds rx.buf_len ; 018B' rx.buf_end equ $-1 ; last byte (start+len-1) 018C' tx.bs_mask: ds 1 ; (buf_len - 1) 018D' tx.in_idx: ds 1 ; 018E' tx.out_idx: ds 1 ; 018F' tx.buf: ds tx.buf_len ; 019E' tx.buf_end equ $-1 ; last byte */ typedef struct __attribute__((packed)) { uint8_t mask; uint8_t in_idx; uint8_t out_idx; uint8_t buf[]; } zfifo_t; #define FIFO_BUFSIZE_MASK -3 #define FIFO_INDEX_IN -2 #define FIFO_INDEX_OUT -1 static struct { uint32_t base; uint8_t idx_out, idx_in, mask; } fifo_dsc[NUM_FIFOS]; void z80_memfifo_init(const fifo_t f, uint32_t addr) { fifo_dsc[f].base = addr; if (addr != 0) { z80_bus_cmd(Request); fifo_dsc[f].mask = z80_read(addr + FIFO_BUFSIZE_MASK); fifo_dsc[f].idx_in = z80_read(addr + FIFO_INDEX_IN); fifo_dsc[f].idx_out = z80_read(addr + FIFO_INDEX_OUT); z80_bus_cmd(Release); if (fifo_dsc[f].idx_in != 0 || fifo_dsc[f].idx_out != 0) { DBG_P(1, "## z80_memfifo_init: %i, %lx, in: %.2x, out: %.2x, mask: %.2x\n", f, addr, fifo_dsc[f].idx_in, fifo_dsc[f].idx_out, fifo_dsc[f].mask); } } } int z80_memfifo_is_empty(const fifo_t f) { int rc = 1; if (fifo_dsc[f].base != 0) { uint32_t adr = fifo_dsc[f].base + FIFO_INDEX_IN; uint8_t idx; z80_bus_cmd(Request); idx = z80_read(adr); z80_bus_cmd(Release); rc = idx == fifo_dsc[f].idx_out; } return rc; } int z80_memfifo_is_full(const fifo_t f) { int rc = 0; if (fifo_dsc[f].base != 0) { z80_bus_cmd(Request); rc = ((fifo_dsc[f].idx_in + 1) & fifo_dsc[f].mask) == z80_read(fifo_dsc[f].base+FIFO_INDEX_OUT); z80_bus_cmd(Release); } return rc; } uint8_t z80_memfifo_getc_wait(const fifo_t f) { uint8_t rc, idx; while (z80_memfifo_is_empty(f)) ; z80_bus_cmd(Request); idx = fifo_dsc[f].idx_out; rc = z80_read(fifo_dsc[f].base+idx); fifo_dsc[f].idx_out = ++idx & fifo_dsc[f].mask; z80_write(fifo_dsc[f].base+FIFO_INDEX_OUT, fifo_dsc[f].idx_out); z80_bus_cmd(Release); return rc; } int z80_memfifo_getc(const fifo_t f) { int rc = -1; if (fifo_dsc[f].base != 0) { uint8_t idx = fifo_dsc[f].idx_out; z80_bus_cmd(Request); if (idx != z80_read(fifo_dsc[f].base + FIFO_INDEX_IN)) { rc = z80_read(fifo_dsc[f].base+idx); fifo_dsc[f].idx_out = ++idx & fifo_dsc[f].mask; z80_write(fifo_dsc[f].base+FIFO_INDEX_OUT, fifo_dsc[f].idx_out); } z80_bus_cmd(Release); } return rc; } void z80_memfifo_putc(fifo_t f, uint8_t val) { int idx; while (z80_memfifo_is_full(f)) ; z80_bus_cmd(Request); idx = fifo_dsc[f].idx_in; z80_write(fifo_dsc[f].base+idx, val); fifo_dsc[f].idx_in = ++idx & fifo_dsc[f].mask; z80_write(fifo_dsc[f].base+FIFO_INDEX_IN, fifo_dsc[f].idx_in); z80_bus_cmd(Release); }