]> cloudbase.mooo.com Git - z180-stamp.git/commitdiff
working host communication, new command: connect
authorLeo C <erbl259-lmu@yahoo.de>
Thu, 23 Oct 2014 00:25:00 +0000 (02:25 +0200)
committerLeo C <erbl259-lmu@yahoo.de>
Thu, 23 Oct 2014 00:25:00 +0000 (02:25 +0200)
avr/background.c
avr/cmd_boot.c
avr/command_tbl.c
avr/main.c
avr/z180-serv.c
avr/z80-if.c
include/background.h
include/common.h
include/config.h
include/z180-serv.h [new file with mode: 0644]
include/z80-if.h

index 37e4b02c12b9eaa50d42bce39844436bdd9f09d6..0e1ca40047c5c464fd944fc78873c6d014fb9f06 100644 (file)
@@ -4,15 +4,39 @@
 
 #define BG_FUNC_MAX 5
 
-static bg_func func_tab[BG_FUNC_MAX];
+static struct { 
+       bg_func fct;
+       int param;
+} func_tab[BG_FUNC_MAX];
+
 static int_fast8_t fcount;
 
-int bg_register(bg_func f)
+int bg_register(bg_func f, int initval)
 {
        if (fcount < BG_FUNC_MAX) { 
-               func_tab[fcount++] = f;
+               func_tab[fcount].fct = f;
+               func_tab[fcount].param = initval;
+               return ++fcount - 1;
+       }
+       return -1;
+}
+
+int bg_setstat(int handle, int val)
+{
+       if (handle < fcount) {
+               func_tab[handle].param = val;
                return 1;
        }
+       
+       return 0;
+}
+
+
+int bg_getstat(int handle)
+{
+       if (handle < fcount) {
+               return func_tab[handle].param;
+       }
        return 0;
 }
 
@@ -21,8 +45,9 @@ void bg_shed(void)
 {
        static int_fast8_t current;
        
-       if (func_tab[current]) {
-               func_tab[current](0);
+       if (func_tab[current].fct) {
+               int v = func_tab[current].fct(func_tab[current].param);
+               func_tab[current].param = v;
        }
        if (++current >= fcount)
                current = 0;
index 17ed746e35edde82ff814d15a2f83f8daa05e17a..c2f03519d846baa869600c3cbb09885c954c0844 100644 (file)
@@ -5,9 +5,12 @@
 #include "common.h"
 #include <stdlib.h>
 #include <avr/pgmspace.h>
+#include <util/atomic.h>
 
 #include "command.h"
+#include "con-utils.h"
 #include "z80-if.h"
+#include "z180-serv.h"
 //#include "debug.h"
 
 /* ugly hack to get Z180 loadfile into flash memory */
@@ -130,6 +133,7 @@ command_ret_t do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]
 
        printf_P(PSTR("## CPU now in reset state.\n"));
 
+       restart_z180_serv();
        z80_bus_cmd(Reset);
        return CMD_RET_SUCCESS;
 }
@@ -138,8 +142,78 @@ command_ret_t do_restart(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv
 {
        (void) cmdtp; (void) flag; (void) argc; (void) argv;
 
+       restart_z180_serv();
        z80_bus_cmd(Restart);
 
        return CMD_RET_SUCCESS;
 }
 
+
+command_ret_t do_console(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+       int ch;
+       uint8_t pending, state = 0;
+       
+       (void) cmdtp; (void) flag; (void) argc; (void) argv;
+
+
+       while (1) {
+
+               ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
+                       pending = (Stat & S_CON_PENDING) != 0;
+                       Stat &= ~S_CON_PENDING;
+               }
+               if (pending)
+                       while ((ch = z80_memfifo_getc(fifo_conout)) >= 0)
+                               putchar(ch);
+
+               if ((ch = my_getchar(0)) >= 0) {
+                       switch (state) {
+                       case 0:
+                               if (ch == CONFIG_ESC_CHAR) {
+                                       state = 1;
+                                       /* TODO: Timer starten */
+                               } else {
+                                       z80_memfifo_putc(fifo_conin, ch);
+//                                     serial_putc(ch);
+//                                     if (ch == '\r')
+//                                             serial_putc('\n');
+                               }       
+                               break;
+                       case 1:
+                               switch (ch) {
+
+                               case 'r':
+//                                     z80_reset_pulse();
+                                       break;
+
+                               case 'b':
+                                       break;
+
+                               case 'e':
+                                       break;
+
+                               case 'q':
+                               case 'Q':
+                                       goto quit;
+                                       break;
+
+                               case CONFIG_ESC_CHAR:
+                               default:
+                                       z80_memfifo_putc(fifo_conin, ch);
+//                                     serial_putc(ch);
+//                                     if (ch == '\r')
+//                                             serial_putc('\n');
+                               }
+                               state = 0;
+                               break;
+                       }
+               }
+
+       }
+quit:
+
+       return CMD_RET_SUCCESS;
+}
+       
+
index e8af931b72445a577f6b479a285c961df6625f06..336e6ea3a068d66263519c1d9ebf1fdd014b5ff4 100644 (file)
@@ -14,12 +14,11 @@ extern command_ret_t do_env_save(cmd_tbl_t *, int, int, char * const []);
 extern command_ret_t do_loadf(cmd_tbl_t *, int, int, char * const []);
 extern command_ret_t do_go(cmd_tbl_t *, int, int, char * const []);
 extern command_ret_t do_restart(cmd_tbl_t *, int, int, char * const []);
