项目作者: piotr-wiszowaty

项目描述 :
Forth cross-compiler targeting 6502 processors.
高级语言: Python
项目地址: git://github.com/piotr-wiszowaty/foco65.git
创建时间: 2014-07-19T13:40:30Z
项目社区:https://github.com/piotr-wiszowaty/foco65

开源协议:GNU General Public License v3.0

下载


foco65

Forth cross-compiler targeting 6502 processors. Outputs xasm
compatible assembly source code containing Forth runtime and compiled user
program.

Runtime

Generated Forth runtime is 16-bit indirect-threaded.

Parameter stack is full-descending and addressed by X register. Address and
size of the parameter stack are by default $600 and 256 respectively and are
user-configurable.

Hardware stack is used as return stack.

The runtime uses 12 bytes on zero page: 2-byte instruction pointer ip and
5 2-byte work registers: w, z, cntr, tmp, tmp2 which may be used
in user-defined words (note than on every word entry these registers’ contents
are undefined).

On entry runtime initializes instruction pointer and enters user-defined word
main which is supposed to never exit (i.e. contain an infinite loop).

Generated Source Code

Generated assembly source code for words/data/code blocks is logically grouped
into sections. Default order of sections is: init, boot, data, text.

Sections:

  • init - top of the output, typically contains user-provided assembly code
    block with org directive
  • boot - forth runtime
  • data - user data
  • text - core words and user program

User-defined data is output into the most recently declared data section (with
[data-section] word), everything else is output into the most recently
declared text section (with [text-section] word).

Syntax

Identifiers

Identifiers are case sensitive.

Constant and data identifiers consist of alphanumeric characters, ‘_‘s, ‘-‘s
and ‘?’s and must start with a letter, ‘_‘, ‘-‘ or ‘?’. In generated assembly
labels every ‘-‘ is replaced by ‘_‘ and every ‘?’ is replaced by ‘_is_‘.

Word identifiers consist of printable ASCII characters. Word identifiers
consisting only of alphanumeric characters, ‘_‘s, ‘-‘s, ‘?’s and starting with
a letter, ‘_‘, ‘-‘ or a ‘?’ are translated automatically to valid assembly
labels. Other word identifiers require user-defined label specification (see
below).

Section identifiers consist of printable ASCII characters.

Comments

Syntax:

  1. \ this is a one-line comment
  2. ( this is a multi-line
  3. comment)

Numbers

Syntax:

  1. 123 \ decimal number
  2. $BA98 \ hexadecimal number

Word definitions

Syntax:

: name [ [label] asm-label ] words ;

or:

: name
[ [label] asm-label ]
[code]

; inline-assembly

[end-code] ;

Examples:

  1. : foo begin end-flag until ;
  1. : bar
  2. [code]
  3. lda #1
  4. sta $D5E8
  5. jmp next
  6. [end-code] ;
  1. : +7 ( n1 -- n2 )
  2. [label] plus_7
  3. 7 + ;

Data declarations

One-cell variable:

variable name

Two-cell variable:

2variable name

Assembly label at current program counter:

create name

Compile a one-cell value:

value ,

Compile a one-byte value:

value c,

Allocate a byte-array:

length allot

Allocate Atari XL/XE Antic counted string:

,' text'

Allocate ASCII counted string:

," text"

Allocate Atari XL/XE Antic string:

' text'

Allocate ASCII string:

" text"

Constants

Syntax:

value constant name

Example:

  1. $230 constant dladr

Inline assembly blocks

Syntax:

[code]

; assembly code

[end-code]

Compiler directives

Syntax:

[text-section] name

or:

[data-section] name

Words

! " ' ['] ( * + ," ,' , - / : <= < <> < =
>= > @ [ \ ] 0= 1+ 1- 2! 2* 2/ 2@ 2drop 2dup
2over 2swap 2variable allot and c! c, c@ cell cells cmove
[code] constant count create d+ d- d= [data-section] do
drop dup [end-code] execute fill i j [label] lit +loop
loop lshift m* nip not or over >r r> recursive rot -rot
rshift rsp sp swap [text-section] u< u> unloop variable
while

Usage

  1. foco65 [OPTIONS] INPUT-FILE

OPTIONS:

  1. -h display help
  2. -p ADDR,--pstack-bottom=ADDR parameter stack bottom address
  3. -s SECTS,--sections=SECTS specify comma separated list of sections
  4. default: init,boot,data,text
  5. -S INT,--pstack-SIZE=INT parameter stack size

Example:

  1. $ foco65 foo.forth > foo.asx

Examples

Typical Atari XL/XE executable program structure.

  1. [text-section] init
  2. [code]
  3. org $2000
  4. [end-code]
  5. \ constant definitions
  6. \ data declarations
  7. \ word definitions
  8. [text-section] text
  9. : main
  10. \ user program initialization
  11. begin
  12. \ user program main loop
  13. again ;
  14. [code]
  15. run boot
  16. [end-code]

Atari XL/XE example: display character table.

  1. [text-section] init
  2. [code]
  3. org $3000
  4. [end-code]
  5. [text-section] text
  6. $230 constant dladr
  7. variable screen
  8. variable cursor
  9. variable line
  10. : cursor-next ( -- u )
  11. cursor @ dup 1+ cursor ! ;
  12. : put-char ( c -- )
  13. cursor-next c! ;
  14. : set-cursor ( u -- )
  15. screen @ + cursor ! ;
  16. : main
  17. dladr @ 4 + @ screen !
  18. 0 line !
  19. 16 0 do
  20. line @ set-cursor
  21. line @ 40 + line !
  22. 16 0 do
  23. i j 4 lshift or put-char
  24. loop
  25. loop
  26. begin again ;
  27. [code]
  28. run boot
  29. [end-code]

Atari XL/XE example of defining word in assembly:
wait for keypress and push the pressed key’s code
on the parameter stack.

  1. : get-char ( -- c )
  2. [code]
  3. lda #0
  4. dex
  5. sta pstack,x
  6. stx w
  7. jsr do_gc
  8. ldx w
  9. dex
  10. sta pstack,x
  11. jmp next
  12. do_gc
  13. lda $E425
  14. pha
  15. lda $E424
  16. pha
  17. rts
  18. [end-code] ;

Increase cell at the given address. Shows defining words not
being a valid assembler label.

  1. create array 16 cells allot
  2. \ increase cell at given address
  3. : ++ ( addr -- )
  4. [label] plus_plus
  5. dup @ 1+ swap ! ;