Skip to content

Commit

Permalink
Simplify buffer allocation in parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmsc committed Jul 26, 2024
1 parent 5ea01b8 commit 91b2f19
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 60 deletions.
35 changes: 17 additions & 18 deletions src/alloc.asm
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
.export alloc_prog, alloc_laddr
.export parser_alloc_init, alloc_area_8

.importzp prog_ptr, laddr_ptr, mem_end, var_buf, tmp1
.importzp prog_ptr, laddr_ptr, mem_end, var_buf, tmp1, end_ptr
.import move_dwn, err_nomem
.importzp move_dwn_src, move_dwn_dst

Expand All @@ -83,6 +83,16 @@ alloc_size= tmp1
; Following routines are part of the parser/editor
.code

; Increase program memory area by "opos" (size in bytes)
.proc alloc_prog
.importzp opos
lda opos
ldx #prog_ptr - mem_start
.assert prog_ptr = mem_start, error, "Prog Ptr should be at mem start"
stx opos
.byte $2C ; Skip 2 bytes over next "LDX"
.endproc ; Fall through

.proc alloc_laddr
ldx #laddr_ptr - mem_start
.endproc ; Fall through
Expand Down Expand Up @@ -152,30 +162,19 @@ skip:
;----------------------------------------------------------
; Parser initialization here:
.proc parser_alloc_init
; Init all pointers to AY
ldx #(mem_end-mem_start)
; Init all pointers to AY, starting from "end_ptr"
ldx #(mem_end-end_ptr)
; Adds 256 bytes as program buffer
iny
loop:
sta mem_start, x
sty mem_start+1, x
sta end_ptr, x
sty end_ptr+1, x
dex
dex
bpl loop
; Remove 256 bytes at program start
; Restore correct value of program start and end of source
dec prog_ptr+1
rts
.endproc

; Increase program memory area by "opos" (size in bytes)
.proc alloc_prog
.importzp opos
; Move from "prog_ptr", up by "alloc_size"
ldx #prog_ptr - mem_start
.assert prog_ptr = mem_start, error, "Prog Ptr should be at mem start"
lda opos
stx opos
bne alloc_area_8
dec end_ptr+1
rts
.endproc

Expand Down
3 changes: 2 additions & 1 deletion src/cmdline.bas
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ PROC CompileFile
poke MemEnd, $9B
? "Compiling..."
dpoke @@RELOC_OFFSET, @BYTECODE_ADDR - MemEnd
if USR( @compile_buffer, Adr(MemStart), MemEnd)
dpoke @@BUF_PTR, Adr(MemStart)
if USR( @compile_buffer, MemEnd)
' Parse error, show
? " at line "; dpeek(@@linenum); " column "; peek( @@bmax )
else
Expand Down
10 changes: 2 additions & 8 deletions src/cmdmenu.asm
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,19 @@
; Exported to CMDLINE.BAS
BMAX=bmax
LINENUM=linenum
.exportzp RELOC_OFFSET
.exportzp RELOC_OFFSET, BUF_PTR
RELOC_OFFSET = reloc_addr
BUF_PTR = buf_ptr

.code
; Called from editor
COMPILE_BUFFER:

; Buffer end pointer
pla
sta end_ptr+1
tay
pla
sta end_ptr
jsr parser_alloc_init
; Buffer address
pla
sta buf_ptr+1
pla
sta buf_ptr

; Save our CPU return stack
lda saved_cpu_stack
Expand Down
11 changes: 2 additions & 9 deletions src/compile.asm
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
; Exported to EDITOR.BAS
BMAX=bmax
LINENUM=linenum
.exportzp RELOC_OFFSET
.exportzp RELOC_OFFSET, BUF_PTR
RELOC_OFFSET = reloc_addr

BUF_PTR = buf_ptr

; Our BREAK key handler, placed in DATA segment, as it is modified
; during runtime.
Expand All @@ -71,16 +71,9 @@ COMPILE_BUFFER:

; Buffer end pointer
pla
sta end_ptr+1
tay
pla
sta end_ptr
jsr parser_alloc_init
; Buffer address
pla
sta buf_ptr+1
pla
sta buf_ptr

; Save our CPU return stack
lda saved_cpu_stack
Expand Down
3 changes: 2 additions & 1 deletion src/editor.bas
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ ENDPROC
PROC CompileFile
' Compile main file
? "Parsing: ";
if USR( @compile_buffer, Adr(MemStart), MemEnd)
dpoke @@BUF_PTR, Adr(MemStart)
if USR( @compile_buffer, MemEnd)
' Parse error, go to error line
topLine = dpeek(@@linenum) - 11
column = peek( @@bmax )
Expand Down
34 changes: 14 additions & 20 deletions src/memptr.asm
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,33 @@
; linked into a combine executable.)


; Memory functions
; ----------------
; Parser Memory Areas
; -------------------

.exportzp prog_ptr, array_ptr, var_buf, var_ptr, mem_end
.exportzp label_buf, label_ptr, laddr_buf, laddr_ptr
.exportzp label_buf, label_ptr, laddr_buf, laddr_ptr, end_ptr

.zeropage

; Note that the memory pointers are shared between parser and runtime,
; so that less zeropage memory is used.
;
; During Parsing During Execution
; ---------------------------------------------
; Pointer to program buffer / UNUSED
mem_start:
; End of program to parse
end_ptr: .res 2
; Pointer to end of program buffer
prog_ptr: .res 2
; Pointer to variable name table / variable value table
; Pointers to start / end of variable name table
var_buf: .res 2
var_ptr= array_buf
; Pointer to labels name table / strings/arrays table
array_buf: .res 2
label_buf= array_buf
array_ptr= laddr_buf
var_ptr= label_buf
; Pointers to start / end of labels name table
label_buf: .res 2
label_ptr= laddr_buf
; Pointer to labels address table / end of string/arrays table,
; top of used memory
; Pointers to start / end of labels address table
laddr_buf: .res 2
laddr_ptr= mem_end
; End of used memory
; End of parser memory
mem_end: .res 2

; Used from BASIC code
; Share this to BASIC runtime, to use less ZP memory
.exportzp BASIC_TOP
array_ptr= laddr_buf
BASIC_TOP= array_ptr

; vi:syntax=asm_ca65
5 changes: 2 additions & 3 deletions src/parse.asm
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

.export parser_start, parser_error, parser_skipws, parser_emit_byte, parser_inc_opos
; Parser state
.exportzp bptr, bpos, bmax, linenum, buf_ptr, end_ptr
.exportzp bptr, bpos, bmax, linenum, buf_ptr
.exportzp loop_sp, var_sp
; Output state
.exportzp opos
Expand All @@ -32,7 +32,7 @@
.importzp LT_WHILE_2, LT_FOR_1,LT_FOR_2, LT_EXIT, LT_IF, LT_ELSE, LT_ELIF
; From alloc.asm
.import alloc_prog
.importzp prog_ptr, BASIC_TOP
.importzp prog_ptr, end_ptr, BASIC_TOP
; From vars.asm
.exportzp var_count, label_count
; From runtime.asm
Expand All @@ -49,7 +49,6 @@

.zeropage
buf_ptr:.res 2
end_ptr:.res 2
bmax: .res 1
pptr: .res 2

Expand Down

0 comments on commit 91b2f19

Please sign in to comment.