Skip to content

Commit

Permalink
6502: Porting to the new system setup and Makefile
Browse files Browse the repository at this point in the history
Single space only right now
  • Loading branch information
bshepherdson committed Nov 14, 2024
1 parent 2e6f4d5 commit ec0d6a9
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 39 deletions.
11 changes: 6 additions & 5 deletions 6502/finalize.ft
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
\ Final file that goes last in the process.
\ This sets Target's LATEST and DP properly.

\ TODO: This file is (I think) identical across all the systems now?
host definitions
\ This needs to be the last lines in the target compilation.
host latest var_latest tcode!
host data-space @ var_dp_ptr tcode!

S" entry-point " type entry-point hex. cr
S" entry-point @ " type entry-point tcode@ hex. cr
config data-indirect? @ [IF] prepare-data-initializers [THEN]

\ This needs to be the last lines in the target compilation.
host latest latest0 tcode!
host data-space @ dp0 tcode!

\ At this point the assembler's output region should be complete.
\ Dump the binary!
Expand Down
30 changes: 17 additions & 13 deletions 6502/kernel.ft
Original file line number Diff line number Diff line change
Expand Up @@ -408,22 +408,26 @@ end-code-bare


\ Variables and core states ==================================================
label var_latest 0 asm,16
label var_base 0 asm,16
label var_state 0 asm,16
label var_handler 0 asm,16
label dp0 0 asm,16
label latest0 0 asm,16

TVARIABLE var_dp
TVARIABLE var_latest
TVARIABLE var_base
TVARIABLE var_state
TVARIABLE var_handler

data-space @ CONSTANT var_last_parse 2 tcells tdata-allot
data-space @ CONSTANT var_source 2 tcells tdata-allot
TVARIABLE var_in_ptr
TVARIABLE var_source_id

data-space @ CONSTANT input-buf 128 tdata-allot

label var_last_parse 0 asm,16 0 asm,16
label var_source 0 asm,16 0 asm,16
label var_in_ptr 0 asm,16
label var_source_id 0 asm,16
label input-buf 128 tcode-allot

\ The dictionary pointer is in the zero page for speed.
code DP herel lit, end-code

label var_dp_ptr 0 asm,16

code , ( x -- )
0 zp,x lda,
0 # ldy,
Expand Down Expand Up @@ -928,9 +932,9 @@ label main
\ TODO: Adjust the memory banks at this point, if we're going to.

\ Set the initial DP into the ZP slot.
var_dp_ptr $ lda,
dp0 $ lda,
herel zp sta,
var_dp_ptr 1+ $ lda,
dp0 1+ $ lda,
hereh zp sta,

\ The hardware stack starts configured - set up the data stack by setting X.
Expand Down
22 changes: 12 additions & 10 deletions 6502/model-target.ft
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ target : ,CF ( codeword-addr -- ) compile, ;
\ In STC this is empty, so this word is a no-op.
target : ,DOCOL ( -- ) ;

host colocated-names? [IF]
target : ,PFA ( -- ) ;
host [ELSE]
host config data-indirect? @ [IF]
target : ,PFA ( -- ) here cell+ , ;
host [ELSE]
target : ,PFA ( -- ) ;
host [THEN]

target : ,xt-slot ( -- ) ,pfa ;
Expand Down Expand Up @@ -52,25 +52,27 @@ target : EXIT [ host $60 tliteral target ] c, ; IMMEDIATE
target : ALIGNED ; host acts: ;
target : ALIGN ; host acts: ;



target : NAME>STRING ( nt -- c-addr u )
cell+ dup char+ swap c@ [ host mask_len tliteral target ] and ;

\ This is actually portable but since it fits right here between ALIGNED and
\ (DOES>), there's no good place to put it with splitting this file into 3.
target : >CFA ( nt -- xt )
name>string + aligned
[ host config mix-code-and-name? @ 0= [IF] target ] @ [ host [THEN] target ] ;
[ host config names-indirect? @ [IF] target ] @ [ host [THEN] target ] ;

host config mix-code-and-name? @ [IF]
target : /NAME-FIELD ( -- u ) 0 ;
host [ELSE]
host config names-indirect? @ [IF]
target : /NAME-FIELD ( -- u ) 1 cells ;
host [ELSE]
target : /NAME-FIELD ( -- u ) 0 ;
host [THEN]

