coco2/dragon64: Initial 64K RAM port tree
authorAlan Cox <alan@linux.intel.com>
Thu, 1 Oct 2015 21:33:12 +0000 (22:33 +0100)
committerAlan Cox <alan@linux.intel.com>
Thu, 1 Oct 2015 21:33:12 +0000 (22:33 +0100)
This isn't a working tree yet, but once it is will replace the ROM replacement
hack tree.

27 files changed:
Kernel/platform-coco2/Makefile [new file with mode: 0644]
Kernel/platform-coco2/README [new file with mode: 0644]
Kernel/platform-coco2/carts.h [new file with mode: 0644]
Kernel/platform-coco2/coco2.s [new file with mode: 0644]
Kernel/platform-coco2/commonmem.s [new file with mode: 0644]
Kernel/platform-coco2/config.h [new file with mode: 0644]
Kernel/platform-coco2/crt0.s [new file with mode: 0644]
Kernel/platform-coco2/devfd.h [new file with mode: 0644]
Kernel/platform-coco2/device.h [new file with mode: 0644]
Kernel/platform-coco2/devices.c [new file with mode: 0644]
Kernel/platform-coco2/devlpr.c [new file with mode: 0644]
Kernel/platform-coco2/devlpr.h [new file with mode: 0644]
Kernel/platform-coco2/devtty.c [new file with mode: 0644]
Kernel/platform-coco2/devtty.h [new file with mode: 0644]
Kernel/platform-coco2/discard.c [new file with mode: 0644]
Kernel/platform-coco2/drivewire.s [new file with mode: 0644]
Kernel/platform-coco2/dw.def [new file with mode: 0644]
Kernel/platform-coco2/fuzix.link [new file with mode: 0644]
Kernel/platform-coco2/ide.s [new file with mode: 0644]
Kernel/platform-coco2/kernel.def [new file with mode: 0644]
Kernel/platform-coco2/libc.c [new file with mode: 0644]
Kernel/platform-coco2/main.c [new file with mode: 0644]
Kernel/platform-coco2/mini_ide.c [new file with mode: 0644]
Kernel/platform-coco2/mini_ide.h [new file with mode: 0644]
Kernel/platform-coco2/mini_ide_discard.c [new file with mode: 0644]
Kernel/platform-coco2/target.mk [new file with mode: 0644]
Kernel/platform-coco2/tricks.s [new file with mode: 0644]