+extern command_ret_t do_console(cmd_tbl_t *, int, int, char * const []);
 extern command_ret_t do_dump_mem(cmd_tbl_t *, int, int, char * const []);
 extern command_ret_t do_eep_cp(cmd_tbl_t *, int, int, char * const []);
 extern command_ret_t do_busreq_pulse(cmd_tbl_t *, int, int, char * const []);
 extern command_ret_t do_date(cmd_tbl_t *, int, int, char * const []);
-//extern command_ret_t do_clock(cmd_tbl_t *, int, int, char * const []);
-//extern command_ret_t do_clock2(cmd_tbl_t *, int, int, char * const []);
 extern command_ret_t do_pin(cmd_tbl_t *, int, int, char * const []);
 
 
@@ -118,10 +117,15 @@ CMD_TBL_ITEM(
        ""
 ),
 CMD_TBL_ITEM(
-       restart, 1, 0,  do_restart,
+       restart, 1, 1,  do_restart,
        "Perform RESET of the CPU",
        ""
 ),
+CMD_TBL_ITEM(
+       connect, 1, 1,  do_console,
+       "Connect to CPU console i/o",
+       ""
+),
 
 #if 0
 CMD_TBL_ITEM(
index 2a1ecd372150dcfeaa83e7b001a6f640cefc822c..34db6eff9eb4bcfd9103839e57b83873662380e8 100644 (file)
@@ -18,6 +18,7 @@
 #include "timer.h"
 #include "cli.h"
 #include "env.h"
+#include "z180-serv.h"
 
 #define udelay(n)  _delay_us(n)
 
@@ -66,6 +67,11 @@ ISR(INT5_vect)
        Stat |= S_MSG_PENDING;
 }
 
+ISR(INT6_vect)
+{
+       Stat |= S_CON_PENDING;
+}
+
 static
 void setup_avr(void)
 {
@@ -105,10 +111,13 @@ void setup_avr(void)
        TCCR3B = (0b01<<WGM32)|(0b001<<CS30); /* CTC Mode, Prescaler 1 */
        TIMSK3 = _BV(OCIE3A);           /* Enable TC2.oca interrupt */
 
-       /* INT5 falling edge */
-       EICRB = (EICRB & ~(0b11 << ISC50)) | 0b10 << ISC50;
-       /* Enable INT5 */
-       EIMSK |= _BV(INT5);
+       /* INT5, INT6: falling edge */
+       EICRB = (EICRB & ~((0b11 << ISC50) | (0b11 << ISC60))) | 
+               (0b10 << ISC50) | (0b10 << ISC60);
+       /* Reset pending ints */
+       EIFR |= _BV(INTF5) | _BV(INTF6);
+       /* Enable INT5, and INT6 */
+       EIMSK |= _BV(INT5) | _BV(INT6);
 }
 
 static
@@ -240,6 +249,7 @@ int main(void)
 
        printf_P(PSTR("\nATMEGA1281+Z8S180 Stamp Monitor\n\n"));
 
+       setup_z180_serv();
 
        main_loop();
 }
index c9203263ff7c67b56e7bd30e465fb63d673224dd..2dbac6515ded462d24ac47f963784780192af1d3 100644 (file)
@@ -4,14 +4,15 @@
 #include "common.h"
 //#include <avr/power.h>
 //#include <avr/pgmspace.h>
-//#include <util/atomic.h>
+#include <util/atomic.h>
 //#include <avr/sleep.h>
 //#include <string.h>
 
-
+#include "background.h"
 #include "debug.h"
 #include "serial.h"
 #include "z80-if.h"
+#include "z180-serv.h"
 
 
 
