Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"cell+" and "[include]" words, "hello world" example #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions examples/hello.foc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[text-section] init

[code]
org $2000 ; start address of program
[end-code]

[text-section] text
create hello ," Hello World" \ the printable text

[include] "lib/core.foc"

\ Hauptprogramm
: main
hello count type \ print the text on screen
key \ wait for keypress
dos \ jump to DOS
;

[code]
run boot ; set the RUNVEC to start program
[end-code]
36 changes: 34 additions & 2 deletions foco65
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ class BranchTarget:
class Words:
def __init__(self):
self.lst = []
self.aliases = {"cells": "2*", "cell": "2*", "not": "0="}
self.aliases = {"cells": "2*", "cell+": "2+", "not": "0="}

def __iter__(self):
return iter(self.lst)
Expand Down Expand Up @@ -397,6 +397,9 @@ class Forth:
elif token == "cells":
x = self.pop(token)
self.push(2 * x)
elif token == "cell+":
x = self.pop(token)
self.push(x + 2)
elif token == "/":
x2 = self.pop(token)
x1 = self.pop(token)
Expand Down Expand Up @@ -620,7 +623,7 @@ class Forth:
item_outputs = map(lambda i: i.output(section), self.items)
section_outputs.append("".join(item_outputs))
return "\n".join(section_outputs)

#####

boot_text = """
Expand Down Expand Up @@ -981,6 +984,19 @@ while_end
jmp next
[end-code] ;

: 2+
[label] two_plus
[code]
clc
lda #2
adc pstack,x
sta pstack,x
lda #0
adc pstack+1,x
sta pstack+1,x
jmp next
[end-code] ;

: count ( addr1 -- addr2 u )
[code]
lda pstack,x
Expand Down Expand Up @@ -1936,6 +1952,20 @@ m_star_done
[end-code] ;
"""

def resolve_includes(text):
i = text.find("[include]")
if (i > 0):
start = text.find('"',i+10)
end = text.find('"', start+1)
chunk1 = text[:i-1]
chunk2 = text[end+1:]
filename = text[start+1:end]
with open(filename, "rt") as f:
ntext = f.read()
text = resolve_includes(chunk1 + " " + ntext + " " + chunk2)

return text

parser = argparse.ArgumentParser()
parser.add_argument("--sections", "-s", metavar="STR", default="init,boot,data,text")
parser.add_argument("--pstack-bottom", "-p", metavar="ADDR", default="$600")
Expand All @@ -1949,6 +1979,8 @@ boot_params = {"pstack_bottom": args.pstack_bottom,
with open(args.file, "rt") as f:
text = f.read()

text = resolve_includes(text)

f = Forth(args.sections.split(","))

try:
Expand Down
69 changes: 69 additions & 0 deletions lib/core.foc
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
( Forth Words from the CORE word set )
( see http://www.forth200x.org/documents/forth16-1.pdf )

\ 6.1.1320 EMIT
\ print TOS as character on screen
: emit ( n -- )
[code]
lda pstack,x ; load char from stack
inx ; adjust stack pointer
inx ;
stx tmp ; save stack pointer
jsr do_ec ; jump to OS print char function
ldx tmp ; restore stack pointer
jmp next ; naechstes Forth-Wort

do_ec ; indirect jump to Atari OS
tax ; function to print char
lda $E407 ; on screen
pha
lda $E406
pha
txa
rts
[end-code] ;

\ 6.1.1750 KEY
\ read one character from keyboard
\ word waits for keypress
: key ( -- n )
[code]
lda #0
dex ; create space on stack
sta pstack,x ; clear high-byte
stx w ; save stack pointer
jsr do_gc ; jump to OS routine to read key
ldx w ; restore stack pointer
dex ; create space for low byte
sta pstack,x ; store key value in low byte
jmp next ; next Forth word

do_gc ; indirect jump to Atari OS
lda $E425 ; Routine to read char from
pha : keyboard
lda $E424
pha
rts
[end-code] ;

\ 6.1.0980 COUNT
\ Return the character string specification for the
\ counted string stored at c-addr1. c-addr2 is the
\ address of the first character after c-addr1.
\ u is the contents of the character at c-addr1,
\ which is the length in characters of the string at
\ c-addr2.
: count ( c-addr1 -- c-addr2 u )
dup c@ swap 1+ swap ;

\ 6.1.2310 TYPE
\ If u is greater than zero, display the character
\ string specified by c-addr and u.
: type ( c-addr u -- )
0 do dup i + c@ emit loop drop ;

\ Leave program and enter DOS via DOSVEC ($0A)
: dos
[code]
jmp ($0A)
[end-code] ;