Arduino Forum

Community => Exhibition / Gallery => Topic started by: miker00lz on Oct 14, 2013, 05:27 am

Title: Arduino 6502 emulator + BASIC interpreter
Post by: miker00lz on Oct 14, 2013, 05:27 am
I wrote a 6502 CPU emulator for the Arduino! I originally wrote it a couple years ago for a NES emulator, then modified it a bit for this. One of the changes was to make cycle timing a little less accurate (doesn't check for page boundary crosses on some opcodes) for a speed increase. If anybody wants that fixed for something, let me know.

To demonstrate it, I have the ROM for "Enhanced BASIC 6502" embedded in the code. Given the Uno's tiny 2 KB RAM, I am only able to provide the CPU emulator with 1.5 KB, but it's enough to minimally run EhBASIC! You can connect to the Arduino with any terminal program (like PuTTY) and play around in the BASIC interpreter.  8)

(http://rubbermallet.org/arduino-ehbasic.png)

It makes for a fun little toy, even if not all that useful. With more memory on an Arduino, you could turn this into a simple Apple ][ emulator or something like that. I might look into that actually. If anybody actually uses this for a project let me know if you need help.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: JimboZA on Oct 14, 2013, 05:33 am
Brilliant...

6502.... Apple ][.... that takes the clock back a bit!
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: miker00lz on Oct 14, 2013, 05:36 am
Thanks! :)

I actually forgot to attach the project to the post, d'oh! Doing that now...
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: fungus on Oct 14, 2013, 08:57 am
Ideas:

Store the program in EEPROM and get more than 767 bytes. It would also survive a reset.

Set up some virtual addresses to change the state of Arduino pins using poke().



Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: miker00lz on Oct 14, 2013, 09:03 am

Ideas:

Store the program in EEPROM and get more than 767 bytes. It would also survive a reset.

Set up some virtual addresses to change the state of Arduino pins using poke().


The program (as in the EhBASIC ROM image) is stored in the flash memory along with the code. The emulator has access to 1536 bytes of RAM, but the first 512 bytes make up the 6502's zero page and stack page, then the next 256 are used by the BASIC interpreter itself. The 767 bytes is just what EhBASIC reports as the remaining storage for BASIC code and variables.

I wouldn't want to use EEPROM because it can only be written to so many times before it goes bad. I'm looking into getting a 32 KB SPI chip for more memory. Should be pretty simple. :)
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: fungus on Oct 14, 2013, 10:10 am

The program (as in the EhBASIC ROM image) is stored in the flash memory along with the code. The emulator has access to 1536 bytes of RAM, but the first 512 bytes make up the 6502's zero page and stack page, then the next 256 are used by the BASIC interpreter itself. The 767 bytes is just what EhBASIC reports as the remaining storage for BASIC code and variables.


I meant store the BASIC program in EEPROM (and variables in RAM).


I wouldn't want to use EEPROM because it can only be written to so many times before it goes bad.


It's a lot of times though. You could probably spend a lifetime writing BASIC and not wear it out (how long would it take you to make 100,000 edits to a program?)

More ideas: Make it so that if you connect a special pin to GND it automatically runs the BASIC program (stored in EEPROM) on boot-up. That way you could program it in BASIC and deploy it in real projects.

Map the analog ports to memory addresses so if you peek() them you read the analog value (only 8 bits, but hey...the bottom 2 bits are usually just noise anyway)
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: miker00lz on Oct 14, 2013, 10:43 am
Good ideas, but I think you are misunderstanding how it works a little bit. I wrote no code of this BASIC interpreter. It is a 6502 program that already existed. I only wrote a 6502 CPU emulator that runs this interpreter. I have no way of knowing where in the 6502 memory space the relevant BASIC program information is, otherwise I might do it. This of course means I have no way to auto-load a program and start it either.

I would have to do all 6502 memory space read/write to the EEPROM, and it could be writing to memory constantly depending on the 6502 machine code being run. It may wear out parts of the EEPROM in a matter of seconds actually!  :smiley-mr-green:

Besides, the BASIC interpreter I'm emulating is really just a simple demonstration for the core concept, the 6502 emulator. I do like your ideas, they just aren't possible to implement with the way this thing works.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: fungus on Oct 14, 2013, 11:17 am

Good ideas, but I think you are misunderstanding how it works a little bit. I wrote no code of this BASIC interpreter. It is a 6502 program that already existed. I only wrote a 6502 CPU emulator that runs this interpreter. I have no way of knowing where in the 6502 memory space the relevant BASIC program information is, otherwise I might do it. This of course means I have no way to auto-load a program and start it either.


Oh, I see...

It might work if you put page0/page1 in RAM and the rest in EEPROM but the BASIC variables would probably end up in EEPROM too, not good.

OH, well, they were just ideas. :-)
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: robtillaart on Oct 14, 2013, 02:42 pm
try it on a MEGA to have more RAM?
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: miker00lz on Oct 14, 2013, 10:07 pm


Good ideas, but I think you are misunderstanding how it works a little bit. I wrote no code of this BASIC interpreter. It is a 6502 program that already existed. I only wrote a 6502 CPU emulator that runs this interpreter. I have no way of knowing where in the 6502 memory space the relevant BASIC program information is, otherwise I might do it. This of course means I have no way to auto-load a program and start it either.


Oh, I see...

It might work if you put page0/page1 in RAM and the rest in EEPROM but the BASIC variables would probably end up in EEPROM too, not good.

OH, well, they were just ideas. :-)



It would be cool to save the programs, for sure! :)
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: miker00lz on Oct 14, 2013, 10:09 pm

try it on a MEGA to have more RAM?


I was kind of thinking about that, but the again it still only has 8 KB. I think the best solution would be a cheap 23A256 for an instant 32 KB.

https://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en539040

Maybe even the 23A512 for 64 KB, then an Apple ][ could be emulated. :)
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: Manawyrm on Oct 15, 2013, 12:26 pm
A very cool piece of code, nice work.
I have to look into it tonight.

If I'm reading your code correctly, to enable the use of external -- say SPI memory you would only need to write read6502() and write6502() for your SPI flash, right? Very cool :)

Also: Have you thought about mapping a whole arduino I/O Port to the memory? That would enable for a HELL OF A LOT of mega cool features. Just think about printer integration, maybe even floppy.
Or maybe even go one step further: Reading programs from a cassette tape *scnr*
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: miker00lz on Oct 16, 2013, 12:28 am

A very cool piece of code, nice work.
I have to look into it tonight.

If I'm reading your code correctly, to enable the use of external -- say SPI memory you would only need to write read6502() and write6502() for your SPI flash, right? Very cool :)


Thanks! Yep, that's right. Those are the only two functions you need to worry about. If you were to get at least 48 KB of SPI memory you could emulate the Apple ][. :)


Quote
Also: Have you thought about mapping a whole arduino I/O Port to the memory? That would enable for a HELL OF A LOT of mega cool features. Just think about printer integration, maybe even floppy.
Or maybe even go one step further: Reading programs from a cassette tape *scnr*


The thought crossed my mind. It would be cool to experiment with. You could also use a microSD shield to store Apple disk images on. You would need something like the Mega 2560 for that, though. There isn't enough program storage on an Uno to link in the SD card functions and still have the BIOS embedded with it, I tried.

Cassette should also be reasonably easy to add. I wrote an Apple emu for Windows a while back with cassette support (via WAV files), and the interface is unbelievably simple.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Oct 29, 2013, 09:34 pm
The program (as in the EhBASIC ROM image) is stored in the flash memory along with the code. The emulator has access to 1536 bytes of RAM, but the first 512 bytes make up the 6502's zero page and stack page, then the next 256 are used by the BASIC interpreter itself. The 767 bytes is just what EhBASIC reports as the remaining storage for BASIC code and variables.

I wouldn't want to use EEPROM because it can only be written to so many times before it goes bad. I'm looking into getting a 32 KB SPI chip for more memory. Should be pretty simple. :)
[/quote]

Hi
I hacked around abit with your emulator and patched Read/Write6502.

I used a 24LC256 32Kb EEPROM for memorystorage.
It can take 1000000 writes before going bad.

To be safe I mapped the first 768bytes to Arduino RAM and the rest is EEPROM space.
That should keep it relative safe even though the basic variables gets written in EEPROM when the basicprogram runs.

It's just a first test any as I plan to run CBM-basic on it for Vic-20/C64 emulation.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: fungus on Oct 29, 2013, 10:28 pm

Hi
I hacked around abit with your emulator and patched Read/Write6502.

I used a 24LC256 32Kb EEPROM for memorystorage.
It can take 1000000 writes before going bad.

To be safe I mapped the first 768bytes to Arduino RAM and the rest is EEPROM space.
That should keep it relative safe even though the basic variables gets written in EEPROM when the basicprogram runs.

It's just a first test any as I plan to run CBM-basic on it for Vic-20/C64 emulation.


I assume you can't put everything in EEPROM or it will be hellishly slow.

Zero page and stack can be in Arduino RAM, the program could be in EEPROM, but where are the BASIC variables stored? On most interpreted BASICs they start at the top of RAM and work down towards where the program is. That makes it hard to detect what goes in RAM and what goes in EEPROM. If the variables are in EEPROM it could slow things down a lot.

There might be a system variable you can look at to see where the end of the program is. Everything above that (ie. vars) goes in RAM, everything below (ie. program) goes in EEPROM.

(maybe)

Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Oct 30, 2013, 09:05 am
It wasnt that slow.

FOR I=0 TO 1000:NEXT completes in 30sec.
I Think it should be around 1sec and the problem is the Arduino I2C that runs at 100KHz.

Can the Wire library run at 1MHz?
That would make it more up to speed?

Or read/write caching memory in RAM?
Like a CPU-cache?
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Oct 30, 2013, 11:59 pm
Ok, the FOR I=0 TO 1000:NEXT loop is now down to 3sec.
That is a tenfold in speed :)

I implemented a CPU FIFO L1-Cache with 4 TLB buffers of 128bytes.
It works great, even with random PEEKs all over the EEPROM.

