Source code to all the tutorials on emulator101.com
kpmiller / emulator101 Goto Github PK
View Code? Open in Web Editor NEWSource code to all the tutorials on emulator101.com
License: The Unlicense
Source code to all the tutorials on emulator101.com
License: The Unlicense
The classic arcade Space Invaders had a B&W monitor. The colors of the aliens were applied via transparent gels on the screen. I added these to the emulation to improve the antic feel of the game.
InvadersView.m
#define RGB_ON 0xFFFFFFFFL
#define RGB_OFF 0x00000000L
#define RGB_GREEN 0x00FF0000L
#define RGB_RED 0x0000FF00L
for (p=0; p<8; p++)
{
if ( 0!= (pix & (1<<p))) {
if ( (j > 8 && j < 76) || (j <= 8 && i > 20 && i < 108) )
*p1 = RGB_GREEN;
else if (j > 190 && j < 222)
*p1 = RGB_RED;
else
*p1 = RGB_ON;
} else
*p1 = RGB_OFF;
p1-=224; //next line
}
Just a small comment about this page, and in particular, this:
{
//saw this in the inspected code, never saw it called
printf ("print char routine called\n");
Actually, this code is called. When the error message CPU HAS FAILED! ERROR EXIT=
is displayed, the routine continues, it picks up the address on the stack, puts it in H
and L
with XTHL
, and then displays the address in these two registers character per character. The routine BYTE0
takes the value of A
and displays its two digits.
Once you have implemented this correctly, the error message contains the address where the exit occurred. This is what my emulator displays, for example:
CPU HAS FAILED! ERROR EXIT=01B6
Great document otherwise, thanks!
0xb0
in the disassembler has an instruction, but in the emulator function it is set to unimplemented.
There are actually a few instructions like this in this area.
Line 370 in 157a840
Shouldn't the pc be updated to +=3? LXI is a 3bytes instruction.
in http://www.emulator101.com/arithmetic-group.html instruction 0x80 is getting implemented, but in 8080emu-first50.c it is not
In http://www.emulator101.com/disassembler-pt-2.html the second to last instruction in the sample disassembly is listed as SRA
, whereas it should be XRA
:
...
002a JZ $0042
002d LDA $20eb
0030 CPI #$99
0032 JZ $003e
0035 ADI #$01
0037 DAA
0038 STA $20eb
003b CALL $1947
003e SRA A <=====
003f STA $20ea
Hey - I was following the emulator101 site (and Im thankful it exists! What a wonderful getting started with emulation write up!)
I just have a question, I tried to disassemble invaders, but looks like at some point, the program is not ending, but just continues to chew up space by writing:
0aea XRA A z.p.. A $00 B $00 C $00 D $1f E $b0 H $3e L $01 SP 2400
0aeb OUT #$03 z.p.. A $00 B $00 C $00 D $1f E $b0 H $3e L $01 SP 2400
0aed OUT #$05 z.p.. A $00 B $00 C $00 D $1f E $b0 H $3e L $01 SP 2400
0aef CALL $1982 z.p.. A $00 B $00 C $00 D $1f E $b0 H $3e L $01 SP 23fe
1982 STA $20c1 z.p.. A $00 B $00 C $00 D $1f E $b0 H $3e L $01 SP 23fe
1985 RET z.p.. A $00 B $00 C $00 D $1f E $b0 H $3e L $01 SP 2400
0af2 EI z.p.. A $00 B $00 C $00 D $1f E $b0 H $3e L $01 SP 2400
0af3 CALL $0ab1 z.p.. A $00 B $00 C $00 D $1f E $b0 H $3e L $01 SP 23fe
0ab1 MVI A,#$40 z.p.. A $40 B $00 C $00 D $1f E $b0 H $3e L $01 SP 23fe
0ab3 JMP $0ad7 z.p.. A $40 B $00 C $00 D $1f E $b0 H $3e L $01 SP 23fe
0ad7 STA $20c0 z.p.. A $40 B $00 C $00 D $1f E $b0 H $3e L $01 SP 23fe
0ada LDA $20c0 z.p.. A $40 B $00 C $00 D $1f E $b0 H $3e L $01 SP 23fe
0add ANA A ..... A $40 B $00 C $00 D $1f E $b0 H $3e L $01 SP 23fe
0ade JNZ $0ada ..... A $40 B $00 C $00 D $1f E $b0 H $3e L $01 SP 23fe
0ada LDA $20c0 ..... A $40 B $00 C $00 D $1f E $b0 H $3e L $01 SP 23fe
0add ANA A ..... A $40 B $00 C $00 D $1f E $b0 H $3e L $01 SP 23fe
0ade JNZ $0ada ..... A $40 B $00 C $00 D $1f E $b0 H $3e L $01 SP 23fe
0ada LDA $20c0 ..... A $40 B $00 C $00 D $1f E $b0 H $3e L $01 SP 23fe
0add ANA A ..... A $40 B $00 C $00 D $1f E $b0 H $3e L $01 SP 23fe
0ade JNZ $0ada ..... A $40 B $00 C $00 D $1f E $b0 H $3e L $01 SP 23fe
0ada LDA $20c0 ..... A $40 B $00 C $00 D $1f E $b0 H $3e L $01 SP 23fe
this repeats over and over until the file chews up all my space or ram.
I was wondering, is the disassembler supposed to get to a certain point and stop itself?
Most of the beginning stuff seems to be in line with the tutorial, but maybe I was doing something wrong?
I took your program and ran:
invaders % cc -g -O0 8080emu-first50.c -o test.out
invaders % sudo gdb test.out
I got gdb to work which was not super easy hah, I am also on a Mac, so your tutorials are great!
GNU gdb (GDB) 9.1
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin19.3.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test.out...
Reading symbols from /Users/futurepr0n/Downloads/invaders/test.out.dSYM/Contents/Resources/DWARF/test.out...
(gdb) run invaders
Starting program: /Users/futurepr0n/Downloads/invaders/test.out invaders
[New Thread 0xd03 of process 55472]
[New Thread 0x1603 of process 55472]
0000 NOP ..... A $00 B $00 C $00 D $00 E $00 H $00 L $00 SP 0000
0001 NOP ..... A $00 B $00 C $00 D $00 E $00 H $00 L $00 SP 0000
0002 NOP ..... A $00 B $00 C $00 D $00 E $00 H $00 L $00 SP 0000
0003 JMP $18d4 ..... A $00 B $00 C $00 D $00 E $00 H $00 L $00 SP 0000
18d4 LXI SP,#$2400 ..... A $00 B $00 C $00 D $00 E $00 H $00 L $00 SP 2400
18d7 MVI B,#$00 ..... A $00 B $00 C $00 D $00 E $00 H $00 L $00 SP 2400
18d9 CALL $01e6 ..... A $00 B $00 C $00 D $00 E $00 H $00 L $00 SP 23fe
01e6 LXI D,#$1b00 ..... A $00 B $00 C $00 D $1b E $00 H $00 L $00 SP 23fe
01e9 LXI H,#$2000 ..... A $00 B $00 C $00 D $1b E $00 H $20 L $00 SP 23fe
01ec JMP $1a32 ..... A $00 B $00 C $00 D $1b E $00 H $20 L $00 SP 23fe
1a32 LDAX D ..... A $01 B $00 C $00 D $1b E $00 H $20 L $00 SP 23fe
1a33 MOV M,A ..... A $01 B $00 C $00 D $1b E $00 H $20 L $00 SP 23fe
1a34 INX H ..... A $01 B $00 C $00 D $1b E $00 H $20 L $01 SP 23fe
1a35 INX D ..... A $01 B $00 C $00 D $1b E $01 H $20 L $01 SP 23fe
1a36 DCR B .sp.. A $01 B $ff C $00 D $1b E $01 H $20 L $01 SP 23fe
1a37 JNZ $1a32 .sp.. A $01 B $ff C $00 D $1b E $01 H $20 L $01 SP 23fe
1a32 LDAX D .sp.. A $00 B $ff C $00 D $1b E $01 H $20 L $01 SP 23fe
1a33 MOV M,A .sp.. A $00 B $ff C $00 D $1b E $01 H $20 L $01 SP 23fe
1a34 INX H .sp.. A $00 B $ff C $00 D $1b E $01 H $20 L $02 SP 23fe
1a35 INX D .sp.. A $00 B $ff C $00 D $1b E $02 H $20 L $02 SP 23fe
1a36 DCR B .s... A $00 B $fe C $00 D $1b E $02 H $20 L $02 SP 23fe
1a37 JNZ $1a32 .s... A $00 B $fe C $00 D $1b E $02 H $20 L $02 SP 23fe
1a32 LDAX D .s... A $00 B $fe C $00 D $1b E $02 H $20 L $02 SP 23fe
1a33 MOV M,A .s... A $00 B $fe C $00 D $1b E $02 H $20 L $02 SP 23fe
1a34 INX H .s... A $00 B $fe C $00 D $1b E $02 H $20 L $03 SP 23fe
1a35 INX D .s... A $00 B $fe C $00 D $1b E $03 H $20 L $03 SP 23fe
1a36 DCR B .s... A $00 B $fd C $00 D $1b E $03 H $20 L $03 SP 23fe
1a37 JNZ $1a32 .s... A $00 B $fd C $00 D $1b E $03 H $20 L $03 SP 23fe
1a32 LDAX D .s... A $10 B $fd C $00 D $1b E $03 H $20 L $03 SP 23fe
1a33 MOV M,A .s... A $10 B $fd C $00 D $1b E $03 H $20 L $03 SP 23fe
1a34 INX H .s... A $10 B $fd C $00 D $1b E $03 H $20 L $04 SP 23fe
1a35 INX D .s... A $10 B $fd C $00 D $1b E $04 H $20 L $04 SP 23fe
1a36 DCR B .sp.. A $10 B $fc C $00 D $1b E $04 H $20 L $04 SP 23fe
1a37 JNZ $1a32 .sp.. A $10 B $fc C $00 D $1b E $04 H $20 L $04 SP 23fe
1a32 LDAX D .sp.. A $00 B $fc C $00 D $1b E $04 H $20 L $04 SP 23fe
1a33 MOV M,A .sp.. A $00 B $fc C $00 D $1b E $04 H $20 L $04 SP 23fe
1a34 INX H .sp.. A $00 B $fc C $00 D $1b E $04 H $20 L $05 SP 23fe
1a35 INX D .sp.. A $00 B $fc C $00 D $1b E $05 H $20 L $05 SP 23fe
1a36 DCR B .s... A $00 B $fb C $00 D $1b E $05 H $20 L $05 SP 23fe
1a37 JNZ $1a32 .s... A $00 B $fb C $00 D $1b E $05 H $20 L $05 SP 23fe
1a32 LDAX D .s... A $00 B $fb C $00 D $1b E $05 H $20 L $05 SP 23fe
1a33 MOV M,A .s... A $00 B $fb C $00 D $1b E $05 H $20 L $05 SP 23fe
1a34 INX H .s... A $00 B $fb C $00 D $1b E $05 H $20 L $06 SP 23fe
1a35 INX D .s... A $00 B $fb C $00 D $1b E $06 H $20 L $06 SP 23fe
1a36 DCR B .sp.. A $00 B $fa C $00 D $1b E $06 H $20 L $06 SP 23fe
I guess Im wondering if the Dissassembly should cap itself or exit after a certain amount of time and condition? In the documents and sites you shared as resources, there was quite a bit of codes I ended up receiving that aren't listed anywhere. STA $20c0 - I did not see any STA opcodes.
Just wondered what my expectations should be and how to properly disassemble and understand how to use the debugger for such things.
at first we stepped through with break points, but after that we were expected to try and "run" the program and capture the results, and the only way I knew how to do this was by running the GDB and capturing the output. Was there another way to properly do this? Should I just be running ./test.out and then it would know to load my invaders files?
I created the invaders (packed) and also have the individual invaders.h/g/etc files in the same directory. I wasn't sure how to tell the program to 'run invaders' other than entering GDB and writing the command.
perhaps this is rather rudimentary yet these two missing file errors is what I am getting...
error: /Users/mvelicangil/Desktop/emulator_i8080_xcode/emulator101-master-3/CocoaPart3-Attract/Invaders/invaders.e: No such file or directory
and
error: /Users/mvelicangil/Desktop/emulator_i8080_xcode/emulator101-master-3/CocoaPart3-Attract/Invaders/invaders.f: No such file or directory
along with
red alerts for invaders.h and invaders.g under the supporting files...
where only main.m resides
ROR opcodes (0x63, 0x73) are swapped up in the table
https://github.com/kpmiller/emulator101/blob/master/6502Disassembler/6502ops.csv#L130
0x63 should be ABS, 0x73 should be ABSX.
Other than that. Thank you very much!
http://
instead of https://
, so the link is dead.Great guide overall, thanks!
From a comment "Small thing i noticed, 0x20 RIM and 0x30 SIM were introduced in the 8085 according to http://www.nj7p.org/Manuals/PDFs/Intel/9800301D.pdf
there is a typo on http://www.emulator101.com/disassembler-pt-1.html,
fread(buffer, fsize, 1, f);
according to fread defination, it shoud be
fread(buffer, 1, fsize, f);
Shouldn't this be 0x08 == (psw & 0x08)
?
Line 828 in 0c327a7
Reported on disqus/twitter:
the reference for CALL specifies SP<-SP+2, but comparing with the JS emulator I believe it should actually be SP<-SP-2
Typo: 0x26 should be: "H <- byte 2"
on line 32:
uint8_t lastnib = code[1]>>4;
should be changed to :
uint8_t lastnib = code[1] & 0x0f;
because we are getting the second half of the second byte of code. The currrent code gets the first half of the last byte. So when opcode 8AB1 is called, we want to get that last "1" to show that we need to do a bitwise OR operation (VA = VA | VB). Currently, the code would get "B" as the lastnib.
As per the intel manual, memory[stack pointer] bit 1 should be always set. Now it is always reset, similarly to bits 3 and 5.
CHIP-8 opcodes 8xy5 and 8xy7 set VF to 0 in case VX and VY are equal but should set it to 1 as no borrow takes place when subtracting equal numbers. The compare operation setting the borrow should be >=
instead of >
.
All the variants of the emulator files have CMC
(Complement Carry) implemented as "clear carry":
emulator101/CocoaPart7-Threading/8080emu.c
Line 744 in 0c327a7
It seems the test program (cpudiag.asm) does not catch that specific error and will pass the CPU as operational because it only checks that CMC results in the carry being set from 1 to 0:
STC
CNC CPUER ;TEST "STC"
CMC
CC CPUER ;TEST "CMC
While I appreciate the idea of having screen and stack in CHIP-8 memory like in the original CHIP-8, the init code sets the stack pointer to 0xFA0 and the screen to 0xF00. The screen is where it would be on a 4k COSMAC VIP, no issue with that, but the stack is inside the screen, so drawing and subroutine calls would interfere with each other.
My guess is, the 0xFA0 are a typo and might have meant to be 0xEA0, which is the start of the stack on the COSMACVIP, but as the stack of this implementation grows from the upper end downwards, it would need to start with a stack pointer of 0xED0, like the original.
http://www.emulator101.com/reference/8080-by-opcode.html has a typo for the RAR description. It should be A = A >> 1; bit 7 = prev CY; CY = prev bit 0
instead of A = A >> 1; bit 7 = prev bit 7; CY = prev bit 0
(similar to how RAL is described).
The opcode 8xy4 sets VF before setting the result, resulting in the wrong state in case VX is VF. The flags test of Timendus excellent test suite would thus fail on that opcode.
When is state->sp initialized?
In the Memory Maps
section ("Combining information from here and here, I learn that the game's ROM is located at address 0, and the game has 8K of RAM starting at address $2000."), the links to http://www.ascotti.org/programming/side/hardware.htm and https://www.mamedev.org/source/src/mame/drivers/8080bw.c.html are both dead.
Hi
I don't know how to contact you as your twitter account seem to be disabled
I'm working on a swift equivalent of your work.
I found the tuto amazing and followed it step by step, now I can't seem to translate or even write the code for screen display, the resulting image is garbage.
Would you be able to help me? I can give the code in Swift so others can reuse it. It's an academic project to prepare a course for students.
Thanks a lot in advance.
[email protected]
http://www.emulator101.com/logical-operations.html
In C, the logical and operation is a single ampersand '&'.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.