Kernel: zeta-v2: Simplify and improve boot code.
authorWill Sowerbutts <will@sowerbutts.com>
Fri, 6 Jan 2017 19:07:04 +0000 (19:07 +0000)
committerWill Sowerbutts <will@sowerbutts.com>
Sun, 8 Jan 2017 23:05:11 +0000 (23:05 +0000)
The kernel image is unlikely to grow larger than 48KB so we can
simplyify the process for relocating the kernel to the correct memory
banks when booting.

Now that we have ROM disk support we can drop copying the RAM disk
contents on boot (userspace could do this by reading from /dev/physmem
or /dev/rd1 if it were still desirable).

We now stop the floppy drive motors on boot so they don't spin forever
where we have booted from floppy disk.

Kernel/platform-zeta-v2/bootrom.s
Kernel/platform-zeta-v2/crt0.s
Kernel/platform-zeta-v2/main.c
Kernel/platform-zeta-v2/zeta-v2.s

index 4086a78..05c85dd 100644 (file)
@@ -29,21 +29,21 @@ kernel_copy:
                cp #4                   ; are we there yet (RAM page == 4?)
                jr nz,kernel_copy
 
-               ; copy data to RAM disk
-               ; 16 pages, starting from ROM page 4, RAM page 48
-               ld a,#4
-ramdisk_copy:
-               out (MPGSEL_1),a        ; map ROM page to bank #1
-               add #44                 ; RAM page = ROM page + 44
-               out (MPGSEL_2),a        ; map RAM page to bank #2
-               ld hl,#0x4000           ; source - bank #1 offset
-               ld de,#0x8000           ; destination - bank #2 offset
-               ld bc,#0x4000           ; count - 16 KiB
-               ldir                    ; copy it
-               sub #43                 ; next ROM page = RAM page - 44 + 1
-               cp #20                  ; are we there yet (RAM page == 4 + 16?)
-               jr nz,ramdisk_copy
-
+;;             ; copy data to RAM disk
+;;             ; 16 pages, starting from ROM page 4, RAM page 48
+;;             ld a,#4
+;; ramdisk_copy:
+;;             out (MPGSEL_1),a        ; map ROM page to bank #1
+;;             add #44                 ; RAM page = ROM page + 44
+;;             out (MPGSEL_2),a        ; map RAM page to bank #2
+;;             ld hl,#0x4000           ; source - bank #1 offset
+;;             ld de,#0x8000           ; destination - bank #2 offset
+;;             ld bc,#0x4000           ; count - 16 KiB
+;;             ldir                    ; copy it
+;;             sub #43                 ; next ROM page = RAM page - 44 + 1
+;;             cp #20                  ; are we there yet (RAM page == 4 + 16?)
+;;             jr nz,ramdisk_copy
+;; 
                ; scary... switching memory bank under our feet
                ld a,#32                ; map page 32 (RAM) to bank 0 
                out (MPGSEL_0),a
index 285a8c4..7183d9c 100644 (file)
@@ -32,7 +32,6 @@
 
         ; imported symbols
         .globl _fuzix_main
-        .globl init_early
         .globl init_hardware
         .globl s__INITIALIZER
         .globl s__COMMONMEM
@@ -55,7 +54,36 @@ init_from_rom:              ; must be at 0x8B -- bootrom.s enters here
         ; fall through
 init_common:
         di
+        ld (_boot_from_rom), a
+        or a
+        jr nz, mappedok     ; bootrom.s loads us in the correct pages
+
+        ; move kernel to the correct location in RAM
+        ; note that this cannot cope with kernel images larger than 48KB
+        ld hl, #0x0000
+        ld a, #32               ; first page of RAM is page 32
+movenextbank:
+        out (MPGSEL_3), a       ; map page at 0xC000 upwards
+        ld de, #0xC000
+        ld bc, #0x4000          ; copy 16KB
+        ldir
+        inc a
+        cp #35                  ; done three pages?
+        jr nz, movenextbank
+
+       ; setup the memory paging for kernel
+        out (MPGSEL_3), a       ; map page 35 at 0xC000
+        ld a, #32
+        out (MPGSEL_0), a       ; map page 32 at 0x0000
+        inc a
+        out (MPGSEL_1), a       ; map page 33 at 0x4000
+        inc a
+        out (MPGSEL_2), a       ; map page 34 at 0x8000
+
+mappedok:
+        ; switch to stack in high memory
         ld sp, #kstack_top
+
         ; move the common memory where it belongs    
         ld hl, #s__DATA
         ld de, #s__COMMONMEM
@@ -71,12 +99,6 @@ init_common:
         ld bc, #l__DATA - 1
         ld (hl), #0
         ldir