@@ -23,7 +24,7 @@ uint8_t z80_get_byte(uint32_t adr)
        uint8_t data;
        
        z80_bus_cmd(Request);
-       data = z80_read(adr),
+       data = z80_read(adr);
        z80_bus_cmd(Release);
        
        return data;
@@ -53,19 +54,12 @@ uint32_t msg_to_addr(uint8_t *msg)
        return addr.as32;
 }
 
-void do_msg_ini_msgfifo(uint8_t subf, int len, uint8_t * msg)
-{
-       (void)subf; (void)len;
-
-       z80_init_msg_fifo(msg_to_addr(msg));
-}
-
 
 void do_msg_ini_memfifo(uint8_t subf, int len, uint8_t * msg)
 {
        (void)len;
 
-       z80_memfifo_init(subf - 1, msg_to_addr(msg));
+       z80_memfifo_init(subf, msg_to_addr(msg));
 }
 
 
@@ -81,10 +75,7 @@ void do_msg_char_out(uint8_t subf, int len, uint8_t * msg)
 const FLASH struct msg_item z80_messages[] =
 {
        { 0,                    /* fct nr. */
-         0, 0,                 /* sub fct nr. from, to */
-         do_msg_ini_msgfifo},
-       { 0,
-         1, 2,
+         1, 3,                 /* sub fct nr. from, to */
          do_msg_ini_memfifo},
        { 1,
          1, 1,
@@ -108,17 +99,19 @@ void do_message(int len, uint8_t *msg)
                sub_fct = *msg++;
                len -= 2;
 
-               while (fct != z80_messages[i].fct)
+               while (fct != z80_messages[i].fct) {
+                       if (z80_messages[i].fct == 0xff) {
+                               DBG_P(1, "do_message: Unknown function: %i, %i\n",
+                                               fct, sub_fct);
+                               return; /* TODO: unknown message # */
+                       }
+               
                        ++i;
-
-               if (z80_messages[i].fct == 0xff) {
-                       DBG_P(1, "do_message: Unknown function: %i, %i\n",
-                                       fct, sub_fct);
-                       return; /* TODO: unknown message # */
                }
 
                while (fct == z80_messages[i].fct) {
-                       if (sub_fct >= z80_messages[i].sub_min && sub_fct <= z80_messages[i].sub_max )
+                       if (sub_fct >= z80_messages[i].sub_min && 
+                                       sub_fct <= z80_messages[i].sub_max )
                                break;
                        ++i;
                }
@@ -149,7 +142,7 @@ void check_msg_fifo(void)
        static int msglen,idx;
        static uint8_t buffer[CTRBUF_LEN];
 
-       while (state != 3 && (ch = z80_msg_fifo_getc()) >= 0) {
+       while ((ch = z80_memfifo_getc(fifo_msgin)) >= 0) {
                switch (state) {
                case 0:         /* wait for start of message */
                        if (ch == 0x81) {
@@ -167,18 +160,89 @@ void check_msg_fifo(void)
                        break;
                case 2:         /* get message */
                        buffer[idx++] = ch;
-                       if (idx == msglen)
-                               state = 3;
+                       if (idx == msglen) {
+                               do_message(msglen, buffer);
+                               state = 0;
+                       }
+                       break;
+               }
+       }
+}
+
+
+int msg_handling(int state)
+{
+       uint8_t pending;
+       
+       ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
+               pending = (Stat & S_MSG_PENDING) != 0;
+               Stat &= ~S_MSG_PENDING;
+       }
+               
+       if (pending) {
+               switch (state) {
+               case 0:
+                       z80_bus_cmd(Request);
+                       uint32_t addr = z80_read(0x40) +
+                               ((uint16_t) z80_read(0x41) << 8) +
+                               ((uint32_t) z80_read(0x42) << 16);
+                       z80_bus_cmd(Release);
+                       if (addr != 0) {
+                               z80_memfifo_init(fifo_msgin, addr);
+                               state = 1;
+                       }
+                       break;
+               case 1:
+                       check_msg_fifo();
                        break;
                }
        }
 
-       if (state == 3) {
-               do_message(msglen, buffer);
-               state = 0;
+       return state;
+}
+       
+
+int console_handling(int state)
+{
+       int ch;
+       uint8_t pending;
+       
+       ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
+               pending = (Stat & S_CON_PENDING) != 0;
+               Stat &= ~S_CON_PENDING;
+       }
+               
+       if (pending) {
+               while ((ch = z80_memfifo_getc(fifo_conout)) >= 0) {
+                       putchar(ch);
+               }
        }
+
+       return state;
+}
+       
+
+
+
+static int handle_msg_handling;
+
+void setup_z180_serv(void)
+{
+               
+       handle_msg_handling = bg_register(msg_handling, 0);
+//     bg_register(console_handling, 0);
 }
 
+void restart_z180_serv(void)
+{
+       z80_bus_cmd(Request);
+       z80_write(0x40, 0);
+       z80_write(0x41, 0);
+       z80_write(0x42, 0);
+       z80_bus_cmd(Release);
+               
+       bg_setstat(handle_msg_handling, 0);
+}
 
 /*--------------------------------------------------------------------------*/
 
@@ -250,5 +314,3 @@ const FLASH uint8_t test1[] = {
 
 
 
-//             check_msg_fifo();
-
index d14d9d7ec3d18d57e0713cdc07241a1429885760..414582e7a28c35fe45f0b019fd92f9b4cfad7419 100644 (file)
@@ -518,8 +518,7 @@ int z80_memfifo_is_empty(const fifo_t f)
 {
        int rc = 1;
 
-       if (((Stat & S_MSG_PENDING) || f != fifo_in) && fifo_dsc[f].base != 0)
-       {
+       if (fifo_dsc[f].base != 0) {
 
                uint32_t adr = fifo_dsc[f].base + FIFO_INDEX_IN;
                uint8_t idx;
@@ -546,7 +545,8 @@ int z80_memfifo_is_full(const fifo_t f)
        return rc;
 }
 
-uint8_t z80_memfifo_getc(const fifo_t f)
+
+uint8_t z80_memfifo_getc_wait(const fifo_t f)
 {
        uint8_t rc, idx;
 
@@ -563,6 +563,24 @@ uint8_t z80_memfifo_getc(const fifo_t f)
        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)
 {
index a624dbb87b4f6a208270b5ebbba385e3b952b7dd..8a430b3195b589bbbcfd379030223b2f358cb7a9 100644 (file)
@@ -3,7 +3,9 @@
 
 typedef int (*bg_func)(int);
 
-int bg_register(bg_func f);
+int bg_register(bg_func f, int initval);
+int bg_setstat(int handle, int val);
+int bg_getstat(int handle);
 void bg_shed(void);
 
 #endif /* BACKGROUND_H */
index d054ad14720ff46cde1aeec6afb4f5aceefcfe70..ef5af12b248940aadba8f1bdef6d16afbeafb0b7 100644 (file)
@@ -38,6 +38,7 @@ extern volatile uint_least8_t Stat;
 
 #define S_10MS_TO      (1<<0)
 #define S_MSG_PENDING  (2<<0)
+#define S_CON_PENDING  (3<<0)
 
 static inline 
 void my_puts(const char *s)
index c0777a8e36deaa9327bb7a682dccc09ff3f5551b..82dbf81c4cbaaa256c454e990c611a3c9e3666a2 100644 (file)
@@ -32,6 +32,7 @@
 #define CONFIG_SYS_ENV_NAMELEN 16
 
 #define CONFIG_SYS_PROMPT      "=> "
+#define CONFIG_ESC_CHAR                ('^'-0x40)
 
 
 /* TODO: */
diff --git a/include/z180-serv.h b/include/z180-serv.h
new file mode 100644 (file)
index 0000000..af4b1c0
--- /dev/null
@@ -0,0 +1,7 @@
+#ifndef Z180_SERV_H
+#define Z180_SERV_H
+
+void setup_z180_serv(void);
+void restart_z180_serv(void);
+
+#endif /* Z180_SERV_H */
index b4a4e6fdb9895ad468e6945e669f8ebe8d9b1831..24fda5d80f086c2365bc5f7d881a673adbd96aa4 100644 (file)
@@ -34,11 +34,16 @@ void z80_memset(uint32_t addr, uint8_t data, uint32_t length);
 void z80_write_block(const FLASH uint8_t *src, uint32_t dest, uint32_t length);
 
 
-typedef enum fifo_t {fifo_in, fifo_out, NUM_FIFOS} fifo_t;
+typedef enum fifo_t {
+               fifo_msgin, fifo_msgout, 
+               fifo_conout, fifo_conin,
+               NUM_FIFOS
+       } fifo_t;
 
 void z80_memfifo_init(const fifo_t f, uint32_t adr);
 int z80_memfifo_is_empty(const fifo_t f);
 int z80_memfifo_is_full(const fifo_t f);
-uint8_t z80_memfifo_getc(const fifo_t f);
+int z80_memfifo_getc(const fifo_t f);
+uint8_t z80_memfifo_getc_wait(const fifo_t f);
 void z80_memfifo_putc(fifo_t f, uint8_t val);