If the data is in the cache it is returned from RAM.
Else the oldest buffer gets flushed (written if dirty) and a new is read in from the EEPROM.

But there is still a bit of penelty when there is a cache-miss on writes.

Perhaps   a greater number of smaller buffers would be better?
Like 32buffers with 16bytes?

How many bytes does EhBasic use for different variables not including strings?
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: fungus on Oct 31, 2013, 06:34 am

Ok, the FOR I=0 TO 1000:NEXT loop is now down to 3sec.
That is a tenfold in speed :)

I implemented a CPU FIFO L1-Cache with 4 TLB buffers of 128bytes.
It works great, even with random PEEKs all over the EEPROM.


Good idea!


Perhaps   a greater number of smaller buffers would be better?
Like 32buffers with 16bytes?


Only one way to find out...

I'm fairly sure the i2c can be made faster, too.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Oct 31, 2013, 09:08 am
Yes, I changed the i2c speed to 400KHz with:
Code: [Select]

Wire.begin();
TWBR = 12;


It can still be optimized more by using block read/write.

Here is the code for the 4 buffer FIFO L1-Cache.
The writecache resides in the readcache function and only writes back a buffer if its dirty.

Code: [Select]

 void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data ) {
 int cache;
 int page=(eeaddress >> 7);
 if (cachepage[0]==page) {
  cacheram[(eeaddress & 127)]=data;
  cachedirty[0]=1;
  return;
 }
 if (cachepage[1]==page) {
  cacheram[(eeaddress & 127)+128]=data;
  cachedirty[1]=1;
  return;
 }  
 if (cachepage[2]==page) {
  cacheram[(eeaddress & 127)+256]=data;
  cachedirty[2]=1;
  return;
 }
 if (cachepage[3]==page) {
  cacheram[(eeaddress & 127)+384]=data;
  cachedirty[3]=1;
  return;
 }
 readcache(page,nextcache);
 cache=nextcache;
 nextcache++;
 if (nextcache>3) nextcache=0;
 cacheram[(eeaddress & 127)+(cache << 7)]=data;
 cachedirty[cache]=1;  
}

byte readEEPROM(int deviceaddress, unsigned int eeaddress ) {
 int cache;
 int page=(eeaddress >> 7);
 if (cachepage[0]==page) return cacheram[(eeaddress & 127)];
 if (cachepage[1]==page) return cacheram[(eeaddress & 127)+128];
 if (cachepage[2]==page) return cacheram[(eeaddress & 127)+256];
 if (cachepage[3]==page) return cacheram[(eeaddress & 127)+384];
 readcache(page,nextcache);
 cache=nextcache;
 nextcache++;
 if (nextcache>3) nextcache=0;
 return cacheram[(eeaddress & 127)+(cache << 7)];
}
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: fungus on Oct 31, 2013, 10:13 am

The writecache resides in the readcache function and only writes back a buffer if its dirty.


Your cache misses are very expensive (you have to use I2C!)  so more, smaller pages is probably good.


Code: [Select]

int page=(eeaddress >> 7);
if (cachepage[0]==page) ...



That code will optimize horribly on Arduino. Bit shifting 16-bit values is really slow, the compiler usually generates a loop for it.

You can eliminate all the if statements and bit shifting by using a simple hash, eg.

Code: [Select]

// eg. Sixteen pages of sixteen bytes each
byte cachePage[16];
byte cacheRam[16][16];

byte a = byte(eeaddress);  // Bottom 8 bits of address
byte p = eeaddress>>8;     // Page of RAM we need (the compiler usually figures out how to optimize a shift by 8 bits)

byte n = a>>4;    // Top 4 bits of 'a' are cache page
byte r = a&0x0f;  // Bottom 4 bits of 'a' are cache index
if (cachePage[n] == p) return cacheRam[n][r];


Later on when you're happy you have the best performance change it to something like:
Code: [Select]

// No bit-shifting please, we're an AVR
byte a = byte(eeaddress);  // Bottom 8 bits of address
byte p = eeaddress>>8;     // Page of RAM we need (the compiler usually figures out how to optimize a shift by 8 bits)
byte r = a&0x0f;
switch (a&0xf0) {
 case 0x00:  if (cachePage[0] == p) return cacheRam[0][r];
 case 0x10:  if (cachePage[1] == p) return cacheRam[1][r];
 ...
 case 0xf0: if (cachePage[15] == p) return cacheRam[15][r];
}


Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Oct 31, 2013, 10:55 am
Thanks for your tip's.

More optimization tonight.
I'm at work at the moment so can't try anything.

When I get this running smooth I'll use a 24LC512 for the full 64Kb.
Then the 6502 ROMs will fit there also.

And also implement I/O into the adress space.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Oct 31, 2013, 11:23 am

Your cache misses are very expensive (you have to use I2C!)  so more, smaller pages is probably good.


I was thinking that cachewrites only happens when the interpreter accesses variables.
In that case 128bytes is better?

If it has to cache 6502 machinecode, 16bytes is better?
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: fungus on Oct 31, 2013, 12:53 pm

I was thinking that cachewrites only happens when the interpreter accesses variables.


Misses on read are expensive too.


If it has to cache 6502 machinecode, 16bytes is better?


With the same amount of cache, smaller pieces is almost always better.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Oct 31, 2013, 01:25 pm
Yes, but on cachewrite there is a 5ms delay on each write.
To allow the EEPROM to store the data.

I'll try it with your 16x16byte cache.

The emulatorcode can also be optimized with inline assembler to make it even faster.

I want to run this on a standalone 328P chip with internal 8MHz osc.
No external Components other than the EEPROM.

So I really need to get to the 1sec loop test, and faster, to be able to take the cache misses.

Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: fungus on Oct 31, 2013, 02:09 pm

Yes, but on cachewrite there is a 5ms delay on each write.
To allow the EEPROM to store the data.


Yep.

More reason to make the blocks very small...

With delays like that it might be worth flagging the dirty bytes individually. You could have a block size of 16 and a 16-bit int with one bit for each dirty byte in the block. :)
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Oct 31, 2013, 02:44 pm


Yes, but on cachewrite there is a 5ms delay on each write.
To allow the EEPROM to store the data.


Yep.

More reason to make the blocks very small...

With delays like that it might be worth flagging the dirty bytes individually. You could have a block size of 16 and a 16-bit int with one bit for each dirty byte in the block. :)



No need for dirtybytes.
The delay is 5ms on a single byte and the same on a 16byte block.

It is per write operation.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Nov 01, 2013, 09:10 am
I rewrote the cachecode so that LEDPin13 flashes when it caches like a diskactivity LED.

With 16byte pages it flashes constantly when ever a basic program runs.
Loading a a buffer on a cachemiss takes 75uS and another 5ms if the exsisting one is Dirty.

With 128byte pages it flashes seldom but if i write a basicprogram that add strings it starts to flash.
Loading a buffer on a cachemiss takes 600uS and another 31ms if the existing one is Dirty.

Finding the right cachepage size is not so easy.

But now it uses blockread/write so there is only 6 read/write i2c operations on a flush/load.
Very fast, hardly no impact on the basic program running.

And the EEPROM lasts 115days of running 24/7 nonstop.

An i2c RAM of the same size would be better but I did not find any.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Nov 01, 2013, 09:20 am
I found the SID library and it fits in my current setup.
I'll add that with a memorymap like the C64 and try some real programs to see how it performs.

I have the ROM images for both VIC-20 and C64 and will perhaps try booting with those instead.

But I think the exec6502 function needs optimization.
The FOR NEXT loop indicates that the 6502 runs at about 300KHz.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: fungus on Nov 02, 2013, 09:22 am

With 16byte pages it flashes constantly when ever a basic program runs.


That will be different for every program and every cacheing scheme.

Maybe my idea for a hash function is bad - it can lead to pathological cases. Maybe the original 'list of if statements' causes less cache misses.


Finding the right cachepage size is not so easy.


:)
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Nov 02, 2013, 11:31 am
Well, I managed to boot both the VIC-20 and C64 roms on it :)
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Nov 02, 2013, 05:27 pm
I did some tests and it seems that a 6502 enviroment is the most happy with 128byte cachepages.

This is because it can keep a loop of code in a single page and there will not be any misses.
It would even work better with 256byte pages because of the 6502 adressingmodes but there is no room in the Arduino for that.

Both EhBasic and CBM-systems work the best with large pages.

So I will work on optimize the caching functions for speed.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: Glass on Nov 02, 2013, 09:45 pm
Very cool !
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Nov 02, 2013, 09:56 pm

Very cool !


I will post the full emulator code here but due to copyright I cant post the roms.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: miker00lz on Nov 07, 2013, 09:42 pm
This is awesome, janost. Really looking forward to seeing your results!  :)

EDIT: Ah, I see you started a thread!
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Dec 18, 2013, 07:32 pm
Yes, It runs on my VideoBlaster platform.
(Look at the last line)

An emulator on a chip :)
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Dec 20, 2013, 11:25 am
I started it with a keyboard and it runs perfect.

Only problem is the 6502 is running at about 33KHz.
FOR I=0 TO 100:NEXT takes 3 seconds.

It needs a faster 6502 emulator.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Dec 21, 2013, 01:05 am
Isn't this what you wanted mike?
Its your emulator running on my VideoBlaster platform.

It runs beautifully in color on a single chip AVR :)
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: fungus on Dec 21, 2013, 12:45 pm

It needs a faster 6502 emulator.


Even if it's twice as fast (doubtful) it still won't be very useful.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Dec 21, 2013, 01:04 pm


It needs a faster 6502 emulator.


Even if it's twice as fast (doubtful) it still won't be very useful.



No, you are right.

But if you have looked at his cpu.c code it is written for GHz platforms and not very efficient.

20times the speed is possible with a 6502 emulator written in ASM.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Dec 23, 2013, 09:18 pm
SInce its Christmas, well, here is the complete code to run EhBasic on the Bambino/1284P platform.

In NTSC color with just 2 resistors.

Merry Christmas to you all :)
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Dec 25, 2013, 02:02 am
I'll make a version with a TFT-display aswell.

