6502 emulation on Gigatron

Topics pertaining to the emulation or simulation of the 65xx microprocessors and their peripheral chips.
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: 6502 emulation on Gigatron

Post by BigEd »

Hmm, it doesn't seem to like line 50. I entered it, it lists as blank, and I can't add line 60 either.
User avatar
mvk
Posts: 81
Joined: 21 Mar 2017

Re: 6502 emulation on Gigatron

Post by mvk »

I'll have to call Bill Gates for advice
User avatar
mvk
Posts: 81
Joined: 21 Mar 2017

Re: 6502 emulation on Gigatron

Post by mvk »

After a lengthy phone conversation, the line 50 issue is found. However, Bill noted that "DIV" and "VDU7" are still "?SYNTAX ERROR's" [his words].
MSBASIC.gt1.zip
(7.68 KiB) Downloaded 249 times
Edit: And so are "FOR J%" and "FOR I%". I tried to fix the program, but the interpreter then crashes. So another debugging session is called for.
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: 6502 emulation on Gigatron

Post by BigEd »

Thanks for the fix! I do believe I've read somewhere that MS Basic doesn't allow loop variables to be integers, so that's most likely as expected. VDU 7 is just a beep, and in this case DIV is rather like divide, possibly with an INT, so we should be good to go.
Chromatix
Posts: 1462
Joined: 21 May 2018

Re: 6502 emulation on Gigatron

Post by Chromatix »

Right - DIV is provided in floating-point BASICs to explicitly perform an integer division, usually accompanied by a MOD keyword to provide the corresponding remainder. In an integer BASIC there is no need for them.
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: 6502 emulation on Gigatron

Post by BigEd »

Hmm, next obstacle with the Pi spigot, I get an out of memory error at line 90. Is the expression too complex? I'd only entered up to line 110 and ran with an input of 9.
User avatar
mvk
Posts: 81
Joined: 21 Mar 2017

Re: 6502 emulation on Gigatron

Post by mvk »

As said:
mvk wrote:
I tried to fix the program, but the interpreter then crashes. So another debugging session is called for.
Regarding out of memory: It should be able to do two FOR levels. (Three levels is still too much.)
Last edited by mvk on Thu Oct 24, 2019 11:33 am, edited 2 times in total.
User avatar
mvk
Posts: 81
Joined: 21 Mar 2017

Re: 6502 emulation on Gigatron

Post by mvk »

Chromatix wrote:
Right - DIV is provided in floating-point BASICs to explicitly perform an integer division, usually accompanied by a MOD keyword to provide the corresponding remainder. In an integer BASIC there is no need for them.
Micro-Soft BASIC is a floating point BASIC (that was the whole point). It doesn't have DIV. This program isn't an MS BASIC program, so it must be rewritten.
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: 6502 emulation on Gigatron

Post by BigEd »

Oh, sorry, I missed that there was still work in progress.

(In this case, / will work just as well as DIV - the change from / to DIV was purely for performance on a Beeb.)
User avatar
mvk
Posts: 81
Joined: 21 Mar 2017

Re: 6502 emulation on Gigatron

Post by mvk »

I wonder if all that %-fuss really makes much of a difference. All calculations are done as floating point anyway. Even the "bytes free" message is printed by the floating point routine.
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: 6502 emulation on Gigatron

Post by BigEd »

It certainly helps in BBC Basic, and I think I've seen it help in MS Basic in the past. That's when I would have come across the oddity that loop variables must be floats.
User avatar
mvk
Posts: 81
Joined: 21 Mar 2017

Re: 6502 emulation on Gigatron

Post by mvk »

All these back-and-forth conversions make integer variables slower in MS BASIC. I can only see their point in large arrays. This is easy to test:
Screenshot 2019-10-24 at 17.32.12.png
Result in video mode 3:
Screenshot 2019-10-24 at 17.19.38.png
This is not a completely fair comparison because the GOTO20 is a bit faster than the GOTO50. But if you remove line 10-40, the integer loop still runs slower (3337 ticks).

