6702 emulator in Commodore Basic

From: William Levak <wlevak_at_SDF.ORG>
Date: Thu, 9 Aug 2012 04:41:38 +0000 (UTC)
Message-ID: <Pine.NEB.4.64.1208090412340.5253@sdf.lonestar.org>
Below is the listing of the 6702 emulator as it ended up.  The description 
of how it works is in my previous messages on the 6702 chip.  The emulator 
not only calculates the correct output, but it does it the same way as the 
6702 does, or as close as Commodore Basic is able to do it.

A few lines need further explanation:

   line 160 is a random number generator.  Just remove the REM and the 
program will supply random numbers instead of input.  This line is 
branched to. If you leave it out, you will need to change the goto on line
190.

   line 200:  Commodore Basic does not have an exclusive or, so I use the 
logical equivalent:

           (A EOR B) = (A OR B) - (A AND B)

   line 260  Commodore Basic does not have integer subtraction.  All 
subtraction is done as floating point.  Renormalization could introduce 
random bits in the low order bits.  Since this a cumulative total that is 
the most important number in the program, I have taken the precaution to 
use the INT function to make sure it is represented internally as an 
integer.


When you run the program, it will ask for input.  If the 6702 does not act 
on the input, it will simply ask for more input.  If the 6702 acts on the 
input, the program will print out 4 numbers.  The first is just a counter 
of how many numbers the 6702 has accepted.  The second is the calculated 
output of the 6702.  The third is the actual output of the 6702.  The last 
number is the cumulative total of how many differences there were between 
the actual and calculated values.  If you run this program on a computer 
other than the SuperPet, the last 2 numbers will be nonsense.


By now, I have run several hundred thousand random numbers through this 
program.  The last number should always be zero.


100 dims(7,7)
110 l(0)=6:l(1)=3:l(2)=7:l(3)=8:l(4)=1:l(5)=3:l(6)=5:l(7)=2
120 b(0)=1:b(1)=2:b(2)=4:b(3)=8:b(4)=16:b(5)=32:b(6)=64:b(7)=128
130 t=214:ln=214
140 t2=peek(61408):ift<>t2thene=e+1
150 printn,t,t2,e
160 rem n2=int(256*rnd(ti)):print"next number?"n2:goto180
170 input"next number";n2
180 poke61408,n2
190 if(n2andeo)=0theneo=1-(n2and1):goto160
200 mk=(lnorn2)-(lnandn2)
210 fori=0to7:ifmkandb(i)thens(i,c(i))=1-s(i,c(i))
220 next
230 fori=0to7:c(i)=c(i)+1:ifc(i)=l(i)thenc(i)=0
240 next:ln=n2:eo=1-(n2and1)
250 fori=0to7:ifs(i,c(i))=0goto280
260 ifb(i)andtthent=int(t-b(i)):goto280
270 t=b(i)ort
280 next
290 n=n+1:goto140

wlevak@sdf.lonestar.org
SDF Public Access UNIX System - http://sdf.lonestar.org

       Message was sent through the cbm-hackers mailing list
Received on 2012-08-09 05:00:04

Archive generated by hypermail 2.2.0.