That's for all of you not fond of TV's.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: mrburnette on Dec 25, 2013, 07:32 pm
Quote
here is the complete code to run EhBasic on the Bambino/1284P platform.


Yes, Virginia, there is a Santa Claus

Thanks to the code elves!

Ray
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Dec 27, 2013, 01:12 am

Quote
here is the complete code to run EhBasic on the Bambino/1284P platform.


Yes, Virginia, there is a Santa Claus

Thanks to the code elves!

Ray


:)
Title: Arduino 6502 emulator + Microchess
Post by: oscarv on Jun 05, 2014, 10:22 pm
Hi,

Thanks for this very nice project! I used it to emulate the classic Microchess from the KIM-I on the Arduino.

I've been fascinated by Microchess for a long time: 924 bytes of 6502 code for a chess playing program - and one of the best pieces of programming ever IMHO. Last month, I joked about porting it to the Arduino. But with your code, it took me a few hours. Cheers, and I hope my re-use of your code is OK!

Microchess makes the Arduino play a very decent game. For the few that are niche enough to care, the code is here: http://obsolescenceguaranteed.blogspot.ch/2014/06/6502-microchess-on-arduino.html (http://obsolescenceguaranteed.blogspot.ch/2014/06/6502-microchess-on-arduino.html).

Regards,

Oscar.
Title: KIM-1 emulator on Arduino Uno (KIM Uno?)
Post by: oscarv on Jun 26, 2014, 01:42 pm
Hi,

Just to let you know that after some more fun with the 6502 core, I made a KIM-1 emulator with it. It runs on a basic Arduino Uno, and can either be used through the serial port or with a small keyboard/LED display shield. It's a lot less useful then ehBasic, admittedly, but if it can be done it must be done  :)

A description and sources are here:
http://obsolescenceguaranteed.blogspot.ch/2014/06/kim-uno-kim-i-emulator-on-arduino-uno.html (http://obsolescenceguaranteed.blogspot.ch/2014/06/kim-uno-kim-i-emulator-on-arduino-uno.html)

Regards,

Oscar.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jun 26, 2014, 06:31 pm
Me like! :)

For a much simpler and cheaper display, buy a $3 TFT screen on eBay and use it to emulate the LED-display.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jun 29, 2014, 03:06 am
Get me the memorymap, Ray ,and some ROM dumps and I'll put together a CBM PET?
I love those 6502 machines.

This is fun stuff.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: mrburnette on Jun 29, 2014, 11:32 pm

Get me the memorymap, Ray ,and some ROM dumps and I'll put together a CBM PET?
I love those 6502 machines.

This is fun stuff.



The P.E.T. has some age on her, I hate to pull the ROMS, but will unless I can find the listings online.

I also have an Apple collection, Apple ][ Plus w/ Integer card & CPM card (2) machines, and all 6 of the first Macintosh b/w series units (yes, that is a 128 original next to the Fat Mac!).  Somewhere off to the right out of frame is an Osborne I CPM dual-floopy unit.

Back at one time, I had them all refurbished and they performed flawlessly, but these days I cannot find time for them.  Wish I could find 'em a good home.

Ray
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: retrolefty on Jun 29, 2014, 11:36 pm
What, no Kaypro. They were built like the proverbial brick shit house, weighed a ton.

http://oldcomputers.net/pics/kayproii.jpg
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: mrburnette on Jun 29, 2014, 11:47 pm

What, no Kaypro. They were built like the proverbial brick shit house, weighed a ton.

http://oldcomputers.net/pics/kayproii.jpg


Yep  I do remember and came very close to buying one, but got interested in IBM because I had access to one at work. 

update to Jan, here is the PET ROMs:

http://www.6502.org/users/andre/petindex/roms.html (http://www.6502.org/users/andre/petindex/roms.html)

See if you can get it to work with the 2.2" TFT SPI display... I have like 8 of them!  Also, I have a tube of Atmega 1284 with 16K... Sweet.

Ray
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jun 30, 2014, 10:23 am
I also have a box full of those 2.2" TFTs

I'll set it up on a breadboard :)
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: pito on Jun 30, 2014, 12:01 pm
Nice project! Congratulation!

I took the source from the first post and compiled for the 1284p mighty. I changed the RAM_SIZE to 10000 in cpu.c.
It compiles and I get
Code: [Select]

6502 EhBASIC [C]old/[W]arm ?

Memory size ?

in my terminal.
Sorry for my naive question - what needs to be done next? When I enter a number (or I just press enter) I see nothing.. :)

PS: I've changed to TeraTerm terminal and it seems it works:
Code: [Select]
6502 EhBASIC [C]old/[W]arm ?

Memory size ?

9231 Bytes free

Enhanced BASIC 2.22

Ready
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: pito on Jun 30, 2014, 05:14 pm
Who is complaining the emulator is slow??
This is calculated (and printed out @115k2) in 8.07 seconds:
Code: [Select]
6502 EhBASIC [C]old/[W]arm ?

Memory size ?

31231 Bytes free

Enhanced BASIC 2.22

Ready
5  REM TRIGONOMETRIC FUNCTIONS TEST 1..89 DEGREE
10 PH = 3.141592653/2.0
15 REM DEFINE ARCSIN AND ARCCOS
20 DEF FN ASN(X) = ATN(X/SQR(1.0-X*X))
30 DEF FN ACS(X) = PH-ATN(X/SQR(1.0-X*X))
35 REM TR, TD - TO RAD, TO DEG
40 TR = 3.141592653 / 180.0
50 TD = 180.0 / 3.141592653
55 REM INPUT IN DEGREE (1..89)
60 FOR D = 1 TO 89
70 R = D * TR
80 S = FN ASN(FN ACS(ATN(TAN(COS(SIN(R))))))
90 T = S * TD
100 PRINT T; " "; D; " "; T-D; " "; FRE(1)
110 NEXT D
120 END

RUN
.999995  1 -4.52995E-06  30753
2.00008  2  8.39233E-05  30753
2.99996  3 -4.50611E-05  30753
3.99999  4 -1.26362E-05  30753
4.99997  5 -2.95639E-05  30753
5.99996  6 -4.52995E-05  30753
6.99998  7 -2.43187E-05  30753
7.99999  8 -9.53674E-06  30753
8.99998  9 -1.90735E-05  30753
9.99994  10 -5.62668E-05  30753
..
87.0002  87  1.75476E-04  30753
87.9999  88 -6.86646E-05  30753
89.0001  89  5.34058E-05  30753


Cool!  ;)
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jun 30, 2014, 05:55 pm
It was slow when used together with video rendering.
Most of the ATmega cycles goes to updating the video.

Its not slow when used by it self on the ATmega.

The 6502 emulator runs at about 600KHz on a 16MHz ATmega.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: mrburnette on Jun 30, 2014, 09:52 pm

...
It compiles and I get
Code: [Select]

6502 EhBASIC [C]old/[W]arm ?

Memory size ?

in my terminal.
Sorry for my naive question - what needs to be done next? When I enter a number (or I just press enter) I see nothing.. :)

PS: I've changed to TeraTerm terminal and it seems it works:
Code: [Select]
6502 EhBASIC [C]old/[W]arm ?
Memory size ?
9231 Bytes free
Enhanced BASIC 2.22
Ready



When you press Enter in response to "Memory size?" All free (S)RAM is used... In the old days, you would type in a smaller number if you needed to load a second "piece" into RAM, maybe an assembler program.
The Cold/Warm is to permit you to have a "C" complete boot with nothing in the program space or a "W"arm boot to preserve the already loaded program. 
These things are holdovers from the original ROMs.

Ray
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jun 30, 2014, 10:18 pm
I did a quick mock up of my VIC-20 emu on the DUE board because it has a 2.2" TFT.
This was only a test to know that the video and cpu emulation works.

Now over to the PET ROM's

Those are a real mess.
Don't even know which ones to use.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: pito on Jul 01, 2014, 12:55 pm
The enhanced basic 2.22 has only 6digits fp - that is a..  :smiley-roll:
I've just tried the "**** COMMODORE 64 BASIC V2 **** " (the "dos" version) and it has got 9 digits precision, what is _much_ better stuff..
Is there a chance to load that rom from somewhere??
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 01, 2014, 01:58 pm
Those ROMs are all over the place.

The thing with CBM ROMs is that they are so overpatched that they need to be run in sets or won't work.
That is both the BASIC and KERNAL ROMs are needed.


Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: mrburnette on Jul 01, 2014, 02:19 pm

I did a quick mock up of my VIC-20 emu on the DUE board because it has a 2.2" TFT.
This was only a test to know that the video and cpu emulation works.

Now over to the PET ROM's

Those are a real mess.
Don't even know which ones to use.



That is pretty cool to see the character-based VIC-20 screen running on a TFT graphic display and on ARM Cortex-M3.  Pretty Wow!

I'm thinking that you should be able to get the Cypress PSoC 4200 to perform the video almost exclusively without the CPU intervening which would put a 4K VIC-20 into a Minty Can size!!!  Way cool, Jan.

Also, as a testament to the 6502 emulator code, it seems to run fine on ARM Cortex.  Great job there,
miker00lz.


Ray
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 01, 2014, 02:59 pm
The thing with the SPI-TFT's is that they are very data hungry.
Even an 8x8 character takes a massiv amount of data over the SPI-bus.

But it's event driven with write through.
The display only updates if you write to videoram and only that character.

I tried one set of PET2001 ROMS but it did not boot.
Have to try some other of those found on the linked site.

But you're right about it being cool.
Wish I could find a keyboard that matches the size :)

Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 01, 2014, 05:39 pm
It does boot the C64 as well.
With as little as 2K RAM but that results in 0 basic bytes free.

But not the PET yet.

Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: pito on Jul 01, 2014, 06:10 pm
So in order to run c64 v2 I need Basic and kernal roms, and I need to convert  them into the "source format". I guess I need to change the addresses in the cpu.c as well (I am using the original code without the display stuff..). Are there any other changes needed?
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 01, 2014, 06:26 pm

