This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Lewisk3's profile picture

6502 CPU Emulated in Lua

Started by Lewisk3, 25 March 2017 - 11:37 PM
Lewisk3 #1
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:
SpoilerHow to compile a program -
First, edit a file and put some code in it, example:

lda #$15
loop:
tax
inx
txa
ldx #$00
cmp #$55
bne loop
If you wanted to run your program in Graphics mode, type this
6502 -m 2
This will attach a render to your program that will render the screen
when memory within its position is updated, from 0200 to 0990 hex.
Format is as follows, in bytes starting at 0200: TextBack, Char (Where text is MSB and back is LSB and char is full Byte)
If you want to run your program without compiling it first type this
6502 -rn <name of="" file="">
This will compile your program to .tempasm and run it then delete it afterward.
To compile, run this command
6502 -c <name of="" file=""> <exported file="" name="">
This will compile your ASM into something the program runner can
understand.
Now to run your file put
6502 -r <compiled asm=""> (You can also include a -s to run the program step mode)


Screenshots:
Spoiler

http://imgur.com/a/l0Xij Program execution.
http://imgur.com/a/gst4B Branching.
http://imgur.com/a/IbCZK Registers and Bitwise instructions.
http://imgur.com/a/7Xv1R Ascii loading and Indirect instruction modes.
http://imgur.com/a/znpZL Interrupt returning.
http://imgur.com/a/rG6vn Subroutine jumping and returning.
http://imgur.com/a/GKjMZ Editor, with syntax highlighting.
http://imgur.com/a/aG7N3 Rendering to the Screen.
http://imgur.com/jv45ciW Disassembly.
http://imgur.com/a/QOVIX Compiled program.




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 on 22 June 2017 - 11:10 AM
Creeper9207 #2
Posted 26 March 2017 - 04:37 AM
this is super cool!
SquidDev #3
Posted 26 March 2017 - 05: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?
Lewisk3 #4
Posted 26 March 2017 - 08:33 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?

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 on 26 March 2017 - 06:33 PM
electrodeyt #5
Posted 11 April 2017 - 07:11 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?
The Redpower Computer IS a 6502, so you would just have to port MineOS to the emulator.
Bomb Bloke #6
Posted 12 April 2017 - 02:46 AM
I was under the impression that it was not an exact copy.
Lewisk3 #7
Posted 12 April 2017 - 09:00 AM
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.
LDDestroier #8
Posted 12 April 2017 - 02:32 PM
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.
Bomb Bloke #9
Posted 13 April 2017 - 12:51 AM
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.
Lewisk3 #10
Posted 13 April 2017 - 10:41 PM
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 on 13 April 2017 - 08:42 PM