Skip to content

Commit

Permalink
Make forth source compatible with both C64 and 128
Browse files Browse the repository at this point in the history
immedif.fs contains implementations of immediate `[IF]` `[ELSE]` and
`[THEN]` words that can be used for conditional compilation.

This allows us to handle differences between C64 and C128 machines at
compile time instead of runtime.
  • Loading branch information
rhalkyard committed Dec 8, 2020
1 parent 837d1a4 commit 6759f4c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ SRC_DIR = forth_src
SRC_NAMES = base debug v asm gfx gfxdemo rnd sin ls turtle fractals \
sprite doloop sys labels mml mmldemo sid spritedemo test testcore \
testcoreplus tester format require compat timer float viceutil turnkey \
wordlist c128
wordlist c128 immedif
SRCS = $(addprefix $(SRC_DIR)/,$(addsuffix .fs,$(SRC_NAMES)))

EMPTY_FILE = _empty.txt
Expand Down
1 change: 1 addition & 0 deletions forth_src/base.fs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ does> dup @ to here
marker ---modules---

.( wordlist..) include wordlist
.( immedif..) include immedif
.( labels..) include labels
.( doloop..) include doloop
.( sys..) include sys
Expand Down
8 changes: 5 additions & 3 deletions forth_src/debug.fs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,11 @@ last-dump ! base ! ;
: n last-dump @ dump ;
hide last-dump

: more
$eb c@ $18 = if
0 $f4 c! \ exit quoted state
: more
\ current cursor y pos
[ native-c128? ]
[if] $eb [else] $d6 [then]
c@ $18 = if
$12 emit ." more" $92 emit
key drop page then ;

Expand Down
38 changes: 38 additions & 0 deletions forth_src/immedif.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
\ Immediate versions of if/else/then to
\ allow conditional compilation.
\ Supports nesting.

header [if] ( -- ) immediate

: [then] ( -- ) ; immediate

: [else] ( -- )
\ consume and discard following words
\ until a matching [else] or [then] is
\ encountered
1 begin
begin parse-name dup while
find-name case
[ parse-name [if] find-name ]
literal of
1+
endof
[ latest ] literal of
1-
dup if 1+ then
endof
[ parse-name [then] find-name ]
literal of
1-
endof
endcase
?dup 0= if exit then
repeat
source nip drop 0= if refill then
source nip drop 0= until
drop
; immediate

define [if] ( flag -- )
0= if postpone [else] then
; \ immediate flag set in header
31 changes: 20 additions & 11 deletions forth_src/v.fs
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,20 @@ $400 + ( addr ) ;

: sol 0 curx ! ;

native-c128? [if]
: rom-kernal ;
: ram-kernal ;
[else]
\ ram + io + kernal rom
\ skip on c128
code rom-kernal
\ $36 lda,# 1 sta,
$36 lda,# 1 sta,
;code
\ ram + io + ram
code ram-kernal
\ $35 lda,# 1 sta,
$35 lda,# 1 sta,
;code
[then]

: reset-buffer
0 bufstart 1- c!
Expand Down Expand Up @@ -195,17 +200,20 @@ or advance-cur or until ;
begin advance-cur editpos 1+ dup
eof= swap c@ space= or or until ;

\ : setcur ( x y -- )
\ xr ! yr ! $fff0 [ clc, ] sys ;

native-c128? [if]
\ SYS doesn't work yet on the 128
\ this code works on both 64 and 128
code setcur ( x y -- )
lsb lda,x inx, lsb ldy,x inx,
w stx, tax,
clc, $fff0 jsr, \ PLOT,
clc, $fff0 jsr, \ PLOT
w ldx,
;code
[else]
: setcur ( x y -- )
xr ! yr ! $fff0 [ clc, ] sys ;
[then]


: refresh-line
cury @ $28 * $400 + $28 bl fill
Expand Down Expand Up @@ -630,16 +638,17 @@ curlinestart @ bufstart eof @ within
0= abort" cl" again ;

define v
\ modifies kernal to change kbd prefs
\ skip on 128
(
ram-kernal $eaea @ $8ca <> if
[ native-c128? ] [if]
\ C128:
[else]
\ C64: modify kernal to change kbd prefs
ram-kernal $eaea @ $8ca <> if
rom-kernal
$e000 dup $2000 move \ rom => ram
$f $eaea c! \ repeat delay
4 $eb1d c! \ repeat speed
then
)
[then]

0 to insert
$80 $28a c! \ key repeat on
Expand Down

0 comments on commit 6759f4c

Please sign in to comment.