From 1726c49c3e0145507d1edbaf95157a78cca5e489 Mon Sep 17 00:00:00 2001 From: Carsten Strotmann Date: Sat, 25 Feb 2017 18:30:12 +0100 Subject: [PATCH 1/5] fixed 'cell' to be '2+', added '2+' as an primitive --- foco65 | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/foco65 b/foco65 index 37027e7..248227c 100755 --- a/foco65 +++ b/foco65 @@ -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) @@ -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) @@ -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 From 711a691deee41a1cf4bb83cf85955350aa216fd5 Mon Sep 17 00:00:00 2001 From: Carsten Strotmann Date: Sun, 26 Feb 2017 23:30:05 +0100 Subject: [PATCH 2/5] changed "cell" to "cell+" to be compliant with Forth 200x standard --- foco65 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/foco65 b/foco65 index 248227c..58b84b3 100755 --- a/foco65 +++ b/foco65 @@ -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) @@ -397,7 +397,7 @@ class Forth: elif token == "cells": x = self.pop(token) self.push(2 * x) - elif token == "cell": + elif token == "cell+": x = self.pop(token) self.push(x + 2) elif token == "/": From 68bd24bcd3b85e8a365f953e72767a60bd46e93f Mon Sep 17 00:00:00 2001 From: Carsten Strotmann Date: Mon, 27 Feb 2017 00:06:10 +0100 Subject: [PATCH 3/5] added '[include] ""' statement --- foco65 | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/foco65 b/foco65 index 58b84b3..11132ef 100755 --- a/foco65 +++ b/foco65 @@ -623,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 = """ @@ -1952,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") @@ -1965,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: From 2430b7741146453a433fe251b1498ba2e018be73 Mon Sep 17 00:00:00 2001 From: Carsten Strotmann Date: Mon, 27 Feb 2017 00:07:27 +0100 Subject: [PATCH 4/5] a "Hello World" example (for ABBUC Mag article) --- examples/hello.foc | 22 ++++++++++++++++ lib/core.foc | 66 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 examples/hello.foc create mode 100644 lib/core.foc diff --git a/examples/hello.foc b/examples/hello.foc new file mode 100644 index 0000000..44650f2 --- /dev/null +++ b/examples/hello.foc @@ -0,0 +1,22 @@ +[text-section] init + +[code] + org $2000 ; Startadresse des Programms +[end-code] + +[text-section] text +\ Der auszugebende Text +create hello ," Hello World" + +[include] "lib/core.foc" + +\ Hauptprogramm +: main + hello count type \ Text ausgeben + key \ auf Tastendruck warten + dos \ in das DOS springen +; + +[code] + run boot \ Programm-Start anspringen (XASM) +[end-code] diff --git a/lib/core.foc b/lib/core.foc new file mode 100644 index 0000000..ed831f1 --- /dev/null +++ b/lib/core.foc @@ -0,0 +1,66 @@ +\ Forth-Word, um ein Zeichen +\ auf dem Bildschirm auszugeben +: emit ( n -- ) + [code] + jmp emit2 +chout + tax ; A-Register sichern + lda $E407 ; Aus dem E: Handler + pha ; die OS-Routine + lda $E406 ; laden und auf den + pha ; Stack legen + txa ; A-Register wiederherstellen + rts ; OS-Routine anspringen +emit2 + lda pstack,x ; Zeichen vom Stack laden + inx ; Steck-Zeiger anpassen + inx ; + stx tmp ; X-Register sichern + jsr chout ; Zeichen ausgeben + ldx tmp ; X-Reg. wiederherstellen + jmp next ; naechstes Forth-Wort + [end-code] +; + +\ Zeichen von OS lesen +: key ( -- n ) + [code] + jmp key2 +getkey + lda $E425 ; Aus dem K:-Handler + pha ; die OS-Routine + lda $E424 ; laden und auf den + pha ; Stack legen + rts ; Routine aufrufen +key2 + jsr getkey ; Tastatur abfragen + dex ; Stack-Zeiger anpassen + sta pstack,x ; Zeichen auf den Stack legen + lda #0 ; High-Byte = 0 + dex ; Stack-Zeiger anpassen + sta pstack,x ; auf den Stack legen + jmp next ; naechstes Forth-Wort + [end-code] +; + +\ Forth-Wort um die Laenge und die +\ Adresse einer Zeichenkette mit +\ Laengenbyte auf den Stack zu +\ legen +: count ( addr -- addr len ) + dup c@ swap 1+ swap +; + +\ eine Zeichenkette an Adresse "addr" +\ mit Laenge "len" auf dem Bildschirm +\ ausgeben +: type ( addr len -- ) + 0 do dup i + c@ emit loop drop +; + +\ über DOSVEC wieder in das DOS springen +: dos + [code] + jmp ($A) + [end-code] +; From 2d73e7a8c294dc76f2cd87992acb7b637b06aa6c Mon Sep 17 00:00:00 2001 From: Carsten Strotmann Date: Tue, 28 Feb 2017 14:51:12 +0100 Subject: [PATCH 5/5] translate comments into english --- examples/hello.foc | 13 +++--- lib/core.foc | 113 +++++++++++++++++++++++---------------------- 2 files changed, 64 insertions(+), 62 deletions(-) diff --git a/examples/hello.foc b/examples/hello.foc index 44650f2..0468cab 100644 --- a/examples/hello.foc +++ b/examples/hello.foc @@ -1,22 +1,21 @@ [text-section] init [code] - org $2000 ; Startadresse des Programms + org $2000 ; start address of program [end-code] [text-section] text -\ Der auszugebende Text -create hello ," Hello World" +create hello ," Hello World" \ the printable text [include] "lib/core.foc" \ Hauptprogramm : main - hello count type \ Text ausgeben - key \ auf Tastendruck warten - dos \ in das DOS springen + hello count type \ print the text on screen + key \ wait for keypress + dos \ jump to DOS ; [code] - run boot \ Programm-Start anspringen (XASM) + run boot ; set the RUNVEC to start program [end-code] diff --git a/lib/core.foc b/lib/core.foc index ed831f1..1ff5bf4 100644 --- a/lib/core.foc +++ b/lib/core.foc @@ -1,66 +1,69 @@ -\ Forth-Word, um ein Zeichen -\ auf dem Bildschirm auszugeben +( 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] - jmp emit2 -chout - tax ; A-Register sichern - lda $E407 ; Aus dem E: Handler - pha ; die OS-Routine - lda $E406 ; laden und auf den - pha ; Stack legen - txa ; A-Register wiederherstellen - rts ; OS-Routine anspringen -emit2 - lda pstack,x ; Zeichen vom Stack laden - inx ; Steck-Zeiger anpassen + lda pstack,x ; load char from stack + inx ; adjust stack pointer inx ; - stx tmp ; X-Register sichern - jsr chout ; Zeichen ausgeben - ldx tmp ; X-Reg. wiederherstellen + stx tmp ; save stack pointer + jsr do_ec ; jump to OS print char function + ldx tmp ; restore stack pointer jmp next ; naechstes Forth-Wort - [end-code] -; -\ Zeichen von OS lesen +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] - jmp key2 -getkey - lda $E425 ; Aus dem K:-Handler - pha ; die OS-Routine - lda $E424 ; laden und auf den - pha ; Stack legen - rts ; Routine aufrufen -key2 - jsr getkey ; Tastatur abfragen - dex ; Stack-Zeiger anpassen - sta pstack,x ; Zeichen auf den Stack legen - lda #0 ; High-Byte = 0 - dex ; Stack-Zeiger anpassen - sta pstack,x ; auf den Stack legen - jmp next ; naechstes Forth-Wort - [end-code] -; +[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] ; -\ Forth-Wort um die Laenge und die -\ Adresse einer Zeichenkette mit -\ Laengenbyte auf den Stack zu -\ legen -: count ( addr -- addr len ) - dup c@ swap 1+ swap -; +\ 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 ; -\ eine Zeichenkette an Adresse "addr" -\ mit Laenge "len" auf dem Bildschirm -\ ausgeben -: type ( addr len -- ) - 0 do dup i + c@ emit loop drop -; +\ 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 ; -\ über DOSVEC wieder in das DOS springen +\ Leave program and enter DOS via DOSVEC ($0A) : dos [code] - jmp ($A) - [end-code] -; + jmp ($0A) + [end-code] ;