So in order to run c64 v2 I need Basic and kernal roms, and I need to convert  them into the "source format". I guess I need to change the addresses in the cpu.c as well (I am using the original code without the display stuff..). Are there any other changes needed?


This change is needed in cpu.c to boot the C64:

Code: [Select]

uint8_t read6502(uint16_t address) {

  if ((address >= 0xA000)&&(address < 0xC000)) return BIOS[(address - 0xA000)];
  if (address >= 0xE000) return BIOS[(address - 0xC000)];
  if ((address >= 0xD000)&&(address < 0xD800)) return(0);
  if (address < RAM_SIZE) return(RAM[address]); 
}

void write6502(uint16_t address, uint8_t value) {
  if (address < RAM_SIZE) {
   RAM[address] = value;
   if ((address>1023)&&(address<2024)) {
     writeVIDEO(address-1024,value);
   }
   
  }

}


For the BIOS I used this file: 64c.251913-01.bin that has both the BASIC and KERNAL ROM's in a single file.

The writeVIDEO puts a char on the TFT if it gets written into screenmemory at $400.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: pito on Jul 01, 2014, 06:51 pm
Do we still need the
Code: [Select]
prog_uchar BIOStop[256] PROGMEM = {....
stuff there?
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 01, 2014, 06:58 pm

Do we still need the
Code: [Select]
prog_uchar BIOStop[256] PROGMEM = {....
stuff there?


You can remove the BIOStop array.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: pito on Jul 01, 2014, 07:06 pm
Ok, I converted the rom and changed to (I am not using the video version yet):
Code: [Select]
uint8_t read6502(uint16_t address) {
 uint8_t tempval = 0;

 if (address == 0xF004) { //EhBASIC simulated ASIC input
   tempval = getkey();
   clearkey();
   return(tempval);
 }

 if ((address >= 0xA000)&&(address < 0xC000)) return BIOS[(address - 0xA000)];
 if (address >= 0xE000) return BIOS[(address - 0xC000)];
 if ((address >= 0xD000)&&(address < 0xD800)) return(0);
 if (address < RAM_SIZE) return(RAM[address]);  

}

void write6502(uint16_t address, uint8_t value) {
 if (address < RAM_SIZE) RAM[address] = value;
 if (address == 0xF001) { //EhBASIC simulated ASIC output
   serout(value);
 }
}


Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 01, 2014, 07:12 pm

Ok, I converted the rom and changed to (I am not using the video version yet):
Code: [Select]
uint8_t read6502(uint16_t address) {
 uint8_t tempval = 0;

 if (address == 0xF004) { //EhBASIC simulated ASIC input
   tempval = getkey();
   clearkey();
   return(tempval);
 }

 if ((address >= 0xA000)&&(address < 0xC000)) return BIOS[(address - 0xA000)];
 if (address >= 0xE000) return BIOS[(address - 0xC000)];
 if ((address >= 0xD000)&&(address < 0xD800)) return(0);
 if (address < RAM_SIZE) return(RAM[address]);  

}

void write6502(uint16_t address, uint8_t value) {
 if (address < RAM_SIZE) RAM[address] = value;
 if (address == 0xF001) { //EhBASIC simulated ASIC output
   serout(value);
 }
}





Yes, that is right.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: mrburnette on Jul 01, 2014, 07:32 pm

...
But you're right about it being cool.
Wish I could find a keyboard that matches the size :)


I'm thinking an old cellphone slide keyboard, or maybe a Blackberry...

One of the "for parts" eBay things...
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: pito on Jul 01, 2014, 07:42 pm
No luck yet. I've tried with 65536 ramsize, and 30000 ramsize. I have got 128kB ram so no problem.
The ehBasic boots and works fine with 49k ramsize.
I did only the changes above..
Maybe the i/o addresses differ..
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 01, 2014, 07:43 pm


...
But you're right about it being cool.
Wish I could find a keyboard that matches the size :)


I'm thinking an old cellphone slide keyboard, or maybe a Blackberry...

One of the "for parts" eBay things...


I have the top one in my museum :)
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 01, 2014, 07:47 pm

No luck yet. I've tried with 65536 ramsize, and 30000 ramsize. I have got 128kB ram so no problem.
The ehBasic boots and works fine with 49k ramsize.
I did only the changes above..
Maybe the i/o addresses differ..


But you will basically end up with a 1000byte screen that you cant see at RAM[1024]
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: pito on Jul 01, 2014, 07:49 pm
Can you explain, plz?
Does it mean the C64 has no i/o similar to enhBasic but I have to use the output from videoram?
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 01, 2014, 07:53 pm

Can you explain, plz?


The PET/VIC-20/C64/C128 is a fullscreen editor and not a terminal OS.

All output gets written to videomemory.
And all input is read from videomemory on the line your are standing on with the cursor when you press enter.

It is possible to redirect the output because that is written through a vector at $FFD2

Not sure about the input but it may be possible,

It's been long since I hacked any of those but by modifying the KERNAL you may get it to work like EHBASIC.

Not sure how good it will work though.

Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: pito on Jul 01, 2014, 07:55 pm
Ok, thanks, so I need to think about a new i/o code..
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: pito on Jul 01, 2014, 07:58 pm
I was the Atari520 user at that time, so no idea how the C64 works :)
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 01, 2014, 08:07 pm
It was Amiga for me :)

I used this when I first began with the VIC-20 EMU
It write out the videomemory to serial so you can at least see if it boots.

Code: [Select]

  for (byte y=0;y<23;y++) {
    for (byte x=0;x<22;x++) {
      for (byte z=0;z<100;z++) {
      exec6502();
      }
     petscii=videomem[eeaddress++];
     if (petscii<32) {
      petscii=petscii+64;
     }
     USART_write(petscii);
    }
    USART_write(10);
    USART_write(13);
  }
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: pito on Jul 01, 2014, 08:49 pm
Ok, it seems it boots:
Code: [Select]
void loop () {
//  exec6502(100); //if timing is enabled, this value is in 6502 clock ticks. otherwise, simply instruction count.
//  if (Serial.available()) {
//    curkey = Serial.read() & 0x7F;
//  }
//}
 int address = 0;
 for (int y=0;y<25;y++) {
   for (int x=0;x<40;x++) {
     exec6502(100);
    unsigned char petscii=VRAM[address++];
    if (petscii<32) {
     petscii=petscii+64;
    }
    Serial.write(petscii);
   }
   Serial.println();
 }
}
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 01, 2014, 09:30 pm
Ok, that's great.

Now to the input,
Since interrupts are not running, to get characters in as they was typed on the non existent keyboard, you need to "poke" those into the keyboard buffer.

This code:

Code: [Select]

void loop() {
 exec6502();
 RAM[198]=1;
 RAM[631]=65;
}


Results in the attached picture :)

So basically you remove the 0xF004 and getval from the read6502 function.

The you add an "If serial.available" in the loop function and poke the available character into the keyboard buffer.

Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: pito on Jul 01, 2014, 10:52 pm
Code: [Select]
void loop () {

 int v_address = 0;
 Serial.write(27);   // Move to Home
 Serial.write("[H");
 
 for (int y=0;y<25;y++) {
   for (int x=0;x<40;x++) {
     exec6502(100);
     if (Serial.available()) {
    curkey = Serial.read() & 0x7F;
  RAM[198]=1;
  RAM[631]=curkey;
}
    unsigned char petscii=VRAM[v_address++];
    if (petscii<32) petscii=petscii+64;
    Serial.write(petscii);
   }
   Serial.write(13);
   Serial.write(10);
 }
}


Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 01, 2014, 10:54 pm

Code: [Select]
void loop () {

 int v_address = 0;
 Serial.write(27);   // Move to Home
 Serial.write("[H");
 
 for (int y=0;y<25;y++) {
   for (int x=0;x<40;x++) {
     exec6502(100);
     if (Serial.available()) {
    curkey = Serial.read() & 0x7F;
  RAM[198]=1;
  RAM[631]=curkey;
}
    unsigned char petscii=VRAM[v_address++];
    if (petscii<32) petscii=petscii+64;
    Serial.write(petscii);
   }
   Serial.write(13);
   Serial.write(10);
 }
}





Cool
You're able to run it. :)
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: pito on Jul 01, 2014, 11:07 pm
It works, but I would prefer a standard i/o.. :)
PS: you may see a snip of the screen does not work properly as it continuously rewrites it (see the line with "4")..
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 01, 2014, 11:41 pm

It works, but I would prefer a standard i/o.. :)
PS: you may see a snip of the screen does not work properly as it continuously rewrites it (see the line with "4")..



Could be because the screen scrolled up?

It's not a good way to continuously rewrite the whole screen.
Causes a lot of data to be transmitted.

And CBM's don't use ascii in their videomemory, it's petscii.
My check is a quick and dirty hack to convert it to ascii.
It doesn't cover all codes.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: pito on Jul 01, 2014, 11:55 pm

It's not a good way to continuously rewrite the whole screen.
Causes a lot of data to be transmitted.
..

Yea, sending deltas only will do better.. But nice to see it works. And faster than the original.. :)
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 02, 2014, 10:25 am
I'll build it on a 1284 and use the C64 ROMs. They are more stable than PET ROMs but exactly the same V2 Basic.
And they can run down to 2K RAM without problems.

Also it supports both color and mono text/graphics.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 03, 2014, 07:37 pm
The C64 textmode now support full color on the 2.2" TFT.
Border, background and character colors.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 03, 2014, 08:08 pm
I use soft-SPI on the due because of hysterical reasons so the screen can be very much faster.

But it works :)

http://www.youtube.com/watch?v=NvQe6eg5_hQ

Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 03, 2014, 08:12 pm
Here is the memorymap if you want to emulate a C64.
Or POKE something into your emulated C64.

Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: pito on Jul 03, 2014, 08:20 pm
The C64 has an 6510 inside. So we need to enhance the emulator somehow :)
Try to run this on the DUE, how long does it take..
Code: [Select]

