Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
 Welcome to our latest new user maak ! (Registered 2024-04-18) You are not logged in - nap
CSDb User Forums


Forums > C64 Coding > Basic emulator
2020-12-22 15:57
Sasq

Registered: Apr 2004
Posts: 155
Basic emulator

How much of the C64 do you need to emulate to run (poke free) basic programs ?
2020-12-22 16:00
JackAsser

Registered: Jun 2002
Posts: 1987
You need the cpu, rudimentary cia impl with timer and keyboard input, a hacked vic (kernel poll d012) + rom and ram correctly mapped. Did one i JavaScript to emulate putchar for a terminal program
2020-12-22 16:06
Sasq

Registered: Apr 2004
Posts: 155
CIA is what will require some work I guess, especially keyboard.

What interrupts needs to be generated, only timer ?
2020-12-22 16:08
Sasq

Registered: Apr 2004
Posts: 155
...and do you still have the source ?
2020-12-22 16:53
chatGPZ

Registered: Dec 2001
Posts: 11101
you only need cia ports and irq that fires 60 times a second... all the rest you can ignore

what are you trying to do though? are you aware of MiSTs basic interpreter?
2020-12-22 17:09
Sasq

Registered: Apr 2004
Posts: 155
I have 6502 and petscii/text mode emulation already, and mapped basic/kernal so I just wanted to to go a step further.

(as mentioned in the bass thread;)

https://youtu.be/g1uuhz7Qupw
2020-12-22 17:21
JackAsser

Registered: Jun 2002
Posts: 1987
Quote: ...and do you still have the source ?

I’ll have a look! Send you on FB later tonight
2020-12-22 19:05
tlr

Registered: Sep 2003
Posts: 1703
If poke-free, you only need small parts (I/O) of the kernal to run basic in emulation and those parts may be stubbed off.

I do this here: blitz 0.1
It runs a blitz compiled binary but that's more or less the same as running a basic program.

Disclaimer: I may have left out a few hooks if they weren't required for blitz.
2020-12-22 19:15
Sasq

Registered: Apr 2004
Posts: 155
Hmm yes that seems to be more about intercepting the kernal than implementing the necessary CIA stuff.

Although I am interested in this too.
2020-12-22 19:22
tlr

Registered: Sep 2003
Posts: 1703
Quote: Hmm yes that seems to be more about intercepting the kernal than implementing the necessary CIA stuff.

Although I am interested in this too.


The point of my example being: the basic interpreter doesn't really require any hardware, only the screen editor does and that's in the kernal.
2020-12-23 01:24
Martin Piper

Registered: Nov 2007
Posts: 631
If you patch out some raster waits etc. You don't even need I/O or CIA. Just CPU will do
2020-12-23 03:19
chatGPZ

Registered: Dec 2001
Posts: 11101
If you want keyboard input, you certainly need the timer irq :)
2020-12-23 05:49
Martin Piper

Registered: Nov 2007
Posts: 631
Quote: If you want keyboard input, you certainly need the timer irq :)

Not really. It's possible to fake keys into the right locations in zero page and stub out the relevant routines.
2020-12-23 09:59
JackAsser

Registered: Jun 2002
Posts: 1987
Quote: Not really. It's possible to fake keys into the right locations in zero page and stub out the relevant routines.

I found it easier to just let a timer call ($fffe) and fake the dc00/1/d registers.
2020-12-23 11:15
Sasq

Registered: Apr 2004
Posts: 155
I'm doing something similar, and it seems to work.

So what I was hoping to look at some source code for converting incoming key presses to dc00/dc01 matrix data...

I guess I also want to fake loading... maybe intercepting FFD5 and copying in the data while skipping the actual load routine somehow ?
2020-12-23 11:29
Martin Piper

Registered: Nov 2007
Posts: 631
If you want to fake the cd00/01 values, you need to convert from key to keyboard matrix values: https://www.c64-wiki.com/wiki/Keyboard#Keyboard_Matrix

And also take note of the data direction registers, and also respond to the "request" to poll a certain column.

Which is not hard, but just a bit picky to get really correct.

Hence why I suggest stubbing out (or intercepting) kernal routines and plonking values into the keyboard buffer memory instead. :)
2020-12-23 16:24
BiGFooT

Registered: Mar 2002
Posts: 31
Well... I'm not sure if this will help or not, but here's a table I've used in my emulator experiments.

keyb_rowcol[256]={
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x72, 0xff, 0xff, 0xff, 0x01, 0xff, 0xff,
0x17, 0x75, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff, 0xff,
0x74, 0xff, 0xff, 0xff, 0x63, 0x02, 0x07, 0x02, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x43, 0x70, 0x73, 0x10, 0x13, 0x20, 0x23, 0x30, 0x33, 0x40, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0x12, 0x34, 0x24, 0x22, 0x16, 0x25, 0x32, 0x35, 0x41, 0x42, 0x45, 0x52, 0x44, 0x47, 0x46,
0x51, 0x76, 0x21, 0x15, 0x26, 0x36, 0x37, 0x11, 0x27, 0x31, 0x14, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x04, 0x04, 0x05, 0x05, 0x06, 0x06, 0x03, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x55, 0x53, 0x57, 0x50, 0x54, 0x67,
0x71, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x56, 0x65, 0x61, 0x62, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};