-        
-        ; save ROM boot flag
-        ld (_boot_from_rom), a
-
-       ; setup the rest of the memory paging
-       call init_early
 
         ; Hardware setup
         call init_hardware
@@ -89,5 +111,4 @@ init_common:
 stop:   halt
         jr stop
 
-        .area _DATA
-_boot_from_rom: .ds 1
+_boot_from_rom: .db 0
index 3832bd1..7897cc5 100644 (file)
@@ -20,8 +20,6 @@ void platform_discard(void)
         bufpool_end->bf_dev = NO_DEVICE;
         bufpool_end++;
     }
-
-    kprintf("platform_discard: bufpool=%x, bufpool_end=%x\n", bufpool, bufpool_end);
 }
 
 void platform_idle(void)
index 2a64dee..644cc53 100644 (file)
@@ -1,10 +1,9 @@
 ; 2014-02-19 Sergey Kiselev
 ; Zeta SBC V2 hardware specific code
 
-        .module zeta-v2
+        .module zeta_v2
 
         ; exported symbols
-        .globl init_early
         .globl init_hardware
        .globl _program_vectors
        .globl map_kernel
@@ -56,88 +55,6 @@ _bufpool:
 ; Initialization code
 ;=========================================================================
         .area _DISCARD
-
-init_early:
-        ; We want to be at the bottom of the RAM in the physical address space.
-        ; This may or may not alredy be the case. We can't read out the current
-        ; values of the memory banking registers.
-
-        ; We are going to assume that we are loaded in either one 64K chunk
-        ; or two 32K chunks, and that this code, plus the stack, live in the
-        ; top 16K bank. First we find ourselves in physical memory.
-        ld hl, #0x0000      ; go looking for bank 0
-        call searchpage     ; find it
-        ld d, a             ; store mapping for low 32K
-        ld hl, #0x8000      ; now go looking for bank 3
-        call searchpage     ; find it
-        ld e, a             ; store mapping for top 64K
-        push de
-
-        ; Now we copy ourselves to the right place in physical memory.
-        ld a, #32           ; target = first page of RAM
-        call copy32k        ; copy first 32K
-        pop de              ; recover memory mapping
-        ld d, e             ; now for the next 32K
-        ld a, #34           ; target = third page of RAM
-        call copy32k        ; copy second 32K
-
-        ; Finally remap all four 16K banks to the bottom of physical memory.
-        ld a, #32           ; first page of RAM
-        ld c, #MPGSEL_0     ; first bank register
-mappage:
-        out (c), a          ; map
-        inc c               ; next bank
-        inc a               ; next page
-        cp #36              ; done last page?
-        ret z
-        jr mappage
-
-copy32k:
-        out (MPGSEL_1), a
-        inc a
-        push af             ; store next RAM page
-        ld a, d             ; setup source
-        out (MPGSEL_0), a
-        call copy16k
-        inc a
-        out (MPGSEL_0), a   ; source is now second half
-        pop af              ; recover next RAM page
-        out (MPGSEL_1), a
-        ; fall through to copy16k
-copy16k:                    ; copy bank 0 to bank 1
-        ld hl, #0x0000
-        ld de, #0x4000
-        ld bc, #0x4000
-        ldir
-        ret
-
-searchpage:                 ; search for a page of virtual memory in physical memory
-        ld a, (hl)          ; read a byte
-        ld c, a             ; save it
-        ld b, #32           ; first 512K is ROM on Zeta2
-checkpage:
-        ld a, #0x55         ; known value
-        ld (hl), a          ; write it at a fixed address
-        ld a, b             ; map the page
-        out (MPGSEL_1), a   ; at 0x4000
-        ld a, (0x4000)      ; read test byte from remapped memory
-        cp #0x55            ; found it?
-        jr nz, nextpage     ; no
-        ld a, #0x42         ; test with another value (possibly it was 0x55 by chance)
-        ld (hl), a          ; write it
-        ld a, (0x4000)      ; read it
-        cp #0x42            ; found it?
-        jr z, foundpage     ; we got one!
-nextpage:
-        inc b               ; next page
-        jr nz, checkpage    ; check next page -- note we test pages above 1MB too
-        halt                ; wrapped to page 0, not found it: shouldn't happen
-foundpage:
-        ld a, c             ; get back the byte
-        ld (hl), a          ; put it back before anyone notices
-        ld a, b             ; return with page number in A
-       ret
-
 init_hardware:
         ; program vectors for the kernel
         ld hl, #0
@@ -145,6 +62,10 @@ init_hardware:
         call _program_vectors
         pop hl
 
+        ; Stop floppy drive motors
+        ld a, #0x0C
+        out (FDC_DOR), a
+
        ; initialize UART0
         ld a, (_boot_from_rom)          ; do not set the baud rate and other
         or a                            ; serial line parameters if the BIOS