Jump to content

FORTH and BASIC graphic program comparison


Recommended Posts


I don't exactly understand what you mean.


I am not the "sharpest knife in the drawer" as we say, but if you wanted R to relate to a random number,

why would you use a function that does not have a random number in it somewhere? :?


(or did I miss a joke here)


Sorry, as Mizapf wrote it was kind of a joke, but I have been wondering about how to make a test for randomness too.



Link to comment
Share on other sites

Patterns on the screen? Maybe you can evaluate randomness with a hires pattern that should become a kind of white noise, but this is still not general enough because it just adds one dimension.


Sorry, as Mizapf wrote it was kind of a joke.


Your jokes are suitable as exam questions. :-D

Link to comment
Share on other sites

Say it ain't so ! :(


Sadly true. :P I had assumed that >83C0 was used because it is identified by TI documentation as the “random number seed”, continued changing until a program was started and is, in fact, used by several other TI languages (TI Basic, GPL, TI Forth among them) for their respective PRNGs. Of course, you know what “assume” does. :)


My awareness of the truth came from finally walking TI Extended Basic's GPL code for the PRNG, which Rich has posted several times. I had not tried to slog through the GPL code to date because I was so sure I would be in for a long session with GPL. It was actually not that difficult. ;-)



  • Like 1
Link to comment
Share on other sites

Yea I posted GPL XB RND code to deal with protests that I did not understand how RND in XB worked.


I made a post stating how insane Texas Instruments was for putting Advanced Calculator code in RND for 49 million in better random histograms.


But consider 99% of the time people only needed 1 to 1000 at most, so 49 million is just insane overkill and detrimental to performance on games.


This combined with my pointing out in Fest West 2017 that TI made XB slow on purpose so XB could be compatible with TI Basic, can you imagine

typing in a game for TI Basic and it would not work in XB only because it was to fast? (So TI put slows into XB to make it backward Compatible.)

Link to comment
Share on other sites

So I took a look at TI-Forth source code for RNDW, RND etc...


Very simple and it performs quite well. I did 2 things that improved the speed of RNDW from 703 uS down to 681.6 uS

measured with the 9901 hardware timer under CLASSIC99


Manual cycle counting on TurboForth indicate it would be about 90uS for the ALC version.

\ TI-FORTH PRNG minor mods for CAMEL99

83C0 CONSTANT 'SEED  ( address of random number seed in PAD RAM)

: RNDW      ( -- n)   'SEED @ 6FE5 * 7AB9 + 5 SRC DUP 'SEED ! ;
: RND       ( n -- n) RNDW ABS SWAP MOD ;

Since the Histogram was not as revealing as much as I wanted it to, I tried looking for the first time a number repeated in a sequence.


This simple little PRNG did not repeat until 20,053 random numbers with most SEED values. Some SEEDs go over 26,000 numbers.


Here is the test code.

( I used a 32 bit counter to avoid the chance of overflow)

\ Random number test code
CREATE DCNT   4 ALLOT                         \ 32bit integer variable, 4 bytes

: DCNT++    ( -- ) DCNT 2@ 1 M+ DCNT 2! ;     \ incr. 32 bit counter
: CLRCNT    ( -- ) 0 0 DCNT 2! ;              \ clear counter
: UD.       ( d -- )  <# #S #>  TYPE SPACE ;  \ print double, unsigned
: .DCNT     ( -- )  DCNT 2@ UD. ;             \ print counter

: NEXT_REP    \ find next repetition of Random number
        9900 'SEED !
        RNDW DUP
        CR ." Searching for:" DECIMAL U.
            OVER =
        CR  ." Duplicate after "  .DCNT  ." random no.s"  ;

Link to comment
Share on other sites



I coded RNDW in ALC and it comes in at 149.1 uS in Indirect threaded code and 127.8 uS in the DTC system.

I think I will keep that in the Kernel.


Here is the ASM version with top of stack cached in register.

By doing the MPY in R3 the answer falls out into R4 which is my TOS cache.

Save 1 instruction!

CODE: RNDW    ( -- n)    \ replaces: 'SEED @ 6FE5 * 7AB9 + 5 SRC DUP 'SEED ! 24+4=28 bytes DTC
              TOS PUSH,
              83C0 @@ R3 MOV,
              R2 6FE5 LI,
              R2 R3 MPY,
              TOS 7AB9 ADDI,
              TOS 5 SRC,
              TOS 83C0 @@ MOV,
              END-CODE   \ 24 bytes

Here is the test code using 9901 timer read called TMR@ which returns 21.3 uS ticks.

: RNDTEST ( -- ) TMR@ RNDW TMR@  NIP  - . ;

\ ticks * 213 = uS*10

Edited by TheBF
Link to comment
Share on other sites

  • 1 month later...

While trolling through the library code for GForth, the GNU Forth system, I found a random number generator.


I uses a very simple algorithm and might be useful to Assembly language programmers here.

Here is the Forth Code:

\ generates random numbers                             12jan94py

\ Copyright (C) 1995,2000,2003,2007 Free Software Foundation, Inc.

\ This file is part of Gforth.

\ Gforth is free software; you can redistribute it and/or
\ modify it under the terms of the GNU General Public License
\ as published by the Free Software Foundation, either version 3
\ of the License, or (at your option) any later version.

\ This program is distributed in the hope that it will be useful,
\ but WITHOUT ANY WARRANTY; without even the implied warranty of
\ GNU General Public License for more details.

\ You should have received a copy of the GNU General Public License
\ along with this program. If not, see http://www.gnu.org/licenses/.

\ From Gforth, modified for CAMEL99 system
83C0 CONSTANT SEED   \ RAM where TI has a number incrementing in main menu
1045 CONSTANT GEN#   \ GForth uses $10450405  \ taking the 1st 16 bits

: RND  ( -- N )  SEED @ GEN# UM* DROP 1+ DUP SEED ! ;
: RANDOMIZE ( N -- 0..N-1 )  RND UM* NIP ;  (edit: CORRECTED this code with UM*)

If we were to translate it to infix notation as a function in integer BASIC it would something like this:

The multiplication (UM*) in Forth is unsigned, mixed multiply ( int*int->double) which is really just the TMS9900 MPY instruction.

Nice and easy.


The cool thing is where the TI-Forth algorithm repeats after 20,000 ints or so, this one repeats after 65,536 and the Histogram is very flat.

In both cases I started with the >3567 seed value used by TI-BASIC just for consistency. (Thanks Lee)

Increased my histogram samples to 320 to give all 32 buckets a potential 10 random numbers which seemed a better way to do it.


Might be useful to someone making games.



For reference I re-did the TI-BASIC histogram using 320 random samples.




Edited by TheBF
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...