X-Git-Url: http://cloudbase.mooo.com/gitweb/z180-stamp.git/blobdiff_plain/349c01b10d1f6e223f963c6cbdf6a94d0b618895..d332e3337306d5f8b5c810048bbd145391c33f29:/avr/z80-if.c diff --git a/avr/z80-if.c b/avr/z80-if.c index 25a71b0..cc0bab1 100644 --- a/avr/z80-if.c +++ b/avr/z80-if.c @@ -2,91 +2,68 @@ * * Pin assignments * - * | Z180-Sig | AVR-Port | Dir | Special Function | + * | Z180-Sig | AVR-Port | Dir | Special Function | * +------------+---------------+-------+-----------------------+ - * | 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 | | - * | IOCS1 | PE 5 | I | | - * |* HALT | P | | | - * |* NMI | P | | | - * | | P | | | - * | | P | | af1 USART1_TX | - * | | P | | af1 USART1_RX | - * | | P |JTDI | remap SPI1_NSS' | - * | | P |JTDO | remap SPI1_SCK' | - * | | P |JTRST | remap SPI1_MISO' | - * | | P | | remap SPI1_MOSI' | - * | | P | | af1 OSC32 | - * | | P | | af1 OSC32 | + * | 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 | | + * | IOCS1 | PE 5 | I | | + * |* HALT | P | | | + * |* NMI | P | | | + * | | P | | | + * | | P | | af1 USART1_TX | + * | | P | | af1 USART1_RX | + * | | P |JTDI | remap SPI1_NSS' | + * | | P |JTDO | remap SPI1_SCK' | + * | | P |JTRST | remap SPI1_MISO' | + * | | P | | remap SPI1_MOSI' | + * | | P | | af1 OSC32 | + * | | P | | af1 OSC32 | */ -#include -#include + +#include "common.h" #include -#include #include "debug.h" #include "z80-if.h" -/* Number of array elements */ -#define NELEMS(x) (sizeof x/sizeof *x) - - -#define CONCAT(x,y) x ## y -#define EVALUATOR(x,y) CONCAT(x,y) - -#define GPIO_(X) CONCAT(GPIO, X) - -struct bits { - uint8_t b0:1; - uint8_t b1:1; - uint8_t b2:1; - uint8_t b3:1; - uint8_t b4:1; - uint8_t b5:1; - uint8_t b6:1; - uint8_t b7:1; -} __attribute__((__packed__)); -#define SBIT(port,pin) ((*(volatile struct bits*)&port).b##pin) - - -#define P_ZCLK PORTB -#define ZCLK 7 -#define DDR_ZCLK DDRB +//#define P_ZCLK PORTB +//#define ZCLK 5 +//#define DDR_ZCLK DDRB #define P_MREQ PORTD #define MREQ 4 #define DDR_MREQ DDRD @@ -130,7 +107,7 @@ struct bits { //#define ADB_PORT PORTE -#define Z80_O_ZCLK SBIT(P_ZCLK, 7) +//#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) @@ -141,11 +118,12 @@ struct bits { //#define Z80_I_HALT SBIT(P_HALT, ) +#define BUS_TO 20 + + #define MASK(n) ((1<<(n))-1) #define SMASK(w,s) (MASK(w) << (s)) -#define LOWSPEED 50000 - typedef union { uint32_t l; @@ -155,91 +133,22 @@ typedef union { static zstate_t zstate; +static volatile uint8_t timer; /* used for bus timeout */ -/*--------------------------------------------------------------------------*/ - -static -uint8_t is_lowspeed() -{ - return (TCCR1B & 7) < 2 && - OCR1A > (F_CPU / 2 / LOWSPEED); -} - -static -void z80_setup_clock(void) -{ - /* ZCLK: Output and low */ - DDR_ZCLK |= _BV(ZCLK); - Z80_O_ZCLK = 0; - - DDRB |= _BV(6); /* Debug */ - PORTB |= _BV(6); /* Debug */ - - PRR0 &= ~_BV(PRTIM1); - - /* Timer1: CTC: Toggle OC1C on compare match */ - OCR1A = 0; - OCR1C = 0; - TCCR1A = (0b01 << COM1C0) | (0b00 << WGM10); - TCCR1B = (0b01 << WGM12) | (0b001 << CS10); -} +/*---------------------------------------------------------*/ +/* 10Hz timer interrupt generated by OC4A */ +/*---------------------------------------------------------*/ - -int z80_clock_set(unsigned long freq) +ISR(TIMER4_COMPA_vect) { - unsigned long ocrval = F_CPU / freq / 2; - uint8_t prescale = 0; - - while (ocrval > (1L<<16)) { - prescale++; - if (prescale < 3) - ocrval = ocrval / 8; - else - ocrval = ocrval / 4; - } - - if ((ocrval == 0) || (prescale > 4)) - return -1; - - ocrval -= 1; - - PINB |= _BV(6); /* Debug */ - - /* Stop Timer */ - TCCR1B = (0b01 << WGM12) | (0b000 << CS10); - TCNT1 = 0; - - OCR1A = ocrval; - OCR1CL = ocrval; - TCCR1A = (0b01 << COM1C0) | (0b00 << WGM10); - TCCR1B = (0b01 << WGM12) | ((prescale+1) << CS10); - - if (ocrval == 0) - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - TCNT1 = 0xFFFF; - } - - PINB |= _BV(6); /* Debug */ - - return 0; -} -uint32_t z80_clock_get(void) -{ - uint32_t count = (OCR1A + 1L) * 2; - uint8_t pre = (TCCR1B & 7) - 1; - - while (pre) { - if (pre > 2) - count *= 4; - else - count *= 8; - pre--; - } + uint8_t i = timer; - return F_CPU/count; + if (i) + timer = i - 1; } +/*--------------------------------------------------------------------------*/ static void z80_addrbus_set_tristate(void) @@ -296,8 +205,6 @@ static void z80_reset_pulse(void) void z80_setup_bus(void) { - z80_setup_clock(); - /* /ZRESET: Output and low */ Z80_O_RST = 0; DDR_RST |= _BV(RST); @@ -318,6 +225,13 @@ void z80_setup_bus(void) z80_dbus_set_in(); zstate = RESET; + + /* Timer 4 */ + PRR1 &= ~_BV(PRTIM4); + OCR4A = F_CPU / 1024 / 10 - 1; /* Timer: 10Hz interval (OC4A) */ + TCCR4B = (0b01<