V-FORTH - Forth-83 for the VIC

Basic and Machine Language

Moderator: Moderators

User avatar
srowe
Vic 20 Scientist
Posts: 1341
Joined: Mon Jun 16, 2014 3:19 pm

Re: V-FORTH - Forth-83 for the VIC

Post by srowe »

To define a word which is implemented in machine code we first need to define a defining word

Code: Select all

: code
create here dup body> ! ; immediate
Words defined using code will execute the contents of the word rather than using the Forth virtual machine.

A simple example

Code: Select all

hex
include sysconsts.fs

code star
a9 c, 2a c, 20 c, ffd2 , 4c c, next ,

star
If you never want to return to Forth then your code can do what you like. Otherwise there are some rules it must follow:
  • you must preserve the .X register, it is the data stack pointer
  • zero page locations between $10 and $77 must not be used
  • a word must complete with a JMP to NEXT
funkheld
Vic 20 Devotee
Posts: 241
Joined: Tue Sep 10, 2019 4:23 am

Re: V-FORTH - Forth-83 for the VIC

Post by funkheld »

hello, thanks for the help vforth.

greeting
funkheld
Vic 20 Devotee
Posts: 241
Joined: Tue Sep 10, 2019 4:23 am

Re: V-FORTH - Forth-83 for the VIC

Post by funkheld »

hello, good day.

greeting

this asm not loop :
-------------------------------------------------
lda #1
sta 4096
lda #2
sta 4097
lda #3
sta 4098
-------------------------------------------------
hex

include sysconsts.fs

: code create here dup body> ! ; immediate

code scrtest
a9 c, 1 c, 8d c, 0 c, 10 c, a9 c, 2 c, 8d c, 1 c, 10 c, a9 c, 3 c, 8d c, 2 c, 10 c, 4C c, next ,

-------------------------------------------------
User avatar
srowe
Vic 20 Scientist
Posts: 1341
Joined: Mon Jun 16, 2014 3:19 pm

Re: V-FORTH - Forth-83 for the VIC

Post by srowe »

I'm not quite sure what you mean, your code works, writing "ABC" to the top of the screen
Screenshot_20190921_113100.png
If you want to do loops in machine code it will be easier to use the assembler vocabulary, when calculating branch offsets by hand it is easy to make mistakes.
funkheld
Vic 20 Devotee
Posts: 241
Joined: Tue Sep 10, 2019 4:23 am

Re: V-FORTH - Forth-83 for the VIC

Post by funkheld »

hello, thanks it ok.

greeting
funkheld
Vic 20 Devotee
Posts: 241
Joined: Tue Sep 10, 2019 4:23 am

Re: V-FORTH - Forth-83 for the VIC

Post by funkheld »

Hi good afternoon.

can you please translate that for vforth, so that you can set a point.

thanks.
greeting

--------------------------------------
ad=4352
ay=y/16*336+(y and 15)
ad=ad+16*x/8+ay
ma=2^(7-(x and 7))
poke ad,peek(ad) or ma
---------------------------------------
User avatar
srowe
Vic 20 Scientist
Posts: 1341
Joined: Mon Jun 16, 2014 3:19 pm

Re: V-FORTH - Forth-83 for the VIC

Post by srowe »

The trick with writing Forth words is decomposing the work into parts that are simple. Each word should work with 2 or 3 values on the stack, any more and it gets difficult to keep track where each one is.

There are two main pieces of work: calculate the byte address to be updated and calculate the bitmask to update the address with.

For the first we will do the two parts that use the x and y values separately first, just to make it clear what they are doing

Code: Select all

( y -- ay )
dup  16  /  336  *  swap  15  and  +

( x -- ax )
8  / 16  *
If we put this together and call it getbad (get byte address) it would look like this

Code: Select all

: getbad  ( x y  --  baddr )
    dup  16  /  336  *  swap  15  and  +
    swap  8  /  16  *  +  4352  +  ;
Next we want the value for the lower three bits of x, calculating powers of two would be complex, there are only eight masks so it is more efficient to use a table

Code: Select all

( x  --  bpos )
create bmask
    128  c,  64  c,  32  c,  16  c,  8  c,  4  c,  2  c,  1  c,
    
 7  and  bmask  +  c@
So the set of words to do this all would be

Code: Select all

: getbad  ( x y  --  baddr )
    dup  16  /  336  *  swap  15  and  +
    swap  8  /  16  *  +  4352  +  ;
    
 create bmask
    128  c,  64  c,  32  c,  16  c,  8  c,  4  c,  2  c,  1  c,
    
  : plot  ( x y  --  )
    over  swap  ( copy of x under original )
    getbad  swap  7  and  bmask  +  c@
    over  c@  ( get current value )
    or  swap  c!  ;
There are some optimizations (use bit shift instead of division, use return stack to hold values )
funkheld
Vic 20 Devotee
Posts: 241
Joined: Tue Sep 10, 2019 4:23 am

Re: V-FORTH - Forth-83 for the VIC

Post by funkheld »

Hello, good evening.

thank you for your help.
that vforth is great when you learn the ricks and help from you.

thanks.
greeting
funkheld
Vic 20 Devotee
Posts: 241
Joined: Tue Sep 10, 2019 4:23 am

Re: V-FORTH - Forth-83 for the VIC

Post by funkheld »

