27. What is an
emulator?
In computing, an emulator is hardware or software or both that
duplicates (or emulates) the functions of a first computer system (the
guest) in a different second computer system (the host), so that the
emulated behaviour closely resembles the behaviour of the real
system.
http://en.wikipedia.org/wiki/Emulator
73. NES Memory
• 2KiB onboard
• Internally stored zero-page, stack, general
purpose memory
74. NES Memory
• 2KiB onboard
• Internally stored zero-page, stack, general
purpose memory
• Addressable up to 0x10000
75. NES Memory
• 2KiB onboard
• Internally stored zero-page, stack, general
purpose memory
• Addressable up to 0x10000
• Program ROM contain opcodes, graphics
and sound
78. Typed Arrays
• TypedArrays help performance critical
applications such as emulators
• Makes it trivial to reach any specified offset
79. Typed Arrays
• TypedArrays help performance critical
applications such as emulators
• Makes it trivial to reach any specified offset
• http://www.khronos.org/registry/
typedarray/specs/latest/
80. Nes.Cpu = function() {
// ...
// Our device has 2KiB of RAM.
var memoryBuffer = new ArrayBuffer(0x800);
// 6502 is a 8 bit processor, able
// to address 8 bits at once.
this.memory = new Uint8Array(memory);
// ...
};
96. Handling Opcodes
• Read location in PC from memory
• Lookup opcode
• Find addressing mode
• Process opcode
97. Handling Opcodes
• Read location in PC from memory
• Lookup opcode
• Find addressing mode
• Process opcode
• Increment PC
98. Cpu.prototype.emulateCycle = function() {
// ...
// Get opcode at PC.
var opcode = this.getOpcode(this.loadMemory(this.pc));
var address;
// Get final address via addressing mode of opcode.
switch (opcode.addressingMode) {
// Zero Page
// Uses a single operand which address
// memory in zero page (0x0000-0x00FF).
case this.addressingModes.ZERO_PAGE:
address = this.loadMemory(this.pc + 1);
break;
}
this.pc += opcode.bytes;
// ...
};
100. Process All The Opcodes
• Read technical paper twice, implement
once
101. Process All The Opcodes
• Read technical paper twice, implement
once
• Debugging is hard, so get it right
102. Process All The Opcodes
• Read technical paper twice, implement
once
• Debugging is hard, so get it right
• When you don’t get it right, try again
118. TileS
• Each 8x8 tile occupies 16b
• Position of bit denotes position of pixel
119. TileS
• Each 8x8 tile occupies 16b
• Position of bit denotes position of pixel
• First 8 bits combine with second 8 bits
120. TileS
• Each 8x8 tile occupies 16b
• Position of bit denotes position of pixel
• First 8 bits combine with second 8 bits
• Combine bits to get colour index
126. Nametables
• Bytes which index pattern table
• 32 horizontal and 30 vertical tiles, 960 tiles
• 1KiB long, delta contains extra colour
information
128. Nothing is wasted
• Remaining 64b stores extra colour
information (attribute bytes)
129. Nothing is wasted
• Remaining 64b stores extra colour
information (attribute bytes)
• Attribute bytes map to 16 tiled blocks of
nametable (64 32x32 pixel tiles)
130. Nothing is wasted
• Remaining 64b stores extra colour
information (attribute bytes)
• Attribute bytes map to 16 tiled blocks of
nametable (64 32x32 pixel tiles)
• Each of these blocks is sub-blocked into 4
tiles (4 16x16 pixel tiles)
131. Nothing is wasted
• Remaining 64b stores extra colour
information (attribute bytes)
• Attribute bytes map to 16 tiled blocks of
nametable (64 32x32 pixel tiles)
• Each of these blocks is sub-blocked into 4
tiles (4 16x16 pixel tiles)
• Sub blocks affected by 2 bits from attribute
bytes
132. Put it all together
0. 1 0 1 1 0 0 1 1
8. 0 0 1 0 1 0 0 1
960. 1 1 0 0 0 0 1 1
=============================
1110 1100 0011 ...
=============================
133. Put it all together
0. 1 0 1 1 0 0 1 1
8. 0 0 1 0 1 0 0 1
960. 1 1 0 0 0 0 1 1
=============================
1110 1100 0011 ...
=============================
134. Put it all together
0. 1 0 1 1 0 0 1 1
8. 0 0 1 0 1 0 0 1
960. 1 1 0 0 0 0 1 1
=============================
1110 1100 0011 ...
=============================
135. Put it all together
0. 1 0 1 1 0 0 1 1
8. 0 0 1 0 1 0 0 1
960. 1 1 0 0 0 0 1 1
=============================
1110 1100 0011 ...
=============================
136. // Byte at offset 0.
var a = 0xB3;
// Byte at offset 8.
var b = 0x29;
// Byte at offset 960.
var c = 0xC3;
var merged = (a & 0x80) >> 0x6;
merged |= b >> 0x7;
merged |= (c & 0x3) << 0x2;
150. Sound
• Vibration of air
• Represented as waves w/ freq. and
amplitude
• Frequency is number of phase changes per
second
151. Sound
• Vibration of air
• Represented as waves w/ freq. and
amplitude
• Frequency is number of phase changes per
second
• Amplitude is volume of the wave measured
in dB
164. PULSE Wave
0x4000. 1011 1111 Volume
Low order bits
0x4002. 0001 0111 of raw period
0x4003. 0000 0001
165. PULSE Wave
0x4000. 1011 1111 Volume
Low order bits
0x4002. 0001 0111 of raw period
High order bits
0x4003. 0000 0001 of raw period
166. PULSE Wave
0x4000. 1011 1111 Volume
Low order bits
0x4002. 0001 0111 of raw period
round(111860.8 / 0x117) - 1) = 400hz
High order bits
0x4003. 0000 0001 of raw period
180. Controller
• Two controllers, mapped to CPU memory
at 0x4016 and 0x4017
• When written to, controller’s state is
stored in sequential 8 bytes, as low order
bit
191. Your very own emulator
• Spend a lot of time researching
192. Your very own emulator
• Spend a lot of time researching
• Start programming your own programs
193. Your very own emulator
• Spend a lot of time researching
• Start programming your own programs
• http://skilldrick.github.com/easy6502/
194. Your very own emulator
• Spend a lot of time researching
• Start programming your own programs
• http://skilldrick.github.com/easy6502/
• http://www.6502.org/books
206. Possibilities
• Multiplayer via WebSockets
• Vibration API for force feedback
• File API and application cache for portable
offline emulators
207. Possibilities
• Multiplayer via WebSockets
• Vibration API for force feedback
• File API and application cache for portable
offline emulators
• localStorage for battery backed saved state