This now also gives us our apples-to-apples benchmark reference, without having to port another Pi program (we already have one for TinyBASIC of course).
Screenshot 2019-10-24 at 17.22.53.png
Hmm, I wonder how we can compute their ratio? Oh wait, there's only one way that's appropriate:
Screenshot 2019-10-24 at 17.28.55.png
Wow: non-trivial machine code v6502 is only 5.7 times slower than the real thing?! Perhaps that's too good to be true, Edit: but confirmed by a stopwatch. This debunks the "v6502 is 10 times slower than the original 6502"-myth, and refines our earlier "8x slowdown" guesstimate. In reality we're running very close to 75 KIPS, or 3 instructions per scanline. Sweet 8)
User avatar
mvk
Posts: 81
Joined: 21 Mar 2017

Re: 6502 emulation on Gigatron

Post by mvk »

BigEd wrote:
Hmm, next obstacle with the Pi spigot, I get an out of memory error at line 90. Is the expression too complex? I'd only entered up to line 110 and ran with an input of 9.
Indeed your program is "too complex" for the miniature stack it gets. The stack smashes the CHRGET/CHRGOT routines that live in the zero page. The only solution is some interpreter rewriting to create more space. These routines alone eat 24 precious bytes. But first we should run this in a PET emulator to figure out how many stack bytes the program needs. Some other time... Fixing this will be a weekend project.
User avatar
mvk
Posts: 81
Joined: 21 Mar 2017

Re: 6502 emulation on Gigatron

Post by mvk »

I hacked CHRGET such that it doesn't need to be copied to the zero page. It means there's a 3.5% speed penalty, as a proper solution is way too much work. But it appears to give your program just enough stack space to run to completion. From the commit notes:

Code: Select all

commit f87436760c751280d22ad3586ea0ea55e01bbad2 (HEAD -> master, master/master)
Date:   Tue Oct 29 11:35:16 2019 +0100

    MSBASIC: Don't copy CHRGET/CHRGET to zero page
    
    To free up more zero page and allow for a deeper stack. TXTPTR is
    still in zero page, so the program doesn't grow a lot. We now do:
    
                    sty SCRATCH
                    ldy #0
                    lda (TXTPTR),y
                    ldy SCRATCH
    
    instead of:
    
            TXTPTR=*+1
                    lda 60000       ; Self modifying operand
        
    All this incurs a slow down of 3.5% in benchmarks. This speed might
    be won back if we put the responsibility of saving Y to the caller
    (as must have been done in CBM-128 BASIC 7.0). But there are so many
    calling sites, this can be too much work.
    
    This benchmark now works:
    
            10 INPUT"HOW MANY DIGITS";N
            20 T=TIME
            30 L=INT(10*N/3)+1:DIM A(L)
            40 Z$="000000":T$="999999"
            50 FOR I=1TOL:A(I)=2:NEXT
            60 M=0:P=0
            70 FOR J=1TON:Q=0:K=2*L+1
            80 FOR I=L TO 1 STEP -1
            90 K=K-2:X=10*A(I)+Q*I
            100 Q=INT(X/K):A(I)=X-Q*K
            110 NEXT
            120 Y=INT(Q/10):A(1)=Q-10*Y:Q=Y
            130 IF Q=9 THEN M=M+1:GOTO170
            140 IF Q>9 THEN PRINT CHR$(49+P);LEFT$(Z$,M);:GOTO170
            150 PRINT CHR$(48+P);LEFT$(T$,M);
            160 P=Q:M=0
            170 NEXT
            180 PRINT CHR$(48+P)
            190 PRINT (TIME-T)/60
    
    Results:
    
            Micro-Soft BASIC        28 digits       510 s   (radix 10)
            Micro-Soft BASIC        14 digits       130 s   (radix 10)
    
    For reference:
    
            10 REM 28 digits of pi
            20 M=94:FOR I=0 TO M-1
            30 Z(I)=20:NEXTI:?"PI=";
            40 E=D%100:D=0:FORI=1TOM
            50 J=M-I:D=D+D*J+Z(J)*100
            60 A=J+J+1:Z(J)=D%A:D=D/A
            70 NEXTI:M=M-6:E=E+D/100
            80 ?E/10;:IF M=88 ?".";
            90 ?E%10;:IF M>10 GOTO 40
    
    Result:
    
            Tiny BASIC              28 digits       65 s    (radix 100)

Updated MSBASIC.gt1 binary is in GitHub as well.
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: 6502 emulation on Gigatron

Post by BigEd »

Hurrah! Well done.
Post Reply