The upper nibble is the Nth bit of PA, the lower nibble is the Nth bit of PB.

if (keyb_rowcol != $ff)
{
$dc00 = 1 << (keyb_rowcol[keycode >> 4])
$dc01 = 1 << (keyb_rowcol[keycode & $0f])
}

If it helps then I can give you a complete emulator, standalone basic source, (de)tokenizer and even a pc native cbm basic interpreter.
2020-12-25 08:06
Sasq

Registered: Apr 2004
Posts: 155
I only handle dc00/dc01 keys and run the IRQ, and it seems to work, excpet if I try RUN I get:

READY.                                                              
RUN                                                                 
                                                                                               
?UNDEF'D STATEMENTBAD SUBSCRIPTREDIM'D A                            
RRAYDIVISION BY ZEROILLEGAL DIRECTTYPE M                            
ISMATCHSTRING TOO LONGFILE DATAFORMULA T                            
OO COMPLE  ERROR                                                    
READY.


What could be wrong ?
2020-12-25 13:41
tlr

Registered: Sep 2003
Posts: 1703
The strings are ended with the last char having the MSB set, maybe a clue?

I'm assuming your cpu-core passes rudimentary testsuites. In that case, have you stubbed of CHROUT and forgot to return registers+flags in a state similar to what the kernal does?
2020-12-25 22:29
mankeli

Registered: Oct 2010
Posts: 110
Probably you don't even need a full cpu. Iirc I managed to implement enough 6510+SID in few hours to play back goatse songs.
2020-12-25 23:05
JackAsser

Registered: Jun 2002
Posts: 1987
Quote: I only handle dc00/dc01 keys and run the IRQ, and it seems to work, excpet if I try RUN I get:

READY.                                                              
RUN                                                                 
                                                                                               
?UNDEF'D STATEMENTBAD SUBSCRIPTREDIM'D A                            
RRAYDIVISION BY ZEROILLEGAL DIRECTTYPE M                            
ISMATCHSTRING TOO LONGFILE DATAFORMULA T                            
OO COMPLE  ERROR                                                    
READY.


What could be wrong ?


Have you ”booted” up using the reset-vector at all? Is memory read/write under rom properly implemented so the free mem calcs gets correcto etc?
2020-12-25 23:40
Krill

Registered: Apr 2002
Posts: 2825
Quoting JackAsser
Have you ”booted” up using the reset-vector at all? Is memory read/write under rom properly implemented so the free mem calcs gets correcto etc?
My bet is on what tlr suggested. BASIC VM needs very little physical machine action to function properly. Where does C-64's BASIC V2 actually reach to RAM beyond $9FFF? =)
2020-12-26 06:29
Martin Piper

Registered: Nov 2007
Posts: 631
When I was running kernal and basic ROMs on my TTL CPU I didn't need to support ROM RAM banking.
2020-12-26 08:20
Sasq

Registered: Apr 2004
Posts: 155
Banking is not implemented.
A000-BFFF and E000-FFFF is read only, ram below is not accessible.
IO areas does not wrap around correctly, and most registers just return 0.
CPU should be good, tested with the Klaus Dormann 6502 test.
(although interrupts are newly added and has not been well tested).
2020-12-26 09:35
JackAsser

Registered: Jun 2002
Posts: 1987
Quote: Quoting JackAsser
Have you ”booted” up using the reset-vector at all? Is memory read/write under rom properly implemented so the free mem calcs gets correcto etc?
My bet is on what tlr suggested. BASIC VM needs very little physical machine action to function properly. Where does C-64's BASIC V2 actually reach to RAM beyond $9FFF? =)


Referring to the mem test loop to determine basic bytes free
2020-12-26 10:58
Sasq

Registered: Apr 2004
Posts: 155
So write goes through to the underlying RAM, but there is no way to read so I don't see that it matters.

How does the memory test work ?
2020-12-26 11:44
JackAsser

Registered: Jun 2002
Posts: 1987
Quote: So write goes through to the underlying RAM, but there is no way to read so I don't see that it matters.

How does the memory test work ?


Part of initialize sys constants: http://unusedino.de/ec64/technical/aay/c64/romfd50.htm

It writes forward in memory #$55 and reads it back until it doesn’t get #$55 anymore, i.e. hitting ROM.

Basic then calls http://unusedino.de/ec64/technical/aay/c64/rome3bf.htm to get min/max RAM pointers to calc avail memory for basic, and set up stack and string buffers properly.
2020-12-26 12:37
chatGPZ

Registered: Dec 2001
Posts: 11101
just preload the initial ram dump (fish out of hoxs64 directory) and stub out the ram test/init junk :)
2020-12-26 13:24
JackAsser

Registered: Jun 2002
Posts: 1987
Quote: just preload the initial ram dump (fish out of hoxs64 directory) and stub out the ram test/init junk :)

That works!
2020-12-26 16:28
Sasq

Registered: Apr 2004
Posts: 155
So.. the weird error messages turned out to be because my PLA did not affect flags (need to use a better 6502 reference :)