host config mix-code-and-data? @ [IF]
target : >BODY ( xt -- addr ) 3 + ;
host [ELSE]
host config data-indirect? @ [IF]
target : >BODY ( xt -- addr ) 3 + @ ;
host [ELSE]
target : >BODY ( xt -- addr ) 3 + ;
host [THEN]


Expand Down
17 changes: 9 additions & 8 deletions 6502/model.ft
Original file line number Diff line number Diff line change
Expand Up @@ -94,28 +94,29 @@ $0000 $ jmp,
\ return addresses work on the 6502.
\ So pop it from the return stack, add 1, and push it onto the data stack.
\ Then the RTS at the end will return to the thread.
colocated-names? [IF]
config data-indirect? @ [IF]
: pfa->tos, ( -- )
dex, dex,
pla, \ lo -> A
clc,
1 # adc, \ A+1 -> A
0 zp,x sta, \ -> TOS_lo
wl zp sta, \ -> W_lo
pla, \ hi -> A
0 # adc,
1 zp,x sta, ;
wh zp sta, \ -> W_hi

\ Now the address is ready in W, and there's space on the stack already.
wl (zp)>tos, ;
[ELSE]
: pfa->tos, ( -- )
dex, dex,
pla, \ lo -> A
clc,
1 # adc, \ A+1 -> A
wl zp sta, \ -> W_lo
0 zp,x sta, \ -> TOS_lo
pla, \ hi -> A
0 # adc,
wh zp sta, \ -> W_hi

\ Now the address is ready in W, and there's space on the stack already.
wl (zp)>tos,
1 zp,x sta, ;
[THEN]

label DOVAR
Expand Down
24 changes: 24 additions & 0 deletions 6502/preamble.ft
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,27 @@ interactive-forth!
1 config alignment !

config little-endian? ON


\ Target spaces ==============================================================
REQUIRE ../host/spaces.ft

$0801 CONSTANT origin
$0810 CONSTANT machine-code-entry
here 2 allot align THERE-PTR CONSTANT prg-header

\ Add a new tiny space for the "origin" pointer in the .prg file.
\ It's a 2-byte little-endian offset value, which is the same format as a
\ target cell.
$0 prg-header org!
$0 prg-header space>offset !
origin $0 prg-header <t!>

: spaces::single ( -- )
single-space!
origin code-space ORG!

\ There's a 2-byte PRG header first.
$2 code-space space>offset ! ;

' spaces::single IS default-spaces!
2 changes: 2 additions & 0 deletions 6502/system.ft
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
?default-spaces

host definitions REQUIRE assembler.ft
host definitions REQUIRE registers.ft
host definitions REQUIRE asm-helpers.ft
Expand Down
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,23 @@ test-arm-copying: forth-arm-copying-tests.bin test.disk FORCE

# Commodore 64 ===============================================================
forth-c64.prg: host/*.ft 6502/*.ft shared/*.ft
$(FORTH) 6502/main.ft
$(FORTH) 6502/preamble.ft -e "' spaces::single IS default-spaces!" \
6502/system.ft -e 'host :noname S" $@" ; IS tcforth-output' \
6502/finalize.ft -e 'bye'

forth-c64-test.prg: host/*.ft 6502/*.ft shared/*.ft
$(FORTH) 6502/main-test.ft
$(FORTH) 6502/preamble.ft -e "' spaces::single IS default-spaces!" \
6502/system.ft -e 'host :noname S" $@" ; IS tcforth-output' \
6502/test-tail.ft \
6502/finalize.ft -e 'bye'

c64: forth-c64.prg

run-c64: forth-c64.prg
$(VICE_C64) $(VICE_C64_FLAGS) forth-c64.prg

test-c64: forth-c64-test.prg FORCE
$(VICE_C64) $(VICE_C64_FLAGS) -warp forth-c64-test.prg
$(VICE_C64) $(VICE_C64_FLAGS) -warp $<

# Top level ==================================================================
test: test-dcpu16 test-dcpu16-separate test-dcpu16-copying \
Expand Down

0 comments on commit ec0d6a9

Please sign in to comment.