5  REM TRIGONOMETRIC FUNCTIONS TEST 0..89 DEGREE
10 PH = 3.141592653/2.0
15 REM DEFINE ARCSIN AND ARCCOS
20 DEF FN ASN(X) = ATN(X/SQR(1.0-X*X))
30 DEF FN ACS(X) = PH-ATN(X/SQR(1.0-X*X))
35 REM TR, TD - TO RAD, TO DEG
40 TR = 3.141592653 / 180.0
50 TD = 180.0 / 3.141592653
55 REM INPUT IN DEGREE (1..89)
60 FOR D = 0 TO 89
70 T = SIN(TR*D)
72 T = COS(TR*T)
74 T = TAN(TR*T)
76 T = TD*ATN(T)
80 T = TD*FN ACS(T)
90 T = TD*FN ASN(T)
100 PRINT D; "="; T; " DELTA="; T-D
110 NEXT D
120 END
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 03, 2014, 08:24 pm
Not much difference to a 6502 besides the IO port at $0000 and $0001.

That IO port control memory mapping of the 64K RAM and ROMs
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 03, 2014, 08:28 pm

The C64 has an 6510 inside. So we need to enhance the emulator somehow :)
Try to run this on the DUE, how long does it take..
Code: [Select]

5  REM TRIGONOMETRIC FUNCTIONS TEST 0..89 DEGREE
10 PH = 3.141592653/2.0
15 REM DEFINE ARCSIN AND ARCCOS
20 DEF FN ASN(X) = ATN(X/SQR(1.0-X*X))
30 DEF FN ACS(X) = PH-ATN(X/SQR(1.0-X*X))
35 REM TR, TD - TO RAD, TO DEG
40 TR = 3.141592653 / 180.0
50 TD = 180.0 / 3.141592653
55 REM INPUT IN DEGREE (1..89)
60 FOR D = 0 TO 89
70 T = SIN(TR*D)
72 T = COS(TR*T)
74 T = TAN(TR*T)
76 T = TD*ATN(T)
80 T = TD*FN ACS(T)
90 T = TD*FN ASN(T)
100 PRINT D; "="; T; " DELTA="; T-D
110 NEXT D
120 END



I'll try.

The general way of measuring speed on a CBM is this.

FOR I=0 TO 1000:NEXT

That takes exactly 1 second on a 1MHZ 6502 in CBM Basic.

Make one I=1 to 10000 and clock it.

That will tell you the factor against the original.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 03, 2014, 08:45 pm
Don't look blindly on my 84MHz DUE.

This runs equally well on any system that can support the memory.
I haven't used any of the DUE's fancies.

I do like to write the library's inline because most of the Arduino library's are such memory hog's.
Better to strip them down and include it in the sketch.
Ladyada isn't the best coder :)

I have attached my cpu.c and Arduino code to boot the C64 emulator in color.
I know it's not ok to put ROM's or BIOS code out in the open but in this case it might be ok?

The reason I use soft-SPI on the DUE is because it needs an exclusice connection to the ISP connector for SPI.
And that you can just stick a 1.8" or 2.2" TFT into one of it's headers if you run it soft. :)
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: pito on Jul 03, 2014, 09:26 pm
This takes 4.5sec on my system:
Code: [Select]
10 FOR I = 1 TO 10000
20 NEXT I
READY.
RUN
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 03, 2014, 09:32 pm

This takes 4.5sec on my system:
Code: [Select]
10 FOR I = 1 TO 10000
20 NEXT I
READY.
RUN



That makes 10000/4500*1MHz or a 2.2MHz 6502 :)
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 03, 2014, 09:50 pm
I don't want to the same on the DUE.

It will show a 10MHz 6502.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 03, 2014, 09:57 pm

This takes 4.5sec on my system:
Code: [Select]
10 FOR I = 1 TO 10000
20 NEXT I
READY.
RUN



Not a new line.

It needs to be written in one line.

10 FOR I=1 to 10000:NEXT

But I guess it's marginal.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: pito on Jul 03, 2014, 09:59 pm


This takes 4.5sec on my system:
Code: [Select]
10 FOR I = 1 TO 10000
20 NEXT I
READY.
RUN



That makes 10000/4500*1MHz or a 2.2MHz 6502 :)

But that is with exec6502(100); inside the "video and input loop" sending 1000bytes of video @115k2 continuously. So it must be much higher speed than the 2.2MHz. How to measure it then??.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 03, 2014, 10:08 pm



This takes 4.5sec on my system:
Code: [Select]
10 FOR I = 1 TO 10000
20 NEXT I
READY.
RUN



That makes 10000/4500*1MHz or a 2.2MHz 6502 :)

But that is with exec6502(100); inside the "video and input loop" sending 1000bytes of video @115k2 continuously. So it must be much higher speed than the 2.2MHz. How to measure it then??.


Yes, it is.

You may have noticed that my exec6502 doesn't have any number, just empty brackets.
The emulator is written for PC and several hundred MHz of CPU and has to be slowed down.
I have removed that to make it run as fast as it can or exec6502(1).

You can't really measure with that video response.

Make it delta and it will perform better.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: pito on Jul 03, 2014, 10:14 pm
I think the 100 in the 6502exec is not about "delays", but it says "do 100 6502 instructions before returning from 6502exec".
With 1 it will do a single 6502 instruction, then it returns. So the higher number there the more time it spends in 6502 emulation..
Also, I think there must be a function which offers a char at a specific ram address to be printed into video ram..
Finding that point I can get rid of the video mess and hook the stuff similarly as the enhBasic does..
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 03, 2014, 10:20 pm

Also, I think there must be a function which offer a char at a specific ram address to be printed into video ram..


No and yes.

As I said before the OS prints through a function at $FFD2.
There is no way the OS knows that you are emulating it?

It puts chars in the videomem believing that you can see them.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: pito on Jul 03, 2014, 10:29 pm
I ran the enhBasic with above trigonometric test under arduino @16MHz (no video mess) and it took 84secs/
Then I ran it under the second system (also the enhBasic,no video mess) and it did it in 8.1sec. So my understanding is the second system's emulation speed  is ~10x faster than the arduino @16MHz - based on your info it means 6MHz.
I doubt DUE can do that such fast..
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 03, 2014, 10:38 pm

I ran the enhBasic with above trigonometric test under arduino @16MHz (no video mess) and it took 84secs/
Then I ran it under the second system (also the enhBasic,no video mess) and it did it in 8.1sec. So my understanding is the second system is ~10x faster than the arduino @16MHz.


I don't have the full history or background of EHBasic.

But Microsoft's basic, running on the CBM and Atari 8-bit, is way faster than EHBasic.
With higher precision.

Trust me. Those Basic versions are the fastest in history on 8-bit platforms.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: pito on Jul 03, 2014, 10:41 pm
It does not matter because both benchmarks were done under enhBasic.  1284p mighty 84secs, second system 8.1secs.
There is serial i/o in enhBasic, so the video does not interfere into the measurement..
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 03, 2014, 10:46 pm

It does not matter because both benchmarks were done under enhBasic.  1284p mighty 84secs, second system 8.1secs.
There is serial i/o in enhBasic, so the video does not interfere into the measurement..


Then measure it the same way on the C64?

No video to interfere?
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 03, 2014, 10:50 pm
I normally work on the NXP LPC platform.
But there aren't anything even close to the DUE on that platform.

So the DUE makes a great breadboard platform.
Even the Thumb ARM-assembly runs straight on the NXP ARM Cortex M0+
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: pito on Jul 03, 2014, 10:50 pm
I cannot compare that under C64 even when run on both systems, as the video (not related to the emulation) messes heavily the results up..
The enhBasic is good for benchmarking (of the emulation speed) on various systems as the serial i/o there does not interfere too much.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 03, 2014, 10:56 pm

I cannot compare that under C64 even when run on both systems, as the video (not related to the emulation) messes heavily the results up..


No, it doesn't?
Don't you just time it from start to ready?

You cant time things that are printing to the screen constantly?

Even the PC stopped decades ago timing things like that?
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: pito on Jul 03, 2014, 11:03 pm
Unless some tricks applied I cannot do it easily with C64 as when I stop the video I cannot see the test has finished :)
With enhBasic you may print start/stop and measure the elapsed time. No video buffer needs to be read.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 03, 2014, 11:07 pm
Don't run my loop blind.

Make the "CPU" run more and the video less?
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: pito on Jul 03, 2014, 11:19 pm
When you need to compare architectures/compilers running the cpu.c emulator then the best option is to use the enhBasic as it communicates via Serial only. So the error related to the communication with the emulated system (with Basic) is small.
The C64 rom communicates via video stream, where the stream takes a lot of time in comparison to the time spent in the emulator. That issue is the same for my Terminal printing, as well as for your tft video emulation.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 03, 2014, 11:26 pm

When you need to compare architectures/compilers running the cpu.c emulator then the best option is to use the enhBasic as it communicates via Serial only. So the error related to the communication with the emulated system (with Basic) is small.
The C64 rom communicates via video stream, where the stream takes a lot of time in comparison to the time spent in the emulator. That issue is the same for my Terminal printing, as well as for your tft video emulation.



The EHBasic is basically the lower 8K of the MS C64 ROM set.

I don't know what you want to measure?
But I do know that its faster.

Using it is what differs.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: pito on Jul 03, 2014, 11:32 pm

I don't know what you want to measure?

People might be interested to see how the cpu.c emulator performs with avr, CM0, CMO+, CM3, CM4, pic32MX, pic32MZ, etc.. compiled with X, Y, Z compilers..  So when they want start to measure the enhBasic running a benchmark is the good option..
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 03, 2014, 11:40 pm


I don't know what you want to measure?

People might be interested to see how the cpu.c emulator performs with avr, CM0, CMO+, CM3, CM4, pic32MX, pic32MZ, etc.. compiled with X, Y, Z compilers..


That's why I put up the code for a C64 in textmode. :)

Compile it on anything.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 03, 2014, 11:44 pm
And your program completes in 1.5 seconds on the emulator running on the DUE :)
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 04, 2014, 12:09 am
By doing color on the CBM ROM's I now have 256 16-bit writes on every character write.

That sucks. Big time?
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: pito on Jul 04, 2014, 12:08 pm

