]> cloudbase.mooo.com Git - z180-stamp.git/blobdiff - avr/z80-if.c
Bugfix z80_addrbus_set_tristate()
[z180-stamp.git] / avr / z80-if.c
index 9492c28d6fd14a1eab7c697656e54dd3b23ab3d0..c49d2146c9e63b058ccb5a2d63ec7b4e4ba4cfd2 100644 (file)
@@ -1,83 +1,70 @@
+/*
+ * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
+ *
+ * SPDX-License-Identifier:    GPL-2.0+
+ */
+
 /**
  *
  * 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 <avr/io.h>
+
+#include "common.h"
 #include <util/atomic.h>
-#include <stdio.h>
 #include "debug.h"
 #include "z80-if.h"
 
 
-/* Number of array elements */
-#define NELEMS(x)  (sizeof x/sizeof *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__));
-
-typedef struct bits pbit_t;
-
-#define SBIT(port,pin) ((*(volatile struct bits*)&port).b##pin)
-
 
 //#define P_ZCLK               PORTB
 //#define ZCLK         5
@@ -136,6 +123,9 @@ typedef struct bits pbit_t;
 //#define Z80_I_HALT   SBIT(P_HALT, )
 
 
+#define BUS_TO 20
+
+
 #define MASK(n)        ((1<<(n))-1)
 #define SMASK(w,s) (MASK(w) << (s))
 
@@ -148,6 +138,20 @@ typedef union {
 
 
 static zstate_t zstate;
+static volatile uint8_t timer;         /* used for bus timeout */
+
+/*---------------------------------------------------------*/
+/* 10Hz timer interrupt generated by OC4A                  */
+/*---------------------------------------------------------*/
+
+ISR(TIMER4_COMPA_vect)
+{
+
+       uint8_t i = timer;
+
+       if (i)
+               timer = i - 1;
+}
 
 /*--------------------------------------------------------------------------*/
 
@@ -164,7 +168,7 @@ static void z80_addrbus_set_tristate(void)
        DDR_ADL = 0;
        P_ADH = 0;
        DDR_ADH = 0;
-       PIN_ADB = P_ADB & ~(MASK(ADB_WIDTH) << ADB_SHIFT);
+       PIN_ADB = P_ADB & (MASK(ADB_WIDTH) << ADB_SHIFT);
        DDR_ADB = DDR_ADB & ~(MASK(ADB_WIDTH) << ADB_SHIFT);
 }
 
@@ -226,6 +230,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<<WGM42)|(0b101<<CS30); /* CTC Mode, Prescaler 1024 */
+       TIMSK4 = _BV(OCIE4A);                             /* Enable oca interrupt */
+
 }
 
 
@@ -247,40 +258,42 @@ static void z80_busreq_hpulse(void)
        }
 
        if (zstate & ZST_ACQUIRED) {
-               while(Z80_I_BUSACK == 1)
+               timer = BUS_TO;
+               while (Z80_I_BUSACK == 1 && timer)
                        ;
-               z80_addrbus_set_active();
+               if (Z80_I_BUSACK == 0)
+                       z80_addrbus_set_active();
        }
 }
 
 
 /*
 
- +              |              |               |               |               |
+ +              |               |               |               |               |
     +   State   |     RESET     |  RESET_AQRD   |    RUNNING    | RUNNING_AQRD  |
-       +        |              |               |               |               |
-          +     |      0       |       1       |       2       |       3       |
-Event        +  |              |               |               |               |
+       +        |               |               |               |               |
+          +     |       0       |       1       |       2       |       3       |
+Event        +  |               |               |               |               |
 ----------------+---------------+---------------+---------------+---------------+
-               |               |               |               |               |
-Reset          |       0       |       0       |       0       |       0       |
-               |               |               |               |               |
-               |               |               |               |               |
-Request                |       1       |               |       3       |               |
-               |               |               |               |               |
-               |               |               |               |               |
-Release                |               |       0       |               |       2       |
-               |               |               |               |               |
-               |               |               |               |               |
-Run            |       2       |       3       |               |               |
-               |               |               |               |               |
-               |               |               |               |               |
-Restart                |               |               |       2       |       3       |
-               |               |               |               |               |
-               |               |               |               |               |
-M_Cycle                |               |               |               |       3       |
-               |               |               |               |               |
-               |               |               |               |               |
+                |               |               |               |               |
+Reset           |       0       |       0       |       0       |       0       |
+                |               |               |               |               |
+                |               |               |               |               |
+Request         |       1       |               |       3       |               |
+                |               |               |               |               |
+                |               |               |               |               |
+Release         |               |       0       |               |       2       |
+                |               |               |               |               |
+                |               |               |               |               |
+Run             |       2       |       3       |               |               |
+                |               |               |               |               |
+                |               |               |               |               |
+Restart         |               |               |       2       |       3       |
+                |               |               |               |               |
+                |               |               |               |               |
+M_Cycle         |               |               |               |       3       |
+                |               |               |               |               |
+                |               |               |               |               |
 */
 
 zstate_t z80_bus_cmd(bus_cmd_t cmd)