Hi good afternoon.

is there a command for Vforth that can save the command eg at $a000 and then execute it by copying it into a
room where it can be called then hint ... or something?

thanks.
greeting
funkheld
Vic 20 Devotee
Posts: 241
Joined: Tue Sep 10, 2019 4:23 am

Re: V-FORTH - Forth-83 for the VIC

Post by funkheld »

Hi good afternoon.

if I make the text so in the VICE (push in directly, not on disk ) in vforth :

: test
     "hello world" cr
;

fine.

if I make the text so for the VIC20 (MiSTER):
if I leave text so on the VICE on disk:
: test
     "hello world" cr
;

it does not work in vforth.

with which program can you please the text for the D64 properly convert?

thanks.
greeting
funkheld
Vic 20 Devotee
Posts: 241
Joined: Tue Sep 10, 2019 4:23 am

Re: V-FORTH - Forth-83 for the VIC

Post by funkheld »

why is this program: plotscr1.fs not loaded.

thanks.
greeting

Code: Select all

VARIABLE BXG 
VARIABLE BYG 

: GETBAD
DUP  16  /  336  *  SWAP  15  AND  +
SWAP  8  /  16  *  +  4352  +  ;
    
CREATE BMASK
128  C,  64  C,  32  C,  16  C,  8  C,  4  C,  2  C,  1  C,
    
: PLOT
OVER  SWAP
GETBAD  SWAP  7  AND  BMASK  +  C@
OVER  C@
OR  SWAP  C!  ;
	
: SETZE
DUP 48 = IF BXG C@ 1 - BXG C!  THEN  BXG C@ 255 = IF 0   BXG C! THEN  
DUP 49 = IF BXG C@ 1 + BXG C!  THEN  BXG C@ 167 > IF 167 BXG C! THEN  
DUP 9  = IF BYG C@ 1 - BYG C!  THEN  BYG C@ 255 = IF 0   BYG C! THEN 
    41 = IF BYG C@ 1 + BYG C!  THEN  BYG C@ 175 > IF 175 BYG C! THEN 
BXG C@ BYG C@ PLOT ;

: GRAFIKSCR
8048 4352 DO 0 I C! LOOP
151 36867 C! 21 36866 C! 204 36869 C! 14 36864 C!
255 16 DO I I 4096 16 - + C! LOOP
255 16 DO 6 I 37888 16 - + C! LOOP ;

: GEHE
80 BXG C! 80 BYG C! 
GRAFIKSCR
BEGIN
197 C@  SETZE ?TERMINAL  UNTIL ;
Attachments
error2.jpg
error2.jpg (3.35 KiB) Viewed 1901 times
error1.jpg
User avatar
srowe
Vic 20 Scientist
Posts: 1341
Joined: Mon Jun 16, 2014 3:19 pm

Re: V-FORTH - Forth-83 for the VIC

Post by srowe »

funkheld wrote: Tue Sep 24, 2019 1:56 am if I make the text so in the VICE (push in directly, not on disk ) in vforth :

: test
     "hello world" cr
;

fine.

if I make the text so for the VIC20 (MiSTER):
if I leave text so on the VICE on disk:
: test
     "hello world" cr
;

it does not work in vforth.

with which program can you please the text for the D64 properly convert?
I use c1541 (which come with VICE). You must write the files so they have a SEQ type and they must not have tab characters, only spaces. Because the CBM character set is different to ASCII you need to use lower case rather than upper case (capitals).

Code: Select all

c1541 vforth.d64 -write demo.fs.cbm demo.fs,seq
funkheld
Vic 20 Devotee
Posts: 241
Joined: Tue Sep 10, 2019 4:23 am

Re: V-FORTH - Forth-83 for the VIC

Post by funkheld »

Hi, Thank You.
your tip is running now.

thanks.
greeting
funkheld
Vic 20 Devotee
Posts: 241
Joined: Tue Sep 10, 2019 4:23 am

Re: V-FORTH - Forth-83 for the VIC

Post by funkheld »

Hi good afternoon.

why does not case work?

msg # 17
msg # 19

thanks.
greeting

Code: Select all

hex

: case
?comp  csp  @  !csp  4  ;   immediate

: of
4  ?pairs  compile over  compile  =  compile  0branch  here  0  ,
compile  drop  5  ;   immediate

: endof
5  ?pairs  compile  branch  here  0  ,  swap  2  [compile]  then
4  ;   immediate

: endcase
4  ?pairs  compile  drop  begin  sp@  csp  @  =  0=  while
2  [compile]  then  repeat  csp  !  ;   immediate

decimal

: testcase
case
1 of ." one" endof
2 of ." two" endof
3 of ." three" endof
." other"
endcase ;

: setze
dup 81 = if 1 testcase then 
dup 69 = if 2 testcase then 
    87 = if 3 testcase then  ;

: gehe 
cr begin
key  setze ?terminal  until  ;
User avatar
srowe
Vic 20 Scientist
Posts: 1341
Joined: Mon Jun 16, 2014 3:19 pm

Re: V-FORTH - Forth-83 for the VIC

Post by srowe »

Error #17 is "not compiling", somewhere a word intended to only be used at compile time is being used. The Error #19 is "conditionals not paired", you need to fix the first error.

I'm on vacation now, I won't have access to my computer.
Post Reply