And your program completes in 1.5 seconds on the emulator running on the DUE :)

Hard to believe :)
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 05, 2014, 10:33 am


And your program completes in 1.5 seconds on the emulator running on the DUE :)

Hard to believe :)


The videoemulation on the TFT only slows down the 6502 emulator when it writes something in videomem or colormem.

Otherwise is has no impact on the emulation and it runs at full speed.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: pito on Jul 05, 2014, 10:54 am
I ran it on pic32mx Fubarino o'clocked 120MHz (compiled with chipkit's compiler, USB serial com), and the below trigo test took about 10secs (C64 V2 ROM). I only read the first half of the bottom line to eliminate video. So it seems there is an issue somewhere (provided mips is ~20% faster than arm and my clock is 30% more yours)  :) For example, if you do not switch CapsLock on, it returns in 1 sec as it does not run the code (the code is empty).. :) Unless the mips compiler is a crap I would estimate 19.9 secs for your code in the DUE..

PS: The test for reference:
Code: [Select]
5  REM PITO'S TRIGONOMETRIC FUNCTIONS TEST 0..89 DEGREE
6  REM FOR C64 V2 BASIC ROMS
7  REM BASED ON WELLKNOWN "9 DEGREE" CALC FORENSIC TEST
8  REM ASIN(ACOS(ATAN(TAN(COS(SIN(9.0)))))) = 9.0 IN DEGREE
10 PH = 3.1415926536/2.0
15 REM DEFINE ARCSIN AND ARCCOS
20 DEF FN ASN(X) = ATN(X/SQR(1.0-X*X))
30 DEF FN ACS(X) = PH-ATN(X/SQR(1.0-X*X))
35 REM TR, TD - TO RAD, TO DEG
40 TR = 3.1415926536 / 180.0
50 TD = 180.0 / 3.1415926536
55 REM INPUT IN DEGREE (0..89)
60 FOR D = 0 TO 89
70 T = SIN(TR*D)
72 T = COS(TR*T)
74 T = TAN(TR*T)
76 T = TD*ATN(T)
80 T = TD*FN ACS(T)
90 T = TD*FN ASN(T)
100 PRINT D; "="; T; " DELTA="; T-D
110 NEXT D
120 END


Code: [Select]
READY.
RUN
0 = .0708403071  DELTA= .0708403071
1 = 1.00188405  DELTA= 1.88405439E-03
2 = 2.00156704  DELTA= 1.56704057E-03
3 = 3.00102166  DELTA= 1.02166273E-03
4 = 4.00055188  DELTA= 5.51875681E-04
5 = 5.00046232  DELTA= 4.62317839E-04
6 = 5.99979405  DELTA=-2.05950812E-04
7 = 6.99975351  DELTA=-2.46489421E-04
8 = 8.00046608  DELTA= 4.6607852E-04
9 = 9.00044075  DELTA= 4.40750271E-04
10 = 10.0001669  DELTA= 1.66922808E-04
..
84 = 83.9999204  DELTA=-7.96020031E-05
85 = 85.0002601  DELTA= 2.60084868E-04
86 = 86.0004052  DELTA= 4.05162573E-04
87 = 87.0003106  DELTA= 3.10570002E-04
88 = 88.0004901  DELTA= 4.90099192E-04
89 = 89.0023091  DELTA= 2.30902433E-03

READY.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: pito on Jul 06, 2014, 02:21 pm
This is the version with transferring deltas only from C64 video RAM to the VT100 terminal (25 rows x 40 columns).
VRAM is a temporary video memory holding "old" characters. We compare the new RAM[] to VRAM and if a difference we print the new character at the VT100 [row,col] position.

Mind you have to have CapsLock on on your keyboard..

Works nice with VT100 in TeraTerm.

Code: [Select]
..
uint8_t VRAM[1000];

void VTposition(uint8_t row, uint8_t col) {
Serial.write(27);
Serial.write('[');
Serial.print(row + 1);
Serial.write(';');
Serial.print(col + 1);
Serial.write('H');
}

void setup () {
Serial.begin (115200);
Serial.println ("Booting the ROM..");
reset6502();
delay(1000);
for (int i=0; i<1000; i++){
VRAM[i] = RAM[i+1024];
}
}

void loop () {
int v_address = 0;
for (uint8_t row=0; row<25; row++) {
for (uint8_t col=0; col<40; col++) {
if (Serial.available()) {
curkey = Serial.read() & 0x7F;
RAM[198] = 1;
RAM[631] = curkey;
}

exec6502(100);

uint8_t petscii = RAM[v_address + 1024];

if (VRAM[v_address] != petscii) {
VRAM[v_address] = petscii;
VTposition(row, col);
if (petscii<32) petscii = petscii + 64;
Serial.write(petscii);
}
v_address++;
}
}
}
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 09, 2014, 01:06 pm
Looks like it got featured on Hackaday before it was finished.

http://hackaday.com/2014/07/06/c64-emulator-for-the-arduino-due
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 10, 2014, 12:52 pm
Running on the ATmega1284P with internal 8MHz oscillator.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 10, 2014, 01:25 pm
Knocking up the OSCCAL to 0xFF made it run at 16MHz.
And using hardware SPI made the screen faster than the DUE version.

Then there is only 16K SRAM so the emulation gets only about 12K free.

But the component count is just 2.
The ATmega and the display :)

What is really great about this config is the fact that there is still 28 IO-pins free.
It could behave like a real 6502 to the outside world?

I mean, give it a cartridge port?

With a 100K leftover in flash space it can run assembler, forth, pascal or any of the things available in cartridges?

And the display has an SD-card slot?
1541 anyone?


Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 10, 2014, 11:55 pm
This even runs on a Uno or ATmega328.

But there isn't any memory left for the emulated machine.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 11, 2014, 02:59 am
It cant be a real C64 because lack of SRAM.
But it still behaves as a C64.

And it can include the SID library.

That means that tracker software runs with the SID sound. :)
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: polymorph on Jul 11, 2014, 03:50 am
Hey, the Teensy 3.1 has 64k of RAM! What about that? And runs at 72MHz, overclockable to 96MHz.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 11, 2014, 04:09 am

Hey, the Teensy 3.1 has 64k of RAM! What about that? And runs at 72MHz, overclockable to 96MHz.


I ran it on the Due.
That has 96K ram and 84MHz.

On the ATmega it performs as a 2.2MHz 6502.

On an ARM at 84MHz it performs as a 10MHz 6502.

It will work on anything that has more than 2K RAM

Try it?
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: polymorph on Jul 11, 2014, 07:48 am
Nice! I don't have a Teensy 3.1, I do have a 3.0. I'll have to go back through this thread to see what I need to put together.
Title: Re: Arduino 6502 emulator + BASIC interpreter + ILI9341 GLCD + Atmega1284P
Post by: mrburnette on Jul 12, 2014, 06:24 pm
I got this breadboarded yesterday and made some current measurements.
Disclaimer: I am knowingly running the 1284P overclocked at 16MHz and 3.09V from 2 AA batteries.  The current (a 20 Ohm resistor limiting the GLCD LEDs) was measured with my Fluke to be 34mA max during screen boot, and about 32mA running.  The Bobduino board is being used with the 16MHz but without a voltage regulator onboard... battery supply via the Vin pin.

Now, to address the serial ASCII entry.  I'm thinking a low-powered Bluetooth module and my old Logitech diNovo mini-keyboard.

I simply love the emulator running on the TFT display.  Way cool, Jan.


Ray

Edit... I added this code:
Code: [Select]

#include <Streaming.h>
// ...
char curkey = 0 ;
// ...
Serial.begin(19200) ;
Serial << "AVR C64 emulator coming online at 19.2K BAUD \n \r \n \r" ;
// pito snippet
  if (Serial.available()) {
    curkey = Serial.read() & 0x7F ;
    Serial << curkey ;
    RAM[198]=1;
    RAM[631]=curkey;
  }


and changed the background TFT color for the picture.  I used TeraTerm as the keyboard.  Notice the error with the lowercase "i" in "Pi" on the TFT output.  One must completely wait after pressing Return/Enter for the TFT to stop scrolling before beginning to type again.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 12, 2014, 09:35 pm
Way cool Ray :)

I got 100mA current with the backlight connected directly to 3.3v
Need to put in a resistor.
Title: Re: Arduino 6502 emulator + BASIC interpreter + ILI9341 GLCD + Atmega1284P
Post by: janost on Jul 12, 2014, 09:43 pm

One must completely wait after pressing Return/Enter for the TFT to stop scrolling before beginning to type again.



That is because you are saying that you only have one character in the keyboard buffer for every keypress.

The buffer is 10 bytes long so to make keyboard entry smooth one actually has to add keys into the buffer and increment available number.
Title: Re: Arduino 6502 emulator + BASIC interpreter + ILI9341 GLCD + Atmega1284P
Post by: janost on Jul 12, 2014, 10:00 pm

Notice the error with the lowercase "i" in "Pi" on the TFT output.


That's not a bug.

CBMs have 2 character sets.

The first with capital letters and graphics only.

Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 13, 2014, 12:57 am
This is really something all CBM lovers (fanatics) should play with.

It is by no way a real C64 but it is a real CBM PET/Vic20/C64 machine.

And its fun.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 15, 2014, 10:05 pm
To come, The SCI-64 :)
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 17, 2014, 12:59 am
I broke a leg on the 1284 while moving it to a perfboard.

Damn me! I shouldn't be doing these mistakes after so many years?

Well, it will still run but the program will be a bit awkward?

Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 17, 2014, 01:50 am
It's going to have a real calculator mode.
That is a tribute to CBM and Jack Tramiel.

And to my dear friend Ray who is writing that part.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: mrburnette on Jul 17, 2014, 02:44 am

I broke a leg on the 1284 while moving it to a perfboard.

Damn me! I shouldn't be doing these mistakes after so many years?

Well, it will still run but the program will be a bit awkward?




I have used an Xacto knife before to cut (scrape) the encasement enough to solder a wire to a broken leg.  After ensuring a connection, coat with a drop of fast set epoxy.

