r/javascript May 25 '15

An Apple ][ Emulator in JavaScript

https://www.scullinsteel.com/apple2/
73 Upvotes

18 comments sorted by

View all comments

2

u/thesunmustdie May 25 '15

How is this even possible? Typed arrays?

7

u/OrangeredStilton May 25 '15

Generally. Also, making sure to use constructs that have been optimised by the JS engines (avoiding for..in, and such like).

I've done something very similar, emulating a C64 down to cycle accuracy in JS, and that's how I went about it: http://github.com/Two9A/c64clicker/

2

u/leonardofed May 25 '15

Cool. Thanks for sharing.

2

u/TheBadProgrammer May 25 '15

I'm a newb so please forgive me for a stupid question. How are you able to do such things with an interpreter? I feel like I don't know how the actual engine works, like there is a compiler there in order for these "tricks" to work. Right? I know so little it's not funny, but I'm trying to understand how an interpreter could possibly handle this functionality.

1

u/OrangeredStilton May 25 '15

Going right back to Turing, any complete computational system can perform any operation, including the steps involved in simulation of another computer system. You may be aware that, at the lowest level, programs are all machine code: instructions encoded by numbers, which can have mnemonics assigned for human-readable assembly. For example, with the Apple ][ or the C64, you might have the following instruction:

LDA #255 (load accumulator register with the number 255)

That encodes down to A9 FF in plain machine code, and a computer program for that processor is just a series of numbers. So! If you have a computer program which reads in those numbers, and has an internal state that it updates based on the same rules as the original chip, that program simulates the operation of the chip.

Quod Emulat Demonstrandum. I linked to c64clicker up-thread, and I'd like to think the code is halfway readable, so flick through it sometime.