diff --git a/Kernel/platform-coco2/Makefile b/Kernel/platform-coco2/Makefile
new file mode 100644 (file)
index 0000000..8ab8fed
--- /dev/null
@@ -0,0 +1,59 @@
+
+CSRCS = devtty.c
+CSRCS += devices.c main.c mini_ide.c
+
+C3SRCS = libc.c
+
+CDSRCS = discard.c mini_ide_discard.c
+
+DSRCS = ../dev/devdw.c
+
+ASRCS = crt0.s coco2.s ide.s
+ASRCS += tricks.s commonmem.s drivewire.s
+
+COBJS = $(CSRCS:.c=$(BINEXT))
+C3OBJS = $(C3SRCS:.c=$(BINEXT))
+CDOBJS = $(CDSRCS:.c=$(BINEXT))
+AOBJS = $(ASRCS:.s=$(BINEXT))
+DOBJS = $(patsubst ../dev/%.c,%.o, $(DSRCS))
+OBJS  = $(COBJS) $(C3OBJS) $(CDOBJS) $(AOBJS) $(DOBJS)
+
+CROSS_CCOPTS += -I../dev/
+
+all: $(OBJS)
+
+export CROSS_CC_SEGD=-mcode-section=.text3 -mfar-code-page=2
+
+$(COBJS): %$(BINEXT): %.c
+       $(CROSS_CC) $(CROSS_CCOPTS) $(CROSS_CC_SEG2) -c $<
+
+$(C3OBJS): %$(BINEXT): %.c
+       $(CROSS_CC) $(CROSS_CCOPTS) $(CROSS_CC_SEGD) -c $<
+
+$(CDOBJS): %$(BINEXT): %.c
+       $(CROSS_CC) $(CROSS_CCOPTS) $(CROSS_CC_SEGDISC) -c $<
+
+$(DOBJS): %$(BINEXT): ../dev/%.c
+       $(CROSS_CC) $(CROSS_CCOPTS) $(CROSS_CC_SEGD) -c $<
+
+$(DDOBJS): %$(BINEXT): ../dev/%.c
+       $(CROSS_CC) $(CROSS_CCOPTS) $(CROSS_CC_SEGDISC) -c $<
+
+$(AOBJS): %$(BINEXT): %.s
+       $(CROSS_AS) $(ASOPTS) $< -o $*.o
+
+clean:
+       rm -f *.o *~
+
+image:
+       $(CROSS_LD) -o ../fuzix.bin --map=../fuzix.map --script=fuzix.link \
+       crt0.o commonmem.o \
+       coco2.o discard.o ../simple.o \
+       ../start.o ../version.o ../lowlevel-6809.o \
+       tricks.o main.o ../timer.o ../kdata.o devices.o \
+       drivewire.o devdw.o mini_ide.o mini_ide_discard.o \
+       ../devio.o ../filesys.o ../process.o ../inode.o ../syscall_fs.o \
+       ../syscall_proc.o ../syscall_other.o ../mm.o ../swap.o \
+       ../tty.o ../devsys.o ../usermem.o ../syscall_fs2.o ../syscall_exec16.o \
+       devtty.o libc.o ../vt.o
+
diff --git a/Kernel/platform-coco2/README b/Kernel/platform-coco2/README
new file mode 100644 (file)
index 0000000..17062b8
--- /dev/null
@@ -0,0 +1,65 @@
+COCO2
+=====
+
+COCO or Dragon 64 with an IDE port and 64K of RAM
+
+Supported hardware:
+       IDE (Glenside or compatible)
+
+Memory Map
+==========
+0000-00FF      Interrupt stack
+0100-01FF      Vectors (then wasted - could hide libc.o in there ?)
+0200-03FF      Display
+0400-7FFF      Code/Data for kernel (take care to keep this below 8000 and
+               shuffle stuff if needed to make it fit)
+8000-EFFF      User space
+F000-FBFF      More kernel bits
+FC00-FEFF      udata
+FF00-FFFF      I/O etc
+
+
+Emulating in XRoar
+==================
+
+Needs the IDE patches
+
+TODO
+====
+
+Loader code
+Set the display up correctly
+Support for a real time clock and clock locking
+Review irq disabling. We shouldn't need much of this at all as we don't have
+floppy hell to contend with, but the switchin/switchout logic needs to be
+reviewed for interrupt safety and the IRQs re-enabled during swap in and
+out. We should also be totally clean now on FIR blocking, so audio might
+actually be doable!
+
+MPI switching
+
+Could we do a cartridge version
+- Re-order so common is in low memory
+- Support map_kernel/map_process flipping
+- Add asm helpers for user mode copying (trivial - and since we never copy
+  into kernel ROM we can optimise them a lot - one map switch only)
+- Fix IDE to support the map flip on transfers (needs little asm code
+  adding)
+- How to do discard, can we just take the hit or do we need to copy the
+  entire cartridge into high RAM, run in high RAM with discard at say
+  0xA000 and then in platform_discard switch on map_kernel cartridge
+  selection so we can let userspace blow away the upper 32K of the RAM
+  mapping.
+
+16K Cartridge version would give us space for
+- Proper video (costs us about 8K)
+- Nice loader (just 'EXEC $C000' and go) < 1K
+- Probably all of sound/joysticks/printer/floppy and a few more buffers
+- Full size user memory bank (so can run full v7 shell) 4K
+
+
+Write a loader into the cartridge start so it bootstraps off disk and runs
+nicely
+
+6847T1 lower case ?
+
diff --git a/Kernel/platform-coco2/carts.h b/Kernel/platform-coco2/carts.h
new file mode 100644 (file)
index 0000000..86d4941
--- /dev/null
@@ -0,0 +1,10 @@
+#define CART_DRAGONDOS 1               /* DragonDOS floppy */
+#define CART_DELTADOS  2               /* DeltaDOS floppy */
+#define CART_RSDOS     3               /* RSDOS Cartridge */
+#define CART_ORCH90    4               /* Orchestra Sound */
+
+extern int cart_find(int id);
+extern uint8_t carttype[4];
+extern uint8_t cartslots;
+extern uint8_t bootslot;
+extern uint8_t system_id;
diff --git a/Kernel/platform-coco2/coco2.s b/Kernel/platform-coco2/coco2.s
new file mode 100644 (file)
index 0000000..6750c9a
--- /dev/null
@@ -0,0 +1,207 @@
+       ;
+       ; COCO2 platform
+       ;
+
+       .module coco2
+
+       ; exported
+       .globl _mpi_present
+       .globl _mpi_set_slot
+       .globl _cart_hash
+       .globl map_kernel
+       .globl map_process
+       .globl map_process_a
+       .globl map_process_always
+       .globl map_save
+       .globl map_restore
+        .globl init_early
+        .globl init_hardware
+        .globl _program_vectors
+       .globl _need_resched
+
+       .globl _ramsize
+       .globl _procmem
+
+       ; imported
+       .globl unix_syscall_entry
+;FIX   .globl fd_nmi_handler
+       .globl null_handler
+       .globl _vtoutput
+
+       ; exported debugging tools
+       .globl _trap_monitor
+       .globl _trap_reboot
+       .globl outchar
+       .globl _di
+       .globl _ei
+       .globl _irqrestore
+
+       include "kernel.def"
+       include "../kernel09.def"
+
+
+               .area .vectors
+       ;
+       ;       At 0x100 as required by the COCO ROM
+       ;
+       jmp badswi_handler                      ; 0x100
+       jmp badswi_handler                      ; 0x103
+       jmp unix_syscall_entry                  ; 0x106
+       ; FIXME: 109 needs to handle bad NMI!
+       jmp badswi_handler                      ; 0x109
+       jmp interrupt_handler                   ; 0x10C
+       jmp firq_handler                        ; 0x10F
+
+       .area .text
+
+init_early:
+       ldx #null_handler
+       stx 1
+       lda #0x7E
+       sta 0
+       sta 0x0071              ; BASIC cold boot flag (will get trashed by
+                               ; irq stack but hopefully still look bad)
+       ;
+       ;       Move the display
+       ;
+       ldx #0xFFC7
+       clr ,x+                 ; Set F0
+       clr ,x++                ; Clear F1-F6
+       clr ,x++
+       clr ,x++
+       clr ,x++
+       clr ,x++
+       clr ,x++
+       rts
+
+init_hardware:
+       ldd #64
+       std _ramsize
+       ldd #32
+       std _procmem
+
+       ; Turn on PIA  CB1 (50Hz interrupt)
+       lda 0xFF03
+       ora #1
+       sta 0xFF03
+       jsr _vtinit
+       rts
+
+        .area .common
+
+_trap_reboot:
+       orcc #0x10
+       clr 0xFFBE
+       lda #0x7e
+       sta 0x0071              ; in case IRQ left it looking valid
+       jmp [0xFFFE]
+
+_trap_monitor:
+       orcc #0x10
+       bra _trap_monitor
+
+_di:
+       tfr cc,b                ; return the old irq state
+       orcc #0x10
+       rts
+_ei:
+       andcc #0xef
+       rts
+
+_irqrestore:                   ; B holds the data
+       tfr b,cc
+       rts
+
+;
+; COMMON MEMORY PROCEDURES FOLLOW
+;
+
+       .area .common
+;
+;      Our vectors live in a fixed block and are not banked out. Likewise
+;      No memory management for now
+;
+_program_vectors:
+map_kernel:
+map_restore:
+map_process:
+map_process_always:
+map_process_a:
+map_save:
+       rts
+;
+;      Helpers for the MPI and Cartridge Detect
+;
+
+;
+;      oldslot = mpi_set_slot(uint8_t newslot)
+;
+_mpi_set_slot:
+       tfr b,a
+       ldb 0xff7f
+       sta 0xff7f
+       rts
+;
+;      int8_t mpi_present(void)
+;
+_mpi_present:
+       lda 0xff7f      ; Save bits
+       ldb #0xff       ; Will get back 33 from an MPI cartridge
+       stb 0xff7f      ; if the emulator is right on this
+       ldb 0xff7f
+       cmpb #0x33
+       bne nompi
+       clr 0xff7f      ; Switch to slot 0
+       ldb 0xff7f
+       bne nompi
+       incb
+       sta 0xff7f      ; Our becker port for debug will be on the default
+                       ; slot so put it back for now
+       rts             ; B = 0
+nompi: ldb #0
+       sta 0xff7f      ; Restore bits just in case
+       rts
+
+       .area .text     ; Must be clear of cartridge mapping areas
+;
+;      uint16_t cart_hash(void)
+;
+_cart_hash:
+       pshs cc
+       orcc #0x10
+       ldx #0xC000
+       ldd #0
+       clr $FFBE       ; Map cartridge
+hashl:
+       addd ,x++
+       cmpx #0xC200
+       bne hashl
+       tfr d,x
+       clr $FFBF       ; Return to normality
+       puls cc,pc
+
+
+       .area .common
+;
+;      FIXME:
+;
+firq_handler:
+badswi_handler:
+       rti
+
+;
+;      debug via printer port
+;
+outchar:
+       sta 0xFF02
+       lda 0xFF20
+       ora #0x02
+       sta 0xFF20
+       anda #0xFD
+       sta 0xFF20
+       rts
+
+       .area .common
+
+_need_resched:
+       .db 0
diff --git a/Kernel/platform-coco2/commonmem.s b/Kernel/platform-coco2/commonmem.s
new file mode 100644 (file)
index 0000000..b820abb
--- /dev/null
@@ -0,0 +1,25 @@
+;
+        .module commonmem
+
+        ; exported symbols
+        .globl _ub
+        .globl _udata
+        .globl kstack_top
+        .globl istack_top
+        .globl istack_switched_sp
+
+        .area .udata
+
+_ub:    ; first 512 bytes: starts with struct u_block, with the kernel stack working down from above
+_udata:
+kstack_base:
+       zmb 512
+kstack_top:
+
+       .area .istack
+
+        ; next 256 bytes: 254 byte interrupt stack, then 2 byte saved stack pointer
+istack_base:
+       zmb 254
+istack_top:
+istack_switched_sp: .dw 0
diff --git a/Kernel/platform-coco2/config.h b/Kernel/platform-coco2/config.h
new file mode 100644 (file)
index 0000000..91b79bc
--- /dev/null
@@ -0,0 +1,63 @@
+/* Enable to make ^Z dump the inode table for debug */
+#undef CONFIG_IDUMP
+/* Enable to make ^A drop back into the monitor */
+#undef CONFIG_MONITOR
+/* Profil syscall support (not yet complete) */
+#undef CONFIG_PROFIL
+/* Multiple processes in memory at once */
+#undef CONFIG_MULTI
+/* Single tasking - for now while we get it booting */
+#undef CONFIG_SINGLETASK
+/* Pure swap */
+#define CONFIG_SWAP_ONLY
+#define CONFIG_SPLIT_UDATA
+#define CONFIG_USERMEM_DIRECT
+
+#define CONFIG_BANKS   1
+/* And swapping */
+#define SWAPDEV     0x0                /* Uses part of IDE slice 0 */
+#define SWAP_SIZE   0x40       /* 32K in 512 byte blocks */
+#define SWAPBASE    0x8000     /* We swap the lot, including stashed uarea */
+#define SWAPTOP     0xF000     /* so it's a round number of 512 byte sectors */
+#define UDATA_SIZE  0x0200     /* one block */
+#define MAX_SWAPS   32
+
+/* Permit large I/O requests to bypass cache and go direct to userspace */
+#define CONFIG_LARGE_IO_DIRECT
+
+/* Video terminal, not a serial tty */
+#define CONFIG_VT
+#define CONFIG_VT_SIMPLE
+
+extern unsigned char vt_mangle_6847(unsigned char c);
+#define VT_MAP_CHAR(x) vt_mangle_6847(x)
+
+/* Vt definitions */
+#define VT_WIDTH       32
+#define VT_HEIGHT      24
+#define VT_RIGHT       31
+#define VT_BOTTOM      23
+#define VT_INITIAL_LINE        0
+
+#define VT_BASE        ((uint8_t *)0x0200)
+
+/* RS/Tandy Color Computer keyboard */
+#undef CONFIG_COCO_KBD
+
+#define TICKSPERSEC 50   /* Ticks per second */
+#define PROGBASE    0x8000  /* also data base */
+#define PROGLOAD    0x8000  /* also data base */
+#define PROGTOP     0xF000  /* Top of program */
+
+/* We need a tidier way to do this from the loader */
+#define CMDLINE        NULL      /* Location of root dev name */
+
+/* Device parameters */
+#define NUM_DEV_TTY 2
+#define NDEVS    2        /* Devices 0..NDEVS-1 are capable of being mounted */
+                          /*  (add new mountable devices to beginning area.) */
+#define TTYDEV   513    /* Device used by kernel for messages, panics */
+#define NBUFS    6       /* Number of block buffers */
+#define NMOUNTS         2        /* Number of mounts at a time */
+#define swap_map(x)    ((uint8_t *)(x))
+
diff --git a/Kernel/platform-coco2/crt0.s b/Kernel/platform-coco2/crt0.s
new file mode 100644 (file)
index 0000000..c4e3341
--- /dev/null
@@ -0,0 +1,50 @@
+               ; exported
+               .globl start
+
+               ; imported symbols
+               .globl _fuzix_main
+               .globl init_early
+               .globl init_hardware
+               .globl kstack_top
+               .globl _system_id
+
+               ; startup code
+               .area .start
+
+start:
+               orcc #0x10              ; interrupts definitely off
+               ; We need to run this with the low 32K ROM mapped
+               sta $FFDE               ; FIXME: selectable - FFBF for 32K cart
+               ldb $80FD               ; save from ROM space
+               sta $FFDF
+               jmp main
+
+               .area .text
+
+main:          
+               lds #kstack_top
+               ldx #__sectionbase_.bss__
+               ldy #__sectionlen_.bss__
+               clra
+bss_wipe:      sta ,x+
+               leay -1,y
+               bne bss_wipe
+               ; This might be in BSS so don't save until we've wiped!
+               lda #0
+               cmpb #0x49              ; Dragon32
+               beq identified
+               inca
+               cmpb #0x31              ; COCO1/2
+               beq identified
+               inca
+               cmpb #0x32              ; COCO3
+               beq identified
+               inca                    ; Beats me
+identified:
+               sta _system_id          ; what sort of a box are we ?
+               jsr init_early
+               jsr init_hardware
+               jsr _fuzix_main
+               orcc #0x10
+stop:          bra stop
+
diff --git a/Kernel/platform-coco2/devfd.h b/Kernel/platform-coco2/devfd.h
new file mode 100644 (file)
index 0000000..b2754e6
--- /dev/null
@@ -0,0 +1,17 @@
+#ifndef __DEVFD_DOT_H__
+#define __DEVFD_DOT_H__
+
+/* public interface */
+int fd_read(uint8_t minor, uint8_t rawflag, uint8_t flag);
+int fd_write(uint8_t minor, uint8_t rawflag, uint8_t flag);
+int fd_open(uint8_t minor, uint16_t flag);
+void fd_timer_tick(void);
+
+/* low level interface */
+uint8_t fd_reset(uint8_t *drive);
+uint8_t fd_operation(uint8_t *cmd, uint8_t *drive);
+uint8_t fd_motor_on(uint8_t drive);
+uint8_t fd_motor_off(void);
+
+#endif /* __DEVFD_DOT_H__ */
+
diff --git a/Kernel/platform-coco2/device.h b/Kernel/platform-coco2/device.h
new file mode 100644 (file)
index 0000000..e57c6a3
--- /dev/null
@@ -0,0 +1,11 @@
+#ifndef __DEVICE_DOT_H__
+#define __DEVICE_DOT_H__
+
+extern uint8_t system_id;
+extern uint8_t mpi_present(void);
+extern uint8_t mpi_set_slot(uint8_t slot);
+extern uint16_t cart_hash(void);
+
+extern uint8_t spi_setup(void);
+
+#endif /* __DEVICE_DOT_H__ */
diff --git a/Kernel/platform-coco2/devices.c b/Kernel/platform-coco2/devices.c
new file mode 100644 (file)
index 0000000..5c3689b
--- /dev/null
@@ -0,0 +1,49 @@
+#include <kernel.h>
+#include <version.h>
+#include <kdata.h>
+#include <devfd.h>
+#include <devdw.h>
+#include <devsys.h>
+#include <devlpr.h>
+#include <tty.h>
+#include <vt.h>
+#include <devtty.h>
+#include <mini_ide.h>
+#include <device.h>
+
+struct devsw dev_tab[] =  /* The device driver switch table */
+{
+// minor    open         close        read      write       ioctl
+// -----------------------------------------------------------------
+  /* 0: /dev/hd                Hard disc block devices (IDE) */
+  {  ide_open,     no_close,    ide_read,  ide_write, ide_ioctl },
+  /* 1: /dev/fd                Floppy disc block devices  */
+  {  no_open,       no_close,    no_rdwr,   no_rdwr ,  no_ioctl },
+  /* 2: /dev/tty       TTY devices */
+  {  tty_open,      tty_close,   tty_read,  tty_write, vt_ioctl },
+    /* 3: /dev/lpr     Printer devices */
+  {  no_open,       no_close,    no_rdwr,   no_rdwr,   no_ioctl  },
+  /* 4: /dev/mem etc   System devices (one offs) */
+  {  no_open,       no_close,    sys_read, sys_write,  sys_ioctl  },
+  /* Pack to 7 with nxio if adding private devices and start at 8 */
+  {  nxio_open,     no_close,    no_rdwr,   no_rdwr,   no_ioctl },
+  {  nxio_open,     no_close,    no_rdwr,   no_rdwr,   no_ioctl },
+  {  nxio_open,     no_close,    no_rdwr,   no_rdwr,   no_ioctl },
+  /* 8: /dev/dw                DriveWire remote disk images */
+  {  dw_open,       no_close,    dw_read,   dw_write,  no_ioctl },
+};
+
+bool validdev(uint16_t dev)
+{
+    /* This is a bit uglier than needed but the right hand side is
+       a constant this way */
+    if(dev > ((sizeof(dev_tab)/sizeof(struct devsw)) << 8) + 255)
+       return false;
+    else
+        return true;
+}
+
+void device_init(void)
+{
+    ide_probe();
+}
diff --git a/Kernel/platform-coco2/devlpr.c b/Kernel/platform-coco2/devlpr.c
new file mode 100644 (file)
index 0000000..9ddfae4
--- /dev/null
@@ -0,0 +1,70 @@
+#include <kernel.h>
+#include <version.h>
+#include <kdata.h>
+#include <device.h>
+#include <devlpr.h>
+
+volatile uint8_t * const pia0b = (uint8_t *)0xFF02;
+volatile uint8_t * const pia1a = (uint8_t *)0xFF20;
+volatile uint8_t * const pia1b = (uint8_t *)0xFF22;
+
+int lpr_open(uint8_t minor, uint16_t flag)
+{
+       if (minor < 2)
+               return 0;
+       udata.u_error = ENODEV;
+       return -1;
+}
+
+int lpr_close(uint8_t minor)
+{
+       if (minor == 1)
+               dw_lpr_close();
+       return 0;
+}
+
+static int iopoll(int sofar)
+{
+       /* Ought to be a core helper for this lot ? */
+       if (need_resched())
+               _sched_yield();
+       if (udata.u_cursig || udata.u_ptab->p_pending) {
+               if (sofar)
+                       return sofar;
+               udata.u_error = EINTR;
+               return -1;
+       }
+       return 0;
+}
+
+int lpr_write(uint8_t minor, uint8_t rawflag, uint8_t flag)
+{
+       uint8_t *p = udata.u_base;
+       uint8_t *pe = p + udata.u_count;
+       int n;
+       irqflags_t irq;
+
+       if (minor == 1) {
+               while (p < pe) {
+                       if ((n = iopoll(pe - p)) != 0)
+                               return n;
+                       dw_lpr(ugetc(p++));
+               }
+       } else {
+               while (p < pe) {
+                       /* Printer busy ? */
+                       while (*pia1b & 1) {
+                               if ((n = iopoll(pe - p)) != 0)
+                                       return n;
+                       }
+                       /* The Dragon shares the printer and keyboard scan
+                          lines. Make sure we don't collide */
+                       irq = di();
+                       *pia0b = ugetc(p++);
+                       *pia1a |= 0x02; /* Strobe */
+                       *pia1a &= ~0x02;
+                       irqrestore(irq);
+               }
+       }
+       return pe - p;
+}
diff --git a/Kernel/platform-coco2/devlpr.h b/Kernel/platform-coco2/devlpr.h
new file mode 100644 (file)
index 0000000..0e078ca
--- /dev/null
@@ -0,0 +1,11 @@
+#ifndef __DEVLPR_DOT_H__
+#define __DEVLPR_DOT_H__
+
+extern int lpr_open(uint8_t minor, uint16_t flag);
+extern int lpr_close(uint8_t minor);
+extern int lpr_write(uint8_t minor, uint8_t rawflag, uint8_t flag);
+
+extern void dw_lpr(uint8_t c);
+extern void dw_lpr_close(void);
+
+#endif
diff --git a/Kernel/platform-coco2/devtty.c b/Kernel/platform-coco2/devtty.c
new file mode 100644 (file)
index 0000000..a317e48
--- /dev/null
@@ -0,0 +1,237 @@
+#include <kernel.h>
+#include <kdata.h>
+#include <printf.h>
+#include <stdbool.h>
+#include <devtty.h>
+#include <device.h>
+#include <devfd.h>
+#include <vt.h>
+#include <tty.h>
+#include <graphics.h>
+
+#undef  DEBUG                  /* UNdefine to delete debug code sequences */
+
+uint8_t *uart_data = (uint8_t *)0xFF04;        /* ACIA data */
+uint8_t *uart_status = (uint8_t *)0xFF05;      /* ACIA status */
+uint8_t *uart_command = (uint8_t *)0xFF06;     /* ACIA command */
+uint8_t *uart_control = (uint8_t *)0xFF07;     /* ACIA control */
+
+unsigned char tbuf1[TTYSIZ];
+unsigned char tbuf2[TTYSIZ];
+
+struct s_queue ttyinq[NUM_DEV_TTY + 1] = {     /* ttyinq[0] is never used */
+       {NULL, NULL, NULL, 0, 0, 0},
+       {tbuf1, tbuf1, tbuf1, TTYSIZ, 0, TTYSIZ / 2},
+       {tbuf2, tbuf2, tbuf2, TTYSIZ, 0, TTYSIZ / 2}
+};
+
+uint8_t vtattr_cap = VTA_INVERSE|VTA_UNDERLINE|VTA_ITALIC|VTA_BOLD|
+                    VTA_OVERSTRIKE|VTA_NOCURSOR;
+
+/* tty1 is the screen tty2 is the serial port */
+
+/* Output for the system console (kprintf etc) */
+void kputchar(char c)
+{
+       if (c == '\n')
+               tty_putc(1, '\r');
+       tty_putc(1, c);
+}
+
+ttyready_t tty_writeready(uint8_t minor)
+{
+       uint8_t c;
+       if (minor == 1)
+               return TTY_READY_NOW;
+       c = *uart_status;
+       return (c & 16) ? TTY_READY_NOW : TTY_READY_SOON; /* TX DATA empty */
+}
+
+/* For DragonPlus we should perhaps support both monitors 8) */
+
+void tty_putc(uint8_t minor, unsigned char c)
+{
+       irqflags_t irq;
+       if (minor == 1) {
+               /* We need a better way generally to handle keyboard v
+                  VT */
+               irq = di();
+               vtoutput(&c, 1);
+               irqrestore(irq);
+       } else
+               *uart_data = c; /* Data */
+}
+
+void tty_sleeping(uint8_t minor)
+{
+    used(minor);
+}
+
+void tty_setup(uint8_t minor)
+{
+       if (minor == 2) {
+               /* FIXME: do proper mode setting */
+               *uart_command = 0x01;   /* DTR high, IRQ enabled, TX irq disabled 8N1 */
+               *uart_control = 0x1E;   /* 9600 baud */
+       }
+}
+
+int tty_carrier(uint8_t minor)
+{
+       /* The serial DCD is status bit 5 but not wired */
+       return 1;
+}
+
+void tty_interrupt(void)
+{
+       uint8_t r = *uart_status;
+       if (r & 0x8) {
+               r = *uart_data;
+               tty_inproc(2,r);
+       }       
+}
+
+uint8_t keymap[8];
+static uint8_t keyin[8];
+static uint8_t keybyte, keybit;
+static uint8_t newkey;
+static int keysdown = 0;
+/* FIXME: shouldn't COCO shiftmask also differ ??? 0x02 not 0x40 ?? */
+static uint8_t shiftmask[8] = {
+       0, 0x40, 0, 0, 0, 0, 0, 0x40
+};
+
+static uint8_t rbit[8] = {
+       0xFE,
+       0xFD,
+       0xFB,
+       0xF7,
+       0xEF,
+       0xDF,
+       0xBF,
+       0x7F,
+};
+
+/* Row inputs: multiplexed with the joystick */
+static volatile uint8_t *pia_row = (uint8_t *)0xFF00;
+/* Columns for scanning: multiplexed with the printer port */
+static volatile uint8_t *pia_col = (uint8_t *)0xFF02;
+/* Control */
+static volatile uint8_t *pia_ctrl = (uint8_t *)0xFF03;
+
+static void keyproc(void)
+{
+       int i;
+       uint8_t key;
+
+       for (i = 0; i < 8; i++) {
+               /* We do the scan in software on the Dragon */
+               *pia_col = rbit[i];
+               keyin[i] = ~*pia_row;
+               key = keyin[i] ^ keymap[i];
+               if (key) {
+                       int n;
+                       int m = 1;
+                       for (n = 0; n < 7; n++) {
+                               if ((key & m) && (keymap[i] & m)) {
+                                       if (!(shiftmask[i] & m))
+                                               keysdown--;
+                               }
+                               if ((key & m) && !(keymap[i] & m)) {
+                                       if (!(shiftmask[i] & m)) {
+                                               keysdown++;
+                                               newkey = 1;
+                                               keybyte = i;
+                                               keybit = n;
+                                       }
+                               }
+                               m += m;
+                       }
+               }
+               keymap[i] = keyin[i];
+       }
+       if (system_id) {        /* COCO series */
+         keybit += 2;
+         if (keybit > 5)
+           keybit -= 6;
+        }
+}
+
+#ifdef CONFIG_COCO_KBD
+uint8_t keyboard[8][7] = {
+       { '@', 'h', 'p', 'x', '0', '8', KEY_ENTER },
+       { 'a', 'i', 'q', 'y', '1', '9', 0 /* clear - used as ctrl*/ },
+       { 'b', 'j', 'r', 'z', '2', ':', KEY_ESC /* break (used for esc) */ },
+       { 'c', 'k', 's', '^' /* up */, '3', ';' , 0 /* NC */ },
+       { 'd', 'l', 't', '|' /* down */, '4', ',', 0 /* NC */ },
+       { 'e', 'm', 'u', KEY_BS /* left */, '5', '-', 0 /* NC */ },
+       { 'f', 'n', 'v', KEY_TAB /* right */, '6', '.', 0 /* NC */ },
+       { 'g', 'o', 'w', ' ', '7', '/', 0 /* shift */ },
+};
+
+uint8_t shiftkeyboard[8][7] = {
+       { '\\', 'H', 'P', 'X', '_', '(', KEY_ENTER },
+       { 'A', 'I', 'Q', 'Y', '!', ')', 0 /* clear - used as ctrl */ },
+       { 'B', 'J', 'R', 'Z', '"', '*', CTRL('C') /* break */ },
+       { 'C', 'K', 'S', '[' /* up */, '#', '+', 0 /* NC */ },
+       { 'D', 'L', 'T', ']' /* down */, '$', '<', 0 /* NC */ },
+       { 'E', 'M', 'U', '{' /* left */, '%', '=', 0 /* NC */ },
+       { 'F', 'N', 'V', '}' /* right */, '&', '>', 0 /* NC */ },
+       { 'G', 'O', 'W', ' ', '\'', '?', 0 /* shift */ },
+};
+#else
+uint8_t keyboard[8][7] = {
+       { '0', '8', '@', 'h', 'p', 'x', KEY_ENTER },
+       { '1', '9', 'a', 'i', 'q', 'y', 0 /* clear - used as ctrl*/ },
+       { '2', ':', 'b', 'j', 'r', 'z', KEY_ESC /* break (used for esc) */ },
+       { '3', ';', 'c', 'k', 's', '^' /* up */, 0 /* NC */ },
+       { '4', ',', 'd', 'l', 't', '|' /* down */, 0 /* NC */ },
+       { '5', '-', 'e', 'm', 'u', KEY_BS /* left */, 0 /* NC */ },
+       { '6', '.', 'f', 'n', 'v', KEY_TAB /* right */, 0 /* NC */ },
+       { '7', '/', 'g', 'o', 'w', ' ', 0 /* shift */ },
+};
+
+uint8_t shiftkeyboard[8][7] = {
+       { '_', '(', '\\', 'H', 'P', 'X', KEY_ENTER },
+       { '!', ')', 'A', 'I', 'Q', 'Y', 0 /* clear - used as ctrl*/ },
+       { '"', '*', 'B', 'J', 'R', 'Z', CTRL('C') /* break */ },
+       { '#', '+', 'C', 'K', 'S', '[' /* up */, 0 /* NC */ },
+       { '$', '<', 'D', 'L', 'T', ']' /* down */, 0 /* NC */ },
+       { '%', '=', 'E', 'M', 'U', '{' /* left */, 0 /* NC */ },
+       { '&', '>', 'F', 'N', 'V', '}' /* right */, 0 /* NC */ },
+       { '\'', '?', 'G', 'O', 'W', ' ', 0 /* shift */ },
+};
+#endif /* COCO_KBD */
+
+static void keydecode(void)
+{
+       uint8_t c;
+
+       if (keymap[7] & 64)     /* shift */
+               c = shiftkeyboard[keybyte][keybit];
+       else
+               c = keyboard[keybyte][keybit];
+       if (keymap[1] & 64) {   /* control */
+               if (c > 31 && c < 127)
+                       c &= 31;
+       }
+       tty_inproc(1, c);
+}
+
+void platform_interrupt(void)
+{
+       uint8_t i = *pia_ctrl;
+       if (i & 0x80) {
+               *pia_col;
+               newkey = 0;
+               keyproc();
+               if (keysdown < 3 && newkey)
+                       keydecode();
+//                fd_timer_tick();
+               timer_interrupt();
+       }
+}
+
+/* This is used by the vt asm code, but needs to live at the top of the kernel */
+uint16_t cursorpos;
+
diff --git a/Kernel/platform-coco2/devtty.h b/Kernel/platform-coco2/devtty.h
new file mode 100644 (file)
index 0000000..2d6f873
--- /dev/null
@@ -0,0 +1,12 @@
+#ifndef __DEVTTY_DOT_H__
+#define __DEVTTY_DOT_H__
+
+#define KEY_ROWS 8
+#define KEY_COLS 7
+extern uint8_t keymap[8];
+extern uint8_t keyboard[8][7];
+extern uint8_t shiftkeyboard[8][7];
+
+extern int gfx_ioctl(uint8_t minor, uarg_t arg, char *ptr);
+
+#endif
diff --git a/Kernel/platform-coco2/discard.c b/Kernel/platform-coco2/discard.c
new file mode 100644 (file)
index 0000000..1791769
--- /dev/null
@@ -0,0 +1,73 @@
+#include <kernel.h>
+#include <timer.h>
+#include <kdata.h>
+#include <printf.h>
+#include <device.h>
+#include <devtty.h>
+#include <carts.h>
+#include <blkdev.h>
+
+static const char *sysname[] = {"Dragon", "COCO", "COCO3", "Unknown"};
+
+struct cart_rom_id {
+       uint16_t hash;
+       uint8_t id;
+       const char *name;
+};
+
+static const char empty[] = "(empty)";
+
+struct cart_rom_id carts[] = {
+       { 0x72B0, CART_DRAGONDOS, "DragonDOS" },
+       { 0x9063, CART_DELTADOS, "DeltaDOS" },
+       { 0xB400, 0, empty },
+       { 0xC248, CART_RSDOS, "RS-DOS" },
+       { 0xE1BA, CART_ORCH90, "Orchestra-90 CC" },
+       { 0x0000, 0, "No ROM" }
+};
+
+static struct cart_rom_id *cart_lookup(uint16_t hash)
+{
+       struct cart_rom_id *cart = carts;
+       do {
+               if (cart->hash == hash)
+                       return cart;
+       } while(cart++->hash);
+       return NULL;
+}
+
+void map_init(void)
+{
+       uint8_t i;
+       uint8_t bslot = 0;
+       uint16_t hash;
+       struct cart_rom_id *rom;
+
+       kprintf("%s system.\n", sysname[system_id]);
+       if (mpi_present()) {
+               kputs("MPI cartridge detected.\n");
+               cartslots = 4;
+               bootslot = mpi_set_slot(0);
+               bslot = bootslot & 3;
+       }
+       for (i = 0; i < cartslots; i++) {
+               mpi_set_slot((i << 4) | i);
+               hash = cart_hash();
+               rom = cart_lookup(hash);
+               if (rom) {
+                       kprintf("%d: %s %c\n",
+                               i, rom->name,
+                               i == bootslot ? '*':' ');
+                       carttype[i] = rom->id;
+               }
+               else
+                       kprintf("%d: Unknown(%x) %c\n",
+                               i, hash,
+                               i == bslot ? '*':' ');
+       }
+       mpi_set_slot(bootslot);
+       /* We put swap on the start of slice 0, but with the first 64K free
+          so we can keep the OS image linearly there */
+       for (i = 0; i < MAX_SWAPS; i++)
+               swapmap_add(i + 128);
+}
diff --git a/Kernel/platform-coco2/drivewire.s b/Kernel/platform-coco2/drivewire.s
new file mode 100644 (file)
index 0000000..35b9aac
--- /dev/null
@@ -0,0 +1,214 @@
+;
+; DriveWire sector routines
+;
+; Copyright 2015 Tormod Volden
+; Copyright 2008 Boisy G. Pitre
+; Distributed under the GNU General Public License, version 2 or later.
+;
+
+       ; exported
+       .globl _dw_operation
+       .globl _dw_reset
+
+       .globl _dw_lpr
+       .globl _dw_lpr_close
+       .globl _dw_rtc_read
+
+
+       ; imported
+       .globl map_process_a
+       .globl map_kernel
+
+       .area .common
+
+_dw_reset:
+       ; maybe reinitalise PIA here?
+       ; and send DW_INIT request to server?
+       rts
+
+_dw_operation:
+       pshs y
+       ; get parameters from C, X points to cmd packet
+       ldy 4,s         ; driveptr (dw_tab in .bss so kernel bank)
+       ldb ,y          ; for now, contains minor = drive number directly
+       lda 5,x         ; page map
+       jsr map_process_a       ; same as map_process on nx32
+       tfr b,a
+       ldb ,x          ; write flag
+       ; buffer location into Y
+       ldy 3,x
+       ; sector number into X
+       ldx 1,x
+       tstb
+       bne @write
+       jsr dw_read_sector
+       bra @done
+@write  jsr dw_write_sector
+@done  bcs @err
+       bne @err
+       ldx #0
+@fin   jsr map_kernel
+@ret   puls y,pc
+@err   ldx #0xFFFF
+       bra @fin
+
+; Write a sector to the DriveWire server
+; Drive number in A, sector number in X, buffer location in Y
+; Sets carry or non-zero flags on error
+dw_write_sector:
+       ; header: OP, drive = A, LSN 23-16 = 0, LSN 15-8 and LSN 7-0 = X
+       clrb
+       pshs a,b,x
+       ldb #OP_WRITE
+       pshs b
+       ; send header
+       tfr s,x
+       pshs y          ; save buffer location
+       ldy #5
+       jsr DWWrite
+       ; send payload
+       ldx ,s
+       ldy #256
+       jsr DWWrite
+       ; calculate checksum of payload, backwards
+       exg x,y         ; Y is zero after DWWrite
+@sum   ldb ,-y
+       abx
+       cmpy ,s         ; buffer location start
+       bne @sum
+       stx ,s          ; checksum to send
+       tfr s,x
+       ldy #2
+       jsr DWWrite
+       ; get status byte from server into following byte
+       ldy #1
+       clra            ; clear carry bit for BECKER variant
+       jsr DWRead
+       leas 7,s
+       bcs @ret
+       bne @ret
+       ldb -5,s        ; received status byte (zero is success)
+@ret   rts
+
+;
+; Based on "DoRead" by Boisy G. Pitre from DWDOS hosted on toolshed.sf.net 
+; Read a sector from the DriveWire server
+; Drive number in A, 16-bit sector in X, buffer location in Y
+; Sets carry or non-zero flags on error
+
+dw_read_sector:
+         ; header: OP, drive = A, LSN 23-16 = 0, LSN 15-8 and LSN 7-0 = X
+         clrb
+         pshs  d,x,y
+         lda   #OP_READEX
+ReRead   pshs  a
+         leax  ,s
+        ldy   #$0005
+        lbsr  DWWrite
+        puls  a
+        ldx   4,s                      get read buffer pointer
+        ldy   #256                     read 256 bytes
+        ldd   #133*1                   1 second timeout
+        bsr   DWRead
+         bcs   ReadEx
+         bne   ReadEx
+; Send 2 byte checksum
+        pshs  y
+        leax  ,s
+        ldy   #2
+        lbsr  DWWrite
+        ldy   #1
+        ldd   #133*1
+        bsr   DWRead
+        leas  2,s
+        bcs   ReadEx
+        bne   ReadEx
+; Check received status byte
+        lda   ,s
+        beq   ReadEx
+        cmpa  #E_CRC
+        bne   ReadErr
+        lda   #OP_REREADEX
+        clr   ,s
+        bra   ReRead  
+ReadErr  comb                  ; set carry bit
+ReadEx  puls  d,x,y,pc
+
+       .area .text
+;
+;      Virtual devices on DriveWire
+;
+;      Line printer
+;      RTC
+;      Virtual serial ???
+;
+;
+;      dw_lpr(uint8_t c)
+;
+;      Print a byte to drive wire printers
+;
+;      B holds the byte. Call with interrupts off
+;
+_dw_lpr:
+       pshs y
+       stb lprb2
+       ldx #lprb
+       ldy #2
+dwop:
+       jsr DWWrite
+       puls y,pc
+
+;
+;      dw_lpr_close(void)
+;
+;      Close the printer device
+;
+_dw_lpr_close:
+       pshs y
+       ldx #lprb3
+       ldy #1
+       bra dwop
+
+;
+;      uint8_t dw_rtc_read(uint8_t *p)
+;
+_dw_rtc_read:
+       pshs y
+       ldy #2
+       pshs x
+       ldx #lprrtw
+       jsr DWWrite
+       puls x
+       ldy #6
+       clra
+       jsr DWRead
+       clrb
+       sbcb #0
+       bra dwop
+
+       .area .data
+
+lprb:  .db 0x50        ; print char
+lprb2: .db 0x00        ; filled in with byte to send
+lprb3: .db 0x46        ; printer flush
+lprrtw:        .db 0x23        ; request for time
+
+       .area .common
+
+
+; Used by DWRead and DWWrite
+IntMasks equ   $50
+NOINTMASK equ  1
+
+; Hardcode these for now so that we can use below files unmodified
+H6309    equ 0
+BECKER   equ 1
+ARDUINO  equ 0
+JMCPBCK  equ 0
+BAUD38400 equ 0
+
+; These files are copied almost as-is from HDB-DOS
+         include "dw.def"
+         include "dwread.s"
+         include "dwwrite.s"
+
diff --git a/Kernel/platform-coco2/dw.def b/Kernel/platform-coco2/dw.def
new file mode 100644 (file)
index 0000000..677f4c7
--- /dev/null
@@ -0,0 +1,70 @@
+********************************************************************
+*
+* Copied from HDB-DOS from toolshed.sf.net
+*
+* dwdefs - DriveWire Definitions File
+*
+* $Id: dwdefs.d,v 1.10 2010/02/21 06:24:47 aaronwolfe Exp $
+*
+* Ed.    Comments                                       Who YY/MM/DD
+* ------------------------------------------------------------------
+*   1    Started                                        BGP 03/04/03
+*   2    Added DWGLOBS area                             BGP 09/12/27
+
+         nam   dwdefs
+         ttl   DriveWire Definitions File
+
+* Addresses
+BBOUT       equ    $FF20
+BBIN        equ    $FF22
+
+* Opcodes
+OP_NOP      equ    $00         No-Op
+OP_RESET1   equ    $FE         Server Reset
+OP_RESET2   equ    $FF         Server Reset
+OP_RESET3   equ    $F8         Server Reset
+OP_DWINIT      equ        'Z           DriveWire dw3 init/OS9 boot
+OP_TIME     equ    '#          Current time requested
+OP_INIT     equ    'I          Init routine called
+OP_READ     equ    'R          Read one sector
+OP_REREAD   equ    'r          Re-read one sector
+OP_READEX   equ    'R+128      Read one sector
+OP_REREADEX equ    'r+128      Re-read one sector
+OP_WRITE    equ    'W          Write one sector
+OP_REWRIT   equ    'w          Re-write one sector
+OP_GETSTA   equ    'G          GetStat routine called
+OP_SETSTA   equ    'S          SetStat routine called
+OP_TERM     equ    'T          Term routine called
+OP_SERINIT  equ    'E
+OP_SERTERM  equ    'E+128
+
+* Printer opcodes
+OP_PRINT    equ    'P          Print byte to the print buffer
+OP_PRINTFLUSH equ  'F          Flush the server print buffer
+
+* Serial opcodes
+OP_SERREAD equ 'C
+OP_SERREADM equ 'c
+OP_SERWRITE equ 'C+128
+OP_SERGETSTAT equ 'D
+OP_SERSETSTAT equ 'D+128
+
+* for dw vfm
+OP_VFM equ 'V+128
+
+* WireBug opcodes (Server-initiated)
+OP_WIREBUG_MODE  equ   'B
+* WireBug opcodes (Server-initiated)
+OP_WIREBUG_READREGS   equ  'R  Read the CoCo's registers
+OP_WIREBUG_WRITEREGS  equ  'r  Write the CoCo's registers
+OP_WIREBUG_READMEM    equ  'M  Read the CoCo's memory
+OP_WIREBUG_WRITEMEM   equ  'm  Write the CoCo's memory
+OP_WIREBUG_GO         equ  'G  Tell CoCo to get out of WireBug mode and continue execution
+
+* VPort opcodes (CoCo-initiated)
+OP_VPORT_READ         equ  'V
+OP_VPORT_WRITE        equ  'v
+
+* Error definitions
+E_CRC      equ   $F3            Same as NitrOS-9 E$CRC
+
diff --git a/Kernel/platform-coco2/fuzix.link b/Kernel/platform-coco2/fuzix.link
new file mode 100644 (file)
index 0000000..9674fcb
--- /dev/null
@@ -0,0 +1,16 @@
+define basesympat __sectionbase_%s__
+define lensympat __sectionlen_%s__
+section .istack load 0x0000
+section .vectors load 0x0100
+section .udata load 0xFD00
+section .start load 0x0400
+section .text
+section .text2
+section .bss
+section .video load 0xF000
+section .data
+section .videodata
+section .common
+section .text3
+section .discard load 0x9000
+entry start
diff --git a/Kernel/platform-coco2/ide.s b/Kernel/platform-coco2/ide.s
new file mode 100644 (file)
index 0000000..8a5d8cb
--- /dev/null
@@ -0,0 +1,62 @@
+;
+;      Glenside style IDE block transfer logic
+;
+
+       .module dragonide
+
+       .globl _devide_read_data
+       .globl _devide_write_data
+
+       .globl _blk_op
+
+        include "kernel.def"
+        include "../kernel09.def"
+
+       .area .common
+
+;
+;      Standard mapping for Glenside style IDE
+;
+;      We don't really support swap properly but what we do is sufficient
+;      for a simple memory mapping.
+;
+_devide_read_data:
+       pshs y,dp
+       lda #0xFF
+       tfr a,dp
+       ldx _blk_op
+       leay 512,x
+       sty endp
+       tst _blk_op+2
+       beq readword
+       jsr map_process_always
+readword:
+       lda <IDEDATA
+       ldb <IDEDATA_L          ; latched
+       std ,x++
+       cmpx endp
+       bne readword
+       jsr map_kernel
+       puls y,dp,pc
+
+_devide_write_data:
+       pshs y,dp
+       lda #0xFF
+       tfr a,dp
+       ldx _blk_op
+       leay 512,x
+       sty endp
+       tst _blk_op+2
+       beq writeword
+       jsr map_process_always
+writeword:
+       ldd ,x++
+       stb <IDEDATA_L
+       sta <IDEDATA
+       cmpx endp
+       bne writeword
+       jsr map_kernel
+       puls y,dp,pc
+
+endp:  .dw 0
+
diff --git a/Kernel/platform-coco2/kernel.def b/Kernel/platform-coco2/kernel.def
new file mode 100644 (file)
index 0000000..ecfc928
--- /dev/null
@@ -0,0 +1,26 @@
+; UZI mnemonics for memory addresses etc
+
+U_DATA                      equ 0xFD00       ; (this is struct u_data from kernel.h)
+U_DATA__TOTALSIZE           equ 0x0200        ; 256+256
+
+VIDEO_BASE                 equ 0x0400       ; 512 bytes for the display
+VIDEO_END                  equ 0x0600
+
+IDEDATA                            equ 0xFF50
+IDEDATA_L                  equ 0xFF58
+
+PROGBASE                    equ 0x8000       ; programs and data start here
+
+; Have these defined so that lowlevel-6809.s can be used as-is
+SAM_USER macro
+       endm
+
+SAM_KERNEL macro
+       endm
+
+SAM_SAVE macro
+       endm
+
+SAM_RESTORE macro
+       endm
+
diff --git a/Kernel/platform-coco2/libc.c b/Kernel/platform-coco2/libc.c
new file mode 100644 (file)
index 0000000..a4cb9dd
--- /dev/null
@@ -0,0 +1,25 @@
+#include "cpu.h"
+
+void *memcpy(void *d, const void *s, size_t sz)
+{
+  unsigned char *dp = d;
+  const unsigned char *sp = s;
+  while(sz--)
+    *dp++=*sp++;
+  return d;
+}
+
+void *memset(void *d, int c, size_t sz)
+{
+  unsigned char *p = d;
+  while(sz--)
+    *p++ = c;
+  return d;
+}
+
+size_t strlen(const char *p)
+{
+  const char *e = p;
+  while(*e++);
+  return e-p-1;
+}
diff --git a/Kernel/platform-coco2/main.c b/Kernel/platform-coco2/main.c
new file mode 100644 (file)
index 0000000..4455331
--- /dev/null
@@ -0,0 +1,46 @@
+#include <kernel.h>
+#include <timer.h>
+#include <kdata.h>
+#include <printf.h>
+#include <device.h>
+#include <devtty.h>
+#include <carts.h>
+#include <blkdev.h>
+
+uint8_t membanks;
+uint8_t system_id;
+uint8_t cartslots = 1;
+uint8_t carttype[4];
+uint8_t bootslot = 0;
+uint16_t swap_dev;
+
+void platform_idle(void)
+{
+}
+
+void do_beep(void)
+{
+}
+
+void platform_discard(void)
+{
+}
+
+/* Find a cartridge or it's slot */
+int cart_find(int id)
+{
+       int i;
+       for (i = 0; i < id; i++) {
+               if (carttype[i] == id)
+                       return i;
+       }
+       return -1;
+}
+
+unsigned char vt_mangle_6847(unsigned char c)
+{
+       if (c >= 96)
+               c -= 32;
+       c &= 0x3F;
+       return c;
+}
diff --git a/Kernel/platform-coco2/mini_ide.c b/Kernel/platform-coco2/mini_ide.c
new file mode 100644 (file)
index 0000000..315c7b0
--- /dev/null
@@ -0,0 +1,115 @@
+#include <kernel.h>
+#include <kdata.h>
+#include <printf.h>
+#include "mini_ide.h"
+
+#define MAX_HD         2
+
+uint8_t ide_present = 1;
+
+#define data   ((volatile uint8_t *)0xFF50)
+#define error  ((volatile uint8_t *)0xFF51)
+#define count  ((volatile uint8_t *)0xFF52)
+#define sec    ((volatile uint8_t *)0xFF53)
+#define cyll   ((volatile uint8_t *)0xFF54)
+#define cylh   ((volatile uint8_t *)0xFF55)
+#define devh   ((volatile uint8_t *)0xFF56)
+#define status ((volatile uint8_t *)0xFF57)
+#define cmd    ((volatile uint8_t *)0xFF57)
+#define datal  ((volatile uint8_t *)0xFF58)
+
+/* FIXME: switch to the correct mpi slot on entry */
+static int ide_transfer(uint8_t minor, bool is_read, uint8_t rawflag)
+{
+    blkno_t block;
+    uint8_t *dptr;
+    uint8_t nblock;
+
+    if (rawflag == 0) {
+        dptr = udata.u_buf->bf_data;
+        block = udata.u_buf->bf_blk;
+        nblock = 1;
+    } else if (rawflag == 1) {
+        if (((uint16_t)udata.u_offset|udata.u_count) & BLKMASK)
+            goto bad2;
+        dptr = udata.u_base;
+        block = udata.u_offset >> 9;
+        nblock = udata.u_count >> 9;
+    } else {
+       nblock = swapcnt >> 9;  /* in 512 byte chunks */
+       dptr = swapbase;
+       block = swapblk;
+    }
+    
+    while(*status & 0x80);     /* Wait !BUSY */
+    *devh = (minor & 0x80) ? 0x50 : 0x40 ;     /* LBA, device */
+    while(*status & 0x80);     /* Wait !BUSY */
+    /* FIXME - slices of about 4MB might be saner! */
+    *cylh = minor & 0x7F;      /* Slice number */
+    *cyll = block >> 8;                /* Each slice is 32MB */
+    *sec = block & 0xFF;
+    *count = nblock;
+    while(!(*status & 0x40));  /* Wait DRDY */
+    *cmd = is_read ? 0x20 : 0x30;
+    while(*status & 0x08);     /* Wait DRQ */
+    while(nblock--) {
+        unsigned int i;
+        while(!(*status & 0x08));      /* Wait DRQ */
+        if (is_read) {
+            for (i = 0; i < 256; i++) {
+                *dptr++ = *data;
+                *dptr++ = *datal;
+            }
+        } else {
+            for (i = 0; i < 256; i++) {
+                *datal = dptr[1];
+                *data = *dptr++;
+                dptr++;
+            }
+        }
+    }
+    while(*status & 0x80);     /* Wait !BUSY */
+    if (*status & 0x01) {      /* Error */
+        kprintf("ide%d: error %x\n", *error);
+bad2:
+        udata.u_error = EIO;
+        return -1;
+    }
+    return nblock;
+
+}
+
+int ide_open(uint8_t minor, uint16_t flag)
+{
+    if(minor > 1 || !(ide_present & (1 << minor))) {
+        udata.u_error = ENODEV;
+        return -1;
+    }
+    return 0;
+}
+
+int ide_read(uint8_t minor, uint8_t rawflag, uint8_t flag)
+{
+    return ide_transfer(minor, true, rawflag);
+}
+
+int ide_write(uint8_t minor, uint8_t rawflag, uint8_t flag)
+{
+    return ide_transfer(minor, false, rawflag);
+}
+
+int ide_ioctl(uint8_t minor, uarg_t request, char *unused)
+{
+    if (request != BLKFLSBUF)
+        return -1;
+    while(*status & 0x80);     /* Wait !BUSY */
+    *devh = (minor & 0x80) ? 0x10 : 0x40 ;     /* LBA, device */
+    while(*status & 0x80);     /* Wait !BUSY */
+    while(!(*status & 0x40));  /* Wait DRDY */
+    *cmd = 0xE7;
+    while(*status & 0x80);     /* Wait !BUSY */
+    return 0;
+}
+
+/* TODO: probe the devices more carefully and do EDD and LBA checks in discard
+   code */
diff --git a/Kernel/platform-coco2/mini_ide.h b/Kernel/platform-coco2/mini_ide.h
new file mode 100644 (file)
index 0000000..a72b491
--- /dev/null
@@ -0,0 +1,6 @@
+extern int ide_open(uint8_t minor, uint16_t flag);
+extern int ide_read(uint8_t minor, uint8_t rawflag, uint8_t flag);
+extern int ide_write(uint8_t minor, uint8_t rawflag, uint8_t flag);
+extern int ide_ioctl(uint8_t minor, uarg_t request, char *data);
+
+extern uint8_t ide_present;
diff --git a/Kernel/platform-coco2/mini_ide_discard.c b/Kernel/platform-coco2/mini_ide_discard.c
new file mode 100644 (file)
index 0000000..49eff53
--- /dev/null
@@ -0,0 +1,97 @@
+#include <kernel.h>
+#include <kdata.h>
+#include <printf.h>
+#include <timer.h>
+#include "mini_ide.h"
+
+#define data   ((volatile uint8_t *)0xFF50)
+#define error  ((volatile uint8_t *)0xFF51)
+#define count  ((volatile uint8_t *)0xFF52)
+#define sec    ((volatile uint8_t *)0xFF53)
+#define cyll   ((volatile uint8_t *)0xFF54)
+#define cylh   ((volatile uint8_t *)0xFF55)
+#define devh   ((volatile uint8_t *)0xFF56)
+#define status ((volatile uint8_t *)0xFF57)
+#define cmd    ((volatile uint8_t *)0xFF57)
+#define datal  ((volatile uint8_t *)0xFF58)
+
+static timer_t giveup;
+
+static int ide_wait_op(uint8_t mask, uint8_t val)
+{
+       while((*status & mask) != val) {
+               if (timer_expired(giveup)) {
+                       kputs(" - no response\n");
+                       return -1;
+               }
+       }
+       return 0;
+}
+
+static int ide_wait_nbusy(void)
+{
+       return ide_wait_op(0x80, 0x00);
+}
+
+static int ide_wait_drdy(void)
+{
+       return ide_wait_op(0x40, 0x40);
+}
+
+static int ide_wait_drq(void)
+{
+       return ide_wait_op(0x08, 0x08);
+}
+
+
+static void ide_identify(int dev, uint8_t *buf)
+{
+       uint8_t *dptr = buf;
+        int i;
+
+       giveup = set_timer_ms(2000);
+
+       if (ide_wait_nbusy() == -1)
+               return;
+       *devh = dev << 4;       /* Select */
+       if (ide_wait_nbusy() == -1)
+               return;
+       if (ide_wait_drdy() == -1)
+               return;
+       *cmd = 0xEC;    /* Identify */
+       if (ide_wait_drq() == -1)
+               return;
+        for (i = 0; i < 256; i++) {
+               *dptr++ = *data;
+               *dptr++ = *datal;
+       }
+       if (ide_wait_nbusy() == -1)
+               return;
+       if (*status & 1)
+               return;
+       if (ide_wait_drdy() == -1)
+               return;
+       kprintf("%x : ", dev);
+       /* Check the LBA bit is set, and print the name */
+       dptr = buf + 54;                /* Name info */
+       if (*dptr)
+               for (i = 0; i < 40; i++)
+                       kputchar(*dptr++);
+       if (!(buf[99] & 0x02)) {        /* No LBA ? */
+               kputs(" - non-LBA\n");
+               return;
+       }
+       kputs(" - OK\n");
+       ide_present |= (1 << dev);
+}
+
+int ide_probe(void)
+{
+       uint8_t *buf = (uint8_t *)tmpbuf();
+       /* Issue an EDD if we can - timeout -> no drives */
+       /* Now issue an identify for each drive */
+       ide_identify(0, buf);
+       if (ide_present)
+               ide_identify(1, buf);
+       brelse((bufptr) buf);
+}
diff --git a/Kernel/platform-coco2/target.mk b/Kernel/platform-coco2/target.mk
new file mode 100644 (file)
index 0000000..e9abbba
--- /dev/null
@@ -0,0 +1 @@
+export CPU = 6809
diff --git a/Kernel/platform-coco2/tricks.s b/Kernel/platform-coco2/tricks.s
new file mode 100644 (file)
index 0000000..bb43618
--- /dev/null
@@ -0,0 +1,183 @@
+;
+;      TODO: udata needs to be in common space so swap needs to be smarter
+;      about split udata
+;
+        .module tricks
+
+       # imported
+        .globl _newproc
+        .globl _chksigs
+        .globl _getproc
+        .globl _trap_monitor
+        .globl _inint
+        .globl map_kernel
+        .globl map_process
+        .globl map_process_a
+        .globl map_process_always
+        .globl copybank
+       .globl _nready
+       .globl _platform_idle
+
+       # exported
+        .globl _switchout
+        .globl _switchin
+        .globl _dofork
+       .globl _ramtop
+
+        include "kernel.def"
+        include "../kernel09.def"
+
+       .area .common
+
+       ; ramtop must be in common although not used here
+_ramtop:
+       .dw 0
+
+; Switchout switches out the current process, finds another that is READY,
+; possibly the same process, and switches it in.  When a process is
+; restarted after calling switchout, it thinks it has just returned
+; from switchout().
+;
+; 
+; This function can have no arguments or auto variables.
+_switchout:
+       orcc #0x10              ; irq off
+        jsr _chksigs
+
+        ; save machine state, including Y and U used by our C code
+        ldd #0 ; return code set here is ignored, but _switchin can 
+        ; return from either _switchout OR _dofork, so they must both write 
+        ; U_DATA__U_SP with the following on the stack:
+       pshs d,y,u
+       sts U_DATA__U_SP        ; this is where the SP is restored in _switchin
+
+        ; find another (or same) process to run, returned in X
+        jsr _getproc
+        jsr _switchin
+        ; we should never get here
+        jsr _trap_monitor
+
+badswitchmsg:
+       .ascii "_switchin: FAIL"
+        .db 13
+       .db 10
+       .db 0
+
+newpp   .dw 0
+
+; new process pointer is in X
+_switchin:
+        orcc #0x10             ; irq off
+
+       stx newpp
+       ; get process table
+       lda P_TAB__P_PAGE_OFFSET+1,x            ; LSB of 16-bit page no
+
+       cmpa #0
+       bne not_swapped
+       jsr _swapper            ; void swapper(ptptr p)
+       ldx newpp
+       lda P_TAB__P_PAGE_OFFSET+1,x
+
+not_swapped:
+       ; we have now new stacks so get new stack pointer before any jsr
+       lds U_DATA__U_SP
+
+       ; get back kernel page so that we see process table
+       jsr map_kernel
+
+       ldx newpp
+        ; check u_data->u_ptab matches what we wanted
+       cmpx U_DATA__U_PTAB
+        bne switchinfail
+
+       lda #P_RUNNING
+       sta P_TAB__P_STATUS_OFFSET,x
+
+       ; fix any moved page pointers
+       lda P_TAB__P_PAGE_OFFSET+1,x
+       sta U_DATA__U_PAGE+1
+
+       ldx #0
+       stx _runticks
+
+        ; restore machine state -- note we may be returning from either
+        ; _switchout or _dofork
+        lds U_DATA__U_SP
+        puls x,y,u ; return code and saved U and Y
+
+        ; enable interrupts, if the ISR isn't already running
+       lda U_DATA__U_ININTERRUPT
+        bne swtchdone ; in ISR, leave interrupts off
+       andcc #0xef
+swtchdone:
+        rts
+
+switchinfail:
+       jsr outx
+        ldx #badswitchmsg
+        jsr outstring
+       ; something went wrong and we didn't switch in what we asked for
+        jmp _trap_monitor
+
+       .area .data
+
+fork_proc_ptr: .dw 0 ; (C type is struct p_tab *) -- address of child process p_tab entry
+
+       .area .common
+;
+;      Called from _fork. We are in a syscall, the uarea is live as the
+;      parent uarea. The kernel is the mapped object.
+;
+_dofork:
+        ; always disconnect the vehicle battery before performing maintenance
+        orcc #0x10      ; should already be the case ... belt and braces.
+
+       ; new process in X, get parent pid into y
+
+       stx fork_proc_ptr
+       ldx P_TAB__P_PID_OFFSET,x
+
+        ; Save the stack pointer and critical registers (Y and U used by C).
+        ; When this process (the parent) is switched back in, it will be as if
+        ; it returns with the value of the child's pid.
+        pshs x,y,u ;  x has p->p_pid from above, the return value in the parent
+
+        ; save kernel stack pointer -- when it comes back in the parent we'll be in
+        ; _switchin which will immediately return (appearing to be _dofork()
+       ; returning) and with X (ie return code) containing the child PID.
+        ; Hurray.
+        sts U_DATA__U_SP
+
+        ; now we're in a safe state for _switchin to return in the parent
+       ; process.
+
+       ldx U_DATA__U_PTAB
+       jsr _swapout
+       cmpd #0
+       bne forked_up
+
+       ; We are now in the kernel child context
+
+        ; now the copy operation is complete we can get rid of the stuff
+        ; _switchin will be expecting from our copy of the stack.
+       puls x
+
+        ldx fork_proc_ptr
+        jsr _newproc
+
+       ; any calls to map process will now map the childs memory
+
+        ; in the child process, fork() returns zero.
+       ldx #0
+        ; runticks = 0;
+       stx _runticks
+       ;
+       ; And we exit, with the kernel mapped, the child now being deemed
+       ; to be the live uarea. The parent is frozen in time and space as
+       ; if it had done a switchout().
+       puls y,u,pc
+
+forked_up:     ; d is already -1
+       puls y,u,pc
+       rts
\ No newline at end of file