I still now have the problem that RUN always reports
"UNDEF'ED STATEMENT"
2020-12-26 16:49
Sasq

Registered: Apr 2004
Posts: 155
So it's just RUN with no argument that doesn't work.

10 PRINT "HEY"
20 GOTO 10
RUN 10

works

maybe the parser seems some extra character at EOL ?

Does the line parser look at the screen or does it remember typed chars ? Because cursor blinking does not work properly.
2020-12-26 17:15
Martin Piper

Registered: Nov 2007
Posts: 631
The line parser looks at the screen data.

"RUN" with something in the screen other than spaces can produce "UNDEF'D STATEMENT" errors.
2020-12-26 17:18
Martin Piper

Registered: Nov 2007
Posts: 631
Cursor flash is done by the standard IRQ routine usually found at $ea31:

>> Flash cursor code starts
EA34 A5 CC LDA $CC ; flash cursor
EA36 D0 29 BNE $EA61
EA38 C6 CD DEC $CD
EA3A D0 25 BNE $EA61
EA3C A9 14 LDA #$14
EA3E 85 CD STA $CD
EA40 A4 D3 LDY $D3
EA42 46 CF LSR $CF
EA44 AE 87 02 LDX $0287
EA47 B1 D1 LDA ($D1),Y
EA49 B0 11 BCS $EA5C
EA4B E6 CF INC $CF
EA4D 85 CE STA $CE
EA4F 20 24 EA JSR $EA24
EA52 B1 F3 LDA ($F3),Y
EA54 8D 87 02 STA $0287
EA57 AE 86 02 LDX $0286
EA5A A5 CE LDA $CE
EA5C 49 80 EOR #$80
EA5E 20 1C EA JSR $EA1C ; display cursor
2020-12-26 18:16
tlr

Registered: Sep 2003
Posts: 1703
Quoting Sasq
So.. the weird error messages turned out to be because my PLA did not affect flags (need to use a better 6502 reference :)

As suspected, though it really surprises me that the Dormann tests wouldn't check that PLA sets flags correctly.
As an alternative I can recommend Wolfgang Lorenz' testsuite which seems to be fairly extensive. I used that for the nu6502 core in blitz.
2020-12-26 18:32
Sasq

Registered: Apr 2004
Posts: 155
Checking screen memory, "RUN" is followed by SPACE so that looks right...
2020-12-26 19:16
tlr

Registered: Sep 2003
Posts: 1703
Quote: Checking screen memory, "RUN" is followed by SPACE so that looks right...

Test with "RUN:" and see if that changes anything.

If there are uncertainties about the CPU, debugging this may be really tedious though. Maybe there are other flag issues?
2020-12-26 19:30
Sasq

Registered: Apr 2004
Posts: 155
I actually just tried colon and it didn't help. Yeah next step should be checking out the suggested test suite
2020-12-26 19:37
tlr

Registered: Sep 2003
Posts: 1703
The CHRGET/CHRGOT routine at $0073 does a bit of flag setting related to parsing, might be another clue.
2020-12-27 02:21
chancer

Registered: Apr 2003
Posts: 342
due to how poor the basic is, maybe making routines based on other machines would be good. OR an optimized Simons basic etc. the BBC micro is good basic wise BUT that has it's issues also. i.e. 20k of ram taken with screen (out of 32k)

things like laser basic / oasis.. etc. ran too slow and took up too much ram.

I doubt I'm the only one who had another machine, and thought WHY is the basic so poor, given the nice hardware.
2020-12-27 10:09
Oswald

Registered: Apr 2002
Posts: 5017
I had no other machine and had no idea basic could be any better for a long time :) Graphics Basic is the best extension btw it has things you havent even dreamed off.
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
Hok/Remember
Guests online: 63
Top Demos
1 Next Level  (9.8)
2 Mojo  (9.7)
3 Coma Light 13  (9.7)
4 Edge of Disgrace  (9.6)
5 Comaland 100%  (9.6)
6 No Bounds  (9.6)
7 Uncensored  (9.6)
8 Wonderland XIV  (9.6)
9 The Ghost  (9.6)
10 Bromance  (9.6)
Top onefile Demos
1 It's More Fun to Com..  (9.8)
2 Party Elk 2  (9.7)
3 Cubic Dream  (9.6)
4 Copper Booze  (9.5)
5 Rainbow Connection  (9.5)
6 Wafer Demo  (9.5)
7 TRSAC, Gabber & Pebe..  (9.5)
8 Onscreen 5k  (9.5)
9 Dawnfall V1.1  (9.5)
10 Quadrants  (9.5)
Top Groups
1 Oxyron  (9.3)
2 Nostalgia  (9.3)
3 Booze Design  (9.3)
4 Censor Design  (9.3)
5 Crest  (9.3)
Top Crackers
1 Mr. Z  (9.9)
2 S!R  (9.9)
3 Mr Zero Page  (9.8)
4 Antitrack  (9.8)
5 OTD  (9.8)

Home - Disclaimer
Copyright © No Name 2001-2024
Page generated in: 0.079 sec.