Jump to content




6502 CPU Emulated in Lua

api utility

9 replies to this topic

#1 Lewisk3

  • Members
  • 150 posts

Posted 26 March 2017 - 12:37 AM

6502 Emulation in Computercraft (Fully working Assembler)


What is 6502?
6502 is an older CPU running off an 8bit Assembly language, most commonly used in the 80s
It was soo common, even the Terminator was programmed on it!

Assembly in Computercraft what?!?!
The main reason I choose to emulate the 6502 language onto CC was that, well I really enjoy the language and
I wanted to challenge myself to see if I could even do it, with 14 days of continuous study and lots and lots of coding,
here we are!

What are the features of this program so far?
It is a fully functioning 6502 CPU, everything works to that of ~90% accuracy compared to the original, physical version.
Individual features - Multiline Execution, Assembler with label, variables and Decimal Coded Byte support, Syntax Interpreter and Graphical Execution, The entire 6502 Assembly OpCodes.


Memory Specs
The memory method I use utilizes the string.char library to store 1 byte per virtual emulated byte.
Memory - 64K
Stack - 256B
Screen memory - 792B (0200 to 0990, counting end and start indexes.)
Key input - $FF
Random generator - $FE
Each address of the memory is an index divisible by 16, as 16 bytes are used per index, the remainder of this number becomes the memory offset.

Bug Testing
As there are 148 different instruction modes in 6502, I am going to need some help testing if they all work the way they are intended,
if any bugs are found, please post about them below!


Download: pastebin get jEtwni5z 6502
Editor: pastebin get 4YdqE1zS editasm
Example that features key input RNG and screen rendering: pastebin get WxMivJq5 test.asm

P.S I'm also working on getting it working with any vanilla Lua IDE!
Special thanks to, SquidDev for help with the compiler!


Tutorials:
Spoiler


Screenshots:
Spoiler




Update 6/20/17
- Some changes in optimization have broken the screen renderer, I apologize that I didn't notice that sooner, it has been fixed.
- New experimental version (qJ4rjZ8Q, has an extra run mode that is slightly faster. Also, includes an ISP (Instructions Per Second) counter.
- Found a bug with the variable char conversion that made assigning a variable a char, effectively useless, fixed that.

- Added Decimal Flag support!
- Fixed some grammar issues on this page :P

Edited by Lewisk3, 22 June 2017 - 12:10 PM.


#2 Creeper9207

  • Members
  • 211 posts

Posted 26 March 2017 - 03:37 AM

this is super cool!

#3 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 26 March 2017 - 04:08 PM

Oh wow, this is impressive. Making an emulator for a real CPU has been on my todo list for a while, but it looks like you beat me too it. I guess this begs the question, how long is it til you'll be able to emulate something like the BBC Micro or Redpower's computer?

#4 Lewisk3

  • Members
  • 150 posts

Posted 26 March 2017 - 07:33 PM

View PostSquidDev, on 26 March 2017 - 04:08 PM, said:

Oh wow, this is impressive. Making an emulator for a real CPU has been on my todo list for a while, but it looks like you beat me too it. I guess this begs the question, how long is it til you'll be able to emulate something like the BBC Micro or Redpower's computer?

Thanks for the support!
This project is the biggest CC program I have ever made, it's been challenging getting everything working, especially the syntax.
I think before any of that happens, i have to get multi-line code execution up and running, which involves a working assembler that can handle labels and variables. (I have a couple ideas about how to do this)
But my biggest hope is to emulate the NES's GPU and screen memory processing then maybe i could get some sort NES games running on CC! given, not with the same graphics or speed as on the actual System.

Edited by Lewisk3, 26 March 2017 - 07:33 PM.


#5 electrodeyt

  • Members
  • 3 posts

Posted 11 April 2017 - 06:11 PM

View PostSquidDev, on 26 March 2017 - 04:08 PM, said:

Oh wow, this is impressive. Making an emulator for a real CPU has been on my todo list for a while, but it looks like you beat me too it. I guess this begs the question, how long is it til you'll be able to emulate something like the BBC Micro or Redpower's computer?
The Redpower Computer IS a 6502, so you would just have to port MineOS to the emulator.

#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 12 April 2017 - 01:46 AM

I was under the impression that it was not an exact copy.

#7 Lewisk3

  • Members
  • 150 posts

Posted 12 April 2017 - 08:00 AM

View PostBomb Bloke, on 12 April 2017 - 01:46 AM, said:

I was under the impression that it was not an exact copy.

Yeah, this emulated version is pretty exact, all 148 instructions had the same OP Codes as they do on your normal 6502, the only thing that isn't exact is the register-based instructions, as A, X and Y decode to 01, 02 and 03 which they do not on the original 6502.

But the emulator also has some extended features such as a lot of screen dedicated memory, specially designed for Computercraft colors and such.
I will make another mode that is for non-CC based things as I do have a Pure Lua port of this on the todo.

#8 LDDestroier

  • Members
  • 1,095 posts
  • LocationACDC Town

Posted 12 April 2017 - 01:32 PM

View PostLewisk3, on 26 March 2017 - 07:33 PM, said:

But my biggest hope is to emulate the NES's GPU and screen memory processing then maybe i could get some sort NES games running on CC! given, not with the same graphics or speed as on the actual System.

Oh my god, do that. Now.

#9 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 12 April 2017 - 11:51 PM

View PostLewisk3, on 12 April 2017 - 08:00 AM, said:

View PostBomb Bloke, on 12 April 2017 - 01:46 AM, said:

I was under the impression that it was not an exact copy.

Yeah, this emulated version is pretty exact, ....

Er, "it" as in the Redpower Computer being discussed.

#10 Lewisk3

  • Members
  • 150 posts

Posted 13 April 2017 - 09:41 PM

View PostBomb Bloke, on 12 April 2017 - 11:51 PM, said:

View PostLewisk3, on 12 April 2017 - 08:00 AM, said:

View PostBomb Bloke, on 12 April 2017 - 01:46 AM, said:

I was under the impression that it was not an exact copy.
Yeah, this emulated version is pretty exact, ....
Er, "it" as in the Redpower Computer being discussed.
I understand, i was mainly replying to share some more information about the emulator and for referencing sake just in case another discussion of the Redpower computer starts up, making sure
people know my emulation is more exact than redpowers.

Edited by Lewisk3, 13 April 2017 - 09:42 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users