changeset 22:06417341c50e

Add memory size detection (coco1/2) and rearrange variables some Add memory size detection using the same algorithm used by the stock ROM, but only do so for the Coco1/2. For the Coco3, just set an arbitrary top of RAM just below the "ROM" area. Also shift the line input buffer below the text screen just to keep everything in one sequence.
author William Astle <lost@l-w.ca>
date Wed, 09 Nov 2022 18:09:19 -0700
parents 2264569d7aca
children 4b4806a32701
files src/lwbasic.s
diffstat 1 files changed, 32 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/lwbasic.s	Tue Nov 08 22:29:47 2022 -0700
+++ b/src/lwbasic.s	Wed Nov 09 18:09:19 2022 -0700
@@ -194,6 +194,7 @@
 ; Start of memory which has the direct page and other data.
                 org 0
 dpstart         equ *                           ; start of direct page
+memtop          rmb 2                           ; absolute top of memory in 64K memory map
 readlinenoecho  rmb 1                           ; if nonzero, the readline routine won't echo its input
                 rmb 0x71-*                      ; align RSTFLG/RSTVEC for stock ROM compatibility
 RSTFLG          rmb 1                           ; 0x55 if RSTVEC is valid
@@ -217,10 +218,14 @@
 FRQVEC          rmb 3                           ; FIRQ vector (for compatibility)
 keyb_state      rmb 8                           ; rollover table state
 keyb_buff       rmb keyb_bufflen                ; the keyboard ring buffer
-                rmb 0x200-*                     ; unused
+linebuff        rmb linebuffsize                ; the line input buffer
+                ifne *&0x1ff
+                rmb 0x200-(*&0x1ff)
+                endc
 textscreen      rmb 0x200                       ; the actual text screen (must be on 512 byte alignment)
-linebuff        rmb linebuffsize                ; the line input buffer
+heapstart       equ *                           ; start of dynamically allocated stuff
                 org 0x8000                      ; the hardware puts the ROMs here; it's not negotiable
+ROMSTART        equ *
 START           orcc #0x50                      ; make sure interrupts are disabled if we come here in an unusual way
                 ifdef COCO3
                 ldu #gime_inite                 ; point to end of GIME initializer
@@ -406,6 +411,31 @@
 coldstart0      std ,x++                        ; blank a couple of bytes
                 cmpx #textscreen                ; end of low memory?
                 blo coldstart0                  ; brif not
+                ifndef COCO3
+; This is the memory size detection sequence. This runs through memory starting at the bottom of memory
+; and stops when it reaches something that can't be modified successfully. This is basically the same
+; algorithm used by the stock ROM. It takes less space than doing a more pointed set of probes. The end
+; result will be X pointing to the byte one below the top of RAM. This is intentional to ensure there
+; is one writeable byte at the top of string space. Note that X will point to the byte after the end
+; of the text screen when we get here.
+                ldx #heapstart                  ; point to start of heap
+coldstart1      lda 2,x                         ; get original value at test location
+                coma                            ; invert all bits
+                sta 2,x                         ; write it to the memory location
+                cmpa 2,x                        ; did it take?
+                bne coldstart2                  ; brif not
+                com 2,x                         ; restore memory byte
+                leax 1,x                        ; move pointer forward
+                bra coldstart1                  ; go check next byte
+                else
+; For the Coco3, we do not need to concern ourselves about where the top actual memory is so we don't
+; bother doing a memory scan in the default 64K memory map. Because we always run from RAM, we can actually
+; set the top of memory to the actual top of the 32K space without having to ensure there is an extra byte
+; available above the string space.
+                ldx #ROMSTART-1                 ; point to top of memory
+                endc
+coldstart2      stx memtop                      ; save absolute top of memory
+                leas ,x                         ; put the stack there
                 ldx #warmstart                  ; set up warm start handler
                 stx RSTVEC
                 lda #0x55                       ; activate warm start handler