Ray

Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: janost on Jul 17, 2014, 04:06 am

I have used an Xacto knife before to cut (scrape) the encasement enough to solder a wire to a broken leg.  After ensuring a connection, coat with a drop of fast set epoxy.

Ray




I know Ray, been there before. :)

That's why I said I shouldn't be doing things like that?
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: polymorph on Jul 17, 2014, 07:49 am
I'm not the only one! Back when any IC was quite expensive, especially when my only income was mowing lawns, I had to repair IC legs a few times.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: petersieg on Jan 16, 2015, 08:50 pm
Hi. Can somone please post the complete code (if without the roms is will still be ok) of the serial version (from pito)?

thx, Peter
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: pito on Feb 26, 2015, 09:53 pm
Enclosed pls find the serial version.

It works with a terminal, ie. Teraterm, 40x25 char. It is an extremely simple emulation of a video output and keyboard input, with no graphics, etc.

Worked here with Fubarino SD (pic32MX) and the newest Majenko's SDZL board (pic32MZ).

It runs the trigonometric demo in 12secs with pic32MZ @200MHz (or 5secs at 921k6 serial).

It may work with atmega1284p (yes it worked) or DUE (etc.), but you have to adjust the ram sizes in both files.

Caps Lock must be ON on your keyboard when writing Basic programs in the emulator.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: petersieg on Mar 03, 2015, 07:28 pm
@pito: thx. I can confirm, it works also with arduino due.

