]> cloudbase.mooo.com Git - z180-stamp.git/blobdiff - avr/z80-if.c
move sys timer setup from main to timer.c
[z180-stamp.git] / avr / z80-if.c
index e36b3696a5b9a43e3a6e057f637142f803ba975c..7a953f06e9017b384615b69b0f6cb5fd5dda7c2e 100644 (file)
 #define MASK(n)        ((1<<(n))-1)
 #define SMASK(w,s) (MASK(w) << (s))
 
+void z80_bus_request_or_exit(void)
+{
+       if (!(z80_bus_cmd(Request) & ZST_ACQUIRED))
+               cmd_error(CMD_RET_FAILURE, EBUSTO, NULL);
+}
 
 static zstate_t zstate;
 static volatile uint8_t timer;         /* used for bus timeout */
 static bool reset_polarity;
 
 /*---------------------------------------------------------*/
-/* 10Hz timer interrupt generated by OC4A                  */
+/* 10Hz timer interrupt generated by OC5A                  */
 /*---------------------------------------------------------*/
 
 ISR(TIMER5_COMPA_vect)
@@ -346,6 +351,9 @@ zstate_t z80_bus_cmd(bus_cmd_t cmd)
                z80_addrbus_set_in();
                z80_reset_active();
                Z80_O_BUSREQ = 1;
+               timer = BUS_TO;
+               while (Z80_I_BUSACK == 0 && timer)
+                       ;
                zstate = RESET;
                break;
 
@@ -391,12 +399,18 @@ zstate_t z80_bus_cmd(bus_cmd_t cmd)
                        z80_addrbus_set_in();
                        z80_reset_active();
                        Z80_O_BUSREQ = 1;
+                       timer = BUS_TO;
+                       while (Z80_I_BUSACK == 0 && timer)
+                               ;
                        zstate = RESET;
                        break;
                case RUNNING_AQRD:
                        z80_dbus_set_in();
                        z80_addrbus_set_in();
                        Z80_O_BUSREQ = 1;
+                       timer = BUS_TO;
+                       while (Z80_I_BUSACK == 0 && timer)
+                               ;
                        zstate = RUNNING;
                        break;
                default:
@@ -407,6 +421,7 @@ zstate_t z80_bus_cmd(bus_cmd_t cmd)
        case Run:
                switch (zstate) {
                case RESET:
+                       _delay_ms(20);          /* TODO: */
                        z80_reset_inactive();
                        zstate = RUNNING;
                        break;
@@ -458,6 +473,41 @@ void z80_setaddress(uint32_t addr)
        PIN_ADB = (((addr >> 16) << ADB_SHIFT) ^ P_ADB) & MASK(ADB_WIDTH) << ADB_SHIFT;
 }
 
+int32_t z80_memsize_detect(void)
+{
+       const uint8_t PATTERN_1 = 0x55;
+       const uint8_t PATTERN_2 = ~PATTERN_1;
+       uint32_t addr;
+
+       if (!(z80_bus_cmd(Request) & ZST_ACQUIRED))
+               return  -EBUSTO;
+
+       uint8_t ram_0 = z80_read(0);
+       uint8_t ram_1 = z80_read(1);
+
+       z80_write(0, ram_0 ^ 0xff);
+       z80_write(1, ram_1);
+       if ((z80_read(0) ^ ram_0) != 0xff) {
+               addr = 0;
+       } else {
+               z80_write(0, PATTERN_1);
+               for (addr=1; addr < CONFIG_SYS_RAMSIZE_MAX; addr <<= 1) {
+                       uint8_t ram_i = z80_read(addr);
+                       z80_write(addr, PATTERN_2);
+                       if (z80_read(0) != PATTERN_1 || z80_read(addr) != PATTERN_2)
+                               break;
+                       z80_write(addr, ram_i);
+               }
+       }
+
+       z80_write(0, ram_0);
+       z80_bus_cmd(Release);
+
+       return addr;
+}
+
+/*--------------------------------------------------------------------------*/
+
 void z80_write(uint32_t addr, uint8_t data)
 {
        z80_setaddress(addr);
@@ -557,6 +607,7 @@ void z80_read_block (uint8_t *dest, uint32_t src, size_t length)
        Z80_O_MREQ = 1;
 }
 
+/*--------------------------------------------------------------------------*/
 
 /*
   0179'                         rx.bs_mask:    ds      1               ; (buf_len - 1)
@@ -697,3 +748,31 @@ void z80_memfifo_putc(fifo_t f, uint8_t val)
        z80_write(fifo_dsc[f].base+FIFO_INDEX_IN, fifo_dsc[f].idx_in);
        z80_bus_cmd(Release);
 }
+
+/*--------------------------------------------------------------------------*/
+
+void z80_load_mem(int_fast8_t verbosity,
+                               const FLASH unsigned char data[],
+                               const FLASH unsigned long *sections,
+                               const FLASH unsigned long address[],
+                               const FLASH unsigned long length_of_sections[])
+{
+       uint32_t sec_base = 0;
+
+       if (verbosity > 1)
+               printf_P(PSTR("Loading Z180 memory... \n"));
+
+       for (unsigned sec = 0; sec < *sections; sec++) {
+               if (verbosity > 0) {
+                       printf_P(PSTR("   From: 0x%.5lX to: 0x%.5lX    (%5li bytes)\n"),
+                                       address[sec],
+                                       address[sec]+length_of_sections[sec] - 1,
+                                       length_of_sections[sec]);
+               }
+
+               z80_write_block_P((const FLASH unsigned char *) &data[sec_base],  /* src */
+                               address[sec],                  /* dest */
+                               length_of_sections[sec]);      /* len */
+               sec_base += length_of_sections[sec];
+       }
+}