@@ -300,18 +313,29 @@ zstate_t z80_bus_cmd(bus_cmd_t cmd)
                case RESET:
                        Z80_O_BUSREQ = 0;
                        Z80_O_RST = 1;
-                       while(Z80_I_BUSACK == 1)
+                       timer = BUS_TO;
+                       while (Z80_I_BUSACK == 1 && timer)
                                ;
-                       z80_addrbus_set_active();
-                       zstate = RESET_AQRD;
+                       if (Z80_I_BUSACK == 0) {
+                               z80_addrbus_set_active();
+                               zstate = RESET_AQRD;
+                       } else {
+                               Z80_O_RST = 0;
+                               Z80_O_BUSREQ = 1;
+                       }
                        break;
 
                case RUNNING:
                        Z80_O_BUSREQ = 0;
-                       while(Z80_I_BUSACK == 1)
+                       timer = BUS_TO;
+                       while (Z80_I_BUSACK == 1 && timer)
                                ;
-                       z80_addrbus_set_active();
-                       zstate = RUNNING_AQRD;
+                       if (Z80_I_BUSACK == 0) {
+                               z80_addrbus_set_active();
+                               zstate = RUNNING_AQRD;
+                       } else {
+                               Z80_O_BUSREQ = 1;
+                       }
                        break;
 
                default:
@@ -372,7 +396,7 @@ zstate_t z80_bus_cmd(bus_cmd_t cmd)
        case M_Cycle:
                switch (zstate) {
                case RUNNING_AQRD:
-                       z80_busreq_hpulse();
+                       z80_busreq_hpulse();    /* TODO: */
                        break;
                default:
                        break;
@@ -430,8 +454,25 @@ 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;
@@ -441,7 +482,7 @@ void z80_memset(uint32_t addr, uint8_t data, uint32_t length)
        Z80_O_MREQ = 1;
 }
 
-void z80_write_block(const __flash uint8_t *src, uint32_t dest, uint32_t length)
+void z80_write_block(const uint8_t *src, uint32_t dest, uint32_t length)
 {
        uint8_t data;
 
@@ -459,6 +500,25 @@ void z80_write_block(const __flash uint8_t *src, uint32_t dest, uint32_t length)
        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               ;
@@ -500,10 +560,9 @@ void z80_memfifo_init(const fifo_t f, uint32_t addr)
 {
        fifo_dsc[f].base = addr;
 
-       if (addr != 0) {
-
 DBG_P(2, "z80_memfifo_init: %i, %lx\n", f, 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);
@@ -533,7 +592,7 @@ int z80_memfifo_is_empty(const fifo_t f)
 
 int z80_memfifo_is_full(const fifo_t f)
 {
-       int rc = 1;
+       int rc = 0;
 
        if (fifo_dsc[f].base != 0) {
                z80_bus_cmd(Request);