Code: [Select]
arduino_6502_pic_C64_OK.cpp.o: warning: definition of `RAM' overriding common
cpu1.c.o: warning: common is here

Der Sketch verwendet 33.628 Bytes (6%) des Programmspeicherplatzes. Das Maximum sind 524.288 Bytes.
Erase flash
Write 34764 bytes to flash


runs just fine.
On Mega, even with just 4096 bytes ram, 21k on variable space is missing.

Peter
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: patolin on May 03, 2015, 10:44 pm
Tested on a Stellaris Launchpad using Energia, works great. Just changed the memory size to 16kb for testing, Because the Stellaris has only 32KB of RAM. This blew my mind (now I will no sleep tonight thinking in this hehehe). Anyways, Does anybody think about an Atari 2600 emu based on an Arduino? May be using another chip for audio/video
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: bigbill on Jun 14, 2015, 01:45 am
I have Leonardo set up to write to the keyboard of my laptop, but it will stop working when i have nes emulator running.

Any idea of an easy way to make those keypresses work on nes emulator?
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: NardJ on Aug 15, 2015, 08:41 pm
Did anyone try this on an esp8266? (26-80Mhz, 32kB, $4)

Specs: http://www.esp8266.com/viewtopic.php?f=5&t=3081


Nard
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: NardJ on Aug 23, 2015, 04:40 pm
Ok, i tried this myself. I had to remove all progmen references because esp8266 doesn't seem to handle this. It still runs on my pro mini, but on an esp8266-01, I get the following error on upload: '.text' will not fit in region íram_0_seq'. I suspect this means that there is not enough memory, but can't understand why, the esp8266-01 should have 512kb. If I reduce the rom table to 2 bytes the error persists. If I reduce the program code the error vanishes.

I have posted an question on the esp8266.com forum for help and am hoping I have to use some specific esp command to load the sketch in the correct memory-part.

Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: NardJ on Aug 24, 2015, 09:58 am
The original code (message 1 of this thread) works on esp8266!  :)

All credits go to martinayotte! esp8266 forum - explanation and sourcecode (http://www.esp8266.com/viewtopic.php?f=8&t=4704&p=26935#p26908)

Code: [Select]
Ø
Starting 6502 CPU...

6502 EhBASIC [C]old/[W]arm ?

Memory size ?

34047 Bytes free

Enhanced BASIC 2.22

Ready
FOR I=1 TO 10000:NEXT I

Ready

I did a speed test using "FOR I=1 TO 10000:NEXT I".The esp8266 finished in 15 seconds on 80 Mhz, and in 8 seconds on 160 Mhz! So this makes the esp8266 emulate an 1,13Mhz 6502. Haven't looked into the code optimization performed after message 1 of this thread, so could become even faster!

Furthermore the internal memory of the esp8266 makes it possible to allocate 32kB in code. (change "#define RAM_SIZE 1536" in cpu.cpp to "#define RAM_SIZE 34816")

This could be the base for a very cheap C64. I found some internet posts which claim that with an esp-12 it should be possible to get some sort of ISP interface. So using this to interface to 2x 32k256 chips (3.3V same as esp8266) we have 64kB. Another pin to input a ps/2 keyboard and a pin to output video commands (to another 8266 to off-load the 6502 emulator ) and we have a <$10 C64!
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: NardJ on Aug 24, 2015, 08:49 pm
And I got Pito's code for the Pic32 working on an esp2866-01. Since the rom is bigger, a little bit less memory remains. The speed is still about 112% that of an real C64. See attachement for code.

Code: [Select]

    **** COMMODORE 64 BASIC V2 ****

 64K RAM SYSTEM  28767 BASIC BYTES FREE

READY.
FOR I=1 TO 10000:NEXT I

READY.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: ahull on Sep 29, 2015, 06:25 pm
Here's a version for the STM32F103 series of microcontrollers. See http://www.stm32duino.com/viewtopic.php?f=19&t=433 (http://www.stm32duino.com/viewtopic.php?f=19&t=433) for more details.


The zipped version will work on pretty much any STM32F103, if the your variant  has more than the 20K RAM in the sketch, simply adjust where appropriate.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: Ti85 on Nov 18, 2016, 04:24 am
Looking at the 6502 code...I'm having trouble understanding the "exec6502(100)" line.  What does the 100 do?  Does the 100 slow down the code execution speed?  Is exec6502(1) faster?  Can someone please explain
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: FrankBoe on Mar 20, 2017, 08:35 pm
Here's a real c64 emulation on a Teensy:
Youtube: https://www.youtube.com/watch?v=CjijgL0VC6k
(please excuse the quality)

Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: oog on Jun 12, 2017, 03:18 pm
This is a great project. Very easy to implement and use. Thanks to miker00lz for sharing this. I took the program version from the first post and added some changes that might be from iterest to others.




EDO RAM Expansion board


I have build a RAM Expansion for my Arduino Nano from an old PC SIMM Module. Now I have about 2MByte with just two RAM chips and a '573 for the Address Latch. It uses just 12 Port Bits from the Arduino for interfacing. Access is not very fast on the Nano, cause I do not have a complete 8 Bit Port (when leaving Serial Interface, SPI and IIC free for expansion). Speed is about 100 KBytes Read or Write per second.
I directly read/wite the AVR ports for better performance, avoiding digitalWrite() and digitalRead() functions.
However, it can't expand the internal RAM of the microcontroller - the necessary signals are not available outside of the microcontroller chip. The RAM is just available with special load and store functions via port pins.


Arduino 6502 emulator with RAM Expansion board


After some benchmarking I use the Nano with 512Bytes of fast internal RAM for Zeropage and Stack. The external EDO Ram is used in the range from $0200 to $BFFF, so I have 48383 Bytes free for EhBasic.

The complete 2 MByte are available as Hi-Memory for the 6502. There are two 1KByte addressblocks in the Memory Map and the EDO RAM can be accessed within these two addressblocks. Each Himem has its own base address register an can be adjusted in Steps of 256 Bytes.


(http://proog.de/dl/d6502/edo6502_1.jpg)


Timer Tick

I also added some virtual Hardware.
The EDO RAM refresh uses a 1 msec timer interrupt (Timer1 Lib), which can also be used as some kind of stopwatch. From the refresh interrupt I generate a timer/counter with adjustable prescaler that can be used for benchmarking for example.

The address for the timer tick is $f0a0/$f0a1 (16 bit).
Write access sets the prescaler and also resets the counter.
Read access reads the current value.

Example:
doke $f0a0,1000
Reset the timer and set the prescaler to 1000 (increase tick counter after 1000 msec)

print deek($f0a0)
Print the current timer tick value

Example:
doke $f0a0,1:for i=0 to 1000:next:print deek($f0a0)
Reset the timer and set the prescaler to 1 (increase tick counter every millisecond)
Print the timer tick after benchmarking the FOR-Loop in milliseconds.

With high prescaler values the timer runs slow. A value of 30000 will generate just two ticks per minute (after 30000 mseconds).

The two bytes of the 16 bit timer are buffered, so read and write access using DEEK/ DOKE will give correct results.


AVR IO-Register / Hardware

The IO-Registers of the AVR are available for Peek and Poke within the address range $F100 - $F1FF.

Example:
poke $F1C6,65 send the letter "A" (ASCII 65) to the Serial Terminal

$F1C6 is $C6 of the AVR (UDR0, USART I/O Data Register)

Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: oog on Jun 12, 2017, 03:23 pm
Some Changes to EhBasic

I modified EhBasic a little bit.
Now Basic-Keywords are accepted in lower case. Some of the code examples makes use of it.
The Integer parameter check is less restrictive.
Poke now works if the second parameter is out of range. In this case the low byte will be taken. The Basic program does not abort.
Input does not abort the Basic program, if you just press enter without typing anything. It simply repeats the "?" prompt and gives you a second chance.
These modifications may have side effects to other functions. As far as I tested, it seems to be OK.


6502 Emulator bugfix decimal mode


I had to patch the adc() procedure in cpu.c for decimal mode to make the HEX$() function of EhBASIC work correctly. It did print wrong characters for 'A'-F'.
A discussion at 6502.org pointed out problems with HEX$() function and decimal mode on emulated 6502 CPUs.
I'm not sure if decimal mode is correct now, but it's good enough for the Hex$() function.

The sbc() function should also be fixed for decimal mode, if anybody needs it.


Memory Map for Arduino Uno / Nano

This is the actual Memory Map:

// 6502 memory map
// 0000-00FF: 6502 Zeropage (AVR internal)
// 0100-01FF: 6502 Stack (AVR internal)
// 0200-BFFF: external EDO RAM
// C000-EFFF: BASIC ROM (BIOS[])
// F000-FEFF: IO_AREA, Simulated I/O
//  F001      simulated ACIA write port
//  F004      simulated ACIA read port
//  F0A0/A1   timer tick counter (r) prescaler & reset (w)
//  F0B0/B1   edoPage1 hi memory address register
//  F0B2/B3   edoPage2 hi memory address register
//  F100/1FF  AVR IO Address space is mapped here
//  F400/7FF  1K hi memory block #1
//  F800/bFF  1K hi memory block #2

// FF00-FF7F: BOOT-ROM (reserved)
// FF80-FFFF: BOOT-ROM (mini_mon)
// FFFA/FFFB: NMI vector
// FFFC/FFFD: RESET vector
// FFFE/FFFF: IRQ vector


Startup message
The startup message has been reformatted to fit on a small 128x64 OLED display.

6502 EhBASIC
48383 Bytes free
Derived from
EhBASIC V2.22
Proog-17.06.08
Ready


Benchmark FOR-loop
Test program: Empty FOR-loop executed 10000 times.

doke $f0a0,10:for i=1 to 10000:next:print deek($f0a0)
4403

Ready

The FOR-loop runs 44.03 seconds.


Minute / Second Counter
A small program that uses the timer/counter to display minutes and seconds on the OLED display.

10 print chr$(12);chr$(3);"    Min:Sec"
20 doke $f0a0,1000:do:d=deek($f0a0):m=int(d/60):s=d-m*60
30 s$=str$(s):s$="0"+right$(s$,1-(s>9)):s$=right$(s$,2)
40 print chr$(6);"    ";m;":";s$;chr$(9)
50 do: loop until d<>deek($f0a0):loop

There are some simple control characters supported by the OLED driver:

chr$(1)   Set cursor Home, line 0
chr$(2-8)   Set cursor at start of line 1-7
chr$(9)   Clear end of line from current cursor position
chr$(10)   CR+LF
chr$(12)   Clear screen


Decode EhBASIC Program
This program decodes a BASIC program which is stored in RAM. It is useful to understand, how EhBASIC encodes the program lines.

Startaddress of BASIC program: $79/$7A.
Endaddress of BASIC program: $7B/$7C.

First element in a BASIC line is a link field to the next line.
Second element is the binary encoded line number.
Token are encoded with bit7 = 1 (128..255).
A BASIC line ends with a zero.

100 a=DEEK($79):e=DEEK($7b)
110 PRINT "Start:";HEX$(a,4),"End:";HEX$(e,4)
120 DO:PRINT HEX$(a,4), HEX$(DEEK(a),4), "Link":a=a+2
130 PRINT HEX$(a,4),HEX$(DEEK(a),4),"L=";DEEK(a):a=a+2
140 DO:c=PEEK(a):PRINT HEX$(a,4), HEX$(c,2),
150 IF c>=$80 THEN PRINT "Token";
160 IF c>31 AND c<$80 THEN PRINT CHR$(c);
170 IF c=0 THEN PRINT
180 PRINT:a=a+1
190 LOOP UNTIL c=0:LOOP UNTIL a>=e


output for the first line:

Start:0301    End:040E
0301          0316          Link
0303          0064          L= 100
0305          61            a
0306          C1            Token
0307          D2            Token
0308          24            $
0309          37            7
030A          39            9
030B          29            )
030C          3A            :
030D          65            e
030E          C1            Token
030F          D2            Token
0310          24            $
0311          37            7
0312          62            b
0313          29            )
0314          20             
0315          00           
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: yingqi95616 on Nov 26, 2018, 02:37 am
I have a (near) full Apple II+ emulator running on DUE, based on Mike's 6502 emulator. See here: https://youtu.be/nySPcWenwTQ for more details.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: airturk on Apr 30, 2019, 04:57 am
This is an awesome project.  Thanks for sharing it.  I came across it while looking for KIM-1 information.

I'm going to use couple of ideas from this project on my RetroShield for Arduino Mega project. (6502 connected to an Arduino Mega, and Mega currently emulating the Apple-I or KIM-1 hardware). I bought the 128KB SPI RAM and will implement the cache. I think it would be great. C64 BASIC will be next :)
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: markrhacker on Jun 13, 2019, 06:17 pm
Hello!
I've just started looking at this - like it.
Quick question - I have the c64 ROMS but how do I convert them so I can load them? 
Currently they are Hex arrays in code (0x??)

Thanks
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: markrhacker on Jun 14, 2019, 03:29 pm
solved it bin2h

Doh.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: guinnessimo on Jul 10, 2019, 08:27 pm
I know it's a few years since this thread started - but we now have 400/480MHz  STM32H743 Nucleo boards that are all part of the growing Arduino family.
 
There's enough grunt there to run a very fast 6502 emulation - and drive a VGA monitor directly from the STM32 LCD driver signals.

For about $25 investment you could have a super-retro 6502 machine - with 1MB of SRAM and 128K of Flash ROM.

Just a Thought


Ken
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: zoomx on Jul 11, 2019, 10:37 am
Right!
You can also use a BluePill, 64Kb flash (but it seems that is 128!) and 20Kb RAM about 2$
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: guinnessimo on Jul 13, 2019, 01:32 pm
Hi All,

I've tried Mike's code (as posted in the first post of this very long thread) on a 400MHz Nucleo STM32H743ZI


Arduino IDE now supports this board if you install the STM32 boards using Board Manager.


Mike's code runs ehBASIC on this board without modification, but you may wish to increase the RAMSIZE to 48K  (49152) and increase the serial speed to 921600 .

Using this program it will print the numbers 0 to 10000 in about 8 seconds

10 FOR I = 0 TO 10000
20 PRINT I
30 NEXT I

The Trigonometric function test runs in about 2 seconds

I also tried a 0 to 1,000,000 empty FOR-NEXT loop. This ran in 111 seconds.

I'm sure if I used some of the modifications and tweaks as discussed in this thread it could be a lot quicker - but if you want a quick and dirty 6502 emulation in a hurry, on a stock Nucleo board, this will only take about 30 minutes to get running


regards


Ken


Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: PICONTROLIT on Jul 30, 2019, 02:34 pm
Hi, I used the code found in this posting to load ROMs of various Commodore machines (PET2001 BASIC1, BASIC2 and CBM8032) with an ESP8266. I was even able to do it for a VIC20.
The screen output is not send via Serial but to a SSD1306 OLED Display.
Now I am struggeling with the C16/PLUS4 ROMs.
At first it came up with the "MONITOR" because it assumed the RUN/STOP key is pressed.
Now everything looks fine, I get the startup screen. The only issue is witn any numbers printed on the screen.
E.g. the bytes free info shows strange letters only.
Same is I enter a "PRINT 2" (or any other number or variable.
This comes back with:   |L   (Pipe/Capital L)

Any idea on that?
Alex

I am using this to access ROM/RAM:

uint8_t read6502(uint16_t address)
 {
  if (address < RAM_SIZE) return (RAM[address]);                                // free RAM for user
  if (address >= BASIC_START_ADDR)  return pgm_read_byte_near(BIOS + (address - BASIC_START_ADDR));
  return (0x00);
 }


void write6502(uint16_t address, uint8_t value)
{
  if (address < RAM_SIZE) RAM[address] = value;
}
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: fjcallejao on Nov 16, 2019, 09:52 am
Hello, i have a problem with compilation of "arduino_6502.ino & cpu.c".

---------------------------------------------------------------------
Arduino:1.8.10 (Windows 10), Tarjeta:"Arduino/Genuino Uno"

cpu.c:70:1: error: unknown type name 'prog_uchar'

 prog_uchar BIOS[10496] PROGMEM = {

 ^~~~~~~~~~

cpu.c:70:12: error: variable 'BIOS' must be const in order to be put into read-only section by means of '__attribute__((progmem))'

 prog_uchar BIOS[10496] PROGMEM = {

            ^~~~

cpu.c:1028:1: error: unknown type name 'prog_uchar'

 prog_uchar BIOStop[256] PROGMEM = {

 ^~~~~~~~~~

cpu.c:1028:12: error: variable 'BIOStop' must be const in order to be put into read-only section by means of '__attribute__((progmem))'

 prog_uchar BIOStop[256] PROGMEM = {

            ^~~~~~~

exit status 1
unknown type name 'prog_uchar'
------------------------------------------------------------

I need help. Thanks
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: j0WvW on Dec 21, 2019, 11:08 am
any idea why the execution of miker00lz 6502 emulator code fail on arduino uno/nano?
I get stucked on the memory size part
Any hints are welcome to go further ;)
thanks
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: fjcallejao on Dec 22, 2019, 09:06 pm
It is a test with a TTGO display and its graphic library, with a small tribute to Commodore 64. I use a serial wifi connection with the putty configured in rlogin. It is a first version that can be improved. Thank the community for the development of libraries. I upload photos and the program.
Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: adamius on Jan 03, 2020, 04:01 am
Hey

Whats the license this code is under? MIT, LGPL, public domain?
I'm considering converting this thing to lua.

thanks





Title: Re: Arduino 6502 emulator + BASIC interpreter
Post by: fjcallejao on Mar 28, 2020, 12:07 pm
Sorry, I don't know the license.
I am trying to save and read the basic in sd card, in case someone has worked on it.
It would be interesting to enable the save command and the load command of the basic commodore.
Title: Re: Arduino 6502 emulator + BASIC interpreter (unknown type name 'prog_uchar')
Post by: jelly007bean on Mar 31, 2020, 09:47 pm
unknown type name 'prog_uchar'

I had the same problem.


To fix it:

1. Make sure the file pgmspace.h is present so that

#include <pgmspace.h>

will reference it. You could simple copy it to the same place as stdint.h
I found it in an older version of the Arduino IDE in arduino-0021.zip
I have attached this file.

2. Change


prog_uchar BIOS[10496] PROGMEM = {


To

const char BIOS[10496] PROGMEM = {

3. Change

prog_uchar BIOStop[256] PROGMEM = {

To

const char BIOStop[256] PROGMEM = {



That's all, and it compiled and loaded, then used a Terminal to run it.