Jump to content
Sign in to follow this  
  • entries
    292
  • comments
    557
  • views
    154329

CHIP8 "Dry Run"

Sign in to follow this  
TANSTAAFL

194 views

In order for my disassembler to be "good", I need it to, as best it can, identify "code" and "data".

My main thoughts on this are to give the code a "virtual run" in the following manner:

Start at the beginning (0x0200), and mark the instructions as code as I go. I only have to actually respond to GOTO, GOSUB, and SKIPIF.

Responding to a GOTO is simple... just jump the program counter.
Responding to a GOSUB is reasonably simple... simply go to the location and progress until I reach a RETURN.
The slight problem with SKIPIF is that the next instruction might be a GOTO, so I have to be sure to try the next instruction as well as the instruction after that.

So, just thinking and typing here, let's take a look at pong:

0200 VA=02
0202 VB=0C
0204 VC=3F
0206 VD=0C
0208 I=2EA
020A DRAW(VA,VB,6)
020C DRAW(VC,VD,6)
020E VE=00
0210 GOSUB 2D4
0212 V6=03
0214 V8=02
0216 V0=60
0218 DELAY=V0
021A V0=DELAY
021C SKIPIF V0==00
021E GOTO 21A
0220 V7=RND(17)
0222 V7+=08
0224 V9=FF
0226 I=2F0
0228 DRAW(V6,V7,1)
022A I=2EA
022C DRAW(VA,VB,6)
022E DRAW(VC,VD,6)
0230 V0=01
0232 SKIPIF KEYUP(V0)
0234 VB+=FE
0236 V0=04
0238 SKIPIF KEYUP(V0)
023A VB+=02
023C V0=1F
023E VB&=V0
0240 DRAW(VA,VB,6)
0242 V0=0C
0244 SKIPIF KEYUP(V0)
0246 VD+=FE
0248 V0=0D
024A SKIPIF KEYUP(V0)
024C VD+=02
024E V0=1F
0250 VD&=V0
0252 DRAW(VC,VD,6)
0254 I=2F0
0256 DRAW(V6,V7,1)
0258 V6+=V8
025A V7+=V9
025C V0=3F
025E V6&=V0
0260 V1=1F
0262 V7&=V1
0264 SKIPIF V6!=02
0266 GOTO 278
0268 SKIPIF V6!=3F
026A GOTO 282
026C SKIPIF V7!=1F
026E V9=FF
0270 SKIPIF V7!=00
0272 V9=01
0274 DRAW(V6,V7,1)
0276 GOTO 22A
0278 V8=02
027A V3=01
027C V0=V7
027E V0-=VB
0280 GOTO 28A
0282 V8=FE
0284 V3=0A
0286 V0=V7
0288 V0-=VD
028A SKIPIF VF==01
028C GOTO 2A2
028E V1=02
0290 V0-=V1
0292 SKIPIF VF==01
0294 GOTO 2BA
0296 V0-=V1
0298 SKIPIF VF==01
029A GOTO 2C8
029C V0-=V1
029E SKIPIF VF==01
02A0 GOTO 2C2
02A2 V0=20
02A4 SOUND=V0
02A6 GOSUB 2D4
02A8 VE+=V3
02AA GOSUB 2D4
02AC V6=3E
02AE SKIPIF V3==01
02B0 V6=03
02B2 V8=FE
02B4 SKIPIF V3==01
02B6 V8=02
02B8 GOTO 216
02BA V9+=FF
02BC SKIPIF V9!=FE
02BE V9=FF
02C0 GOTO 2C8
02C2 V9+=01
02C4 SKIPIF V9!=02
02C6 V9=01
02C8 V0=04
02CA SOUND=V0
02CC V6+=01
02CE SKIPIF V6!=40
02D0 V6+=FE
02D2 GOTO 26C
02D4 I=2F2
02D6 BCD VE
02D8 LOAD V0,V2
02DA I=FONT(V1)
02DC V4=14
02DE V5=00
02E0 DRAW(V4,V5,5)
02E2 V4+=15
02E4 I=FONT(V2)
02E6 DRAW(V4,V5,5)
02E8 RETURN
02EA V0=V8
02EC V0=V8
02EE V0=V8
02F0 V0=V0
02F2 DATA 0000
02F4 DATA 0000



Starting at 0200, we get all the way up to 210 before having to do anything with GOSUB 2D4. I'm just going to add 2D4 to the GOSUB list and continue on...

At 021C, I reach a SKIPIF, so I'll add 0220 to the SKIPIF list.

And a good thing, too, as 021E contains a GOTO, which returns us to 021A, which is already marked as code, so we'll stop and find something else to look at.

Thus far, 0200 through 021F are marked as code.

So, I'll grab the one value I have in the GOSUB list... 02D4, and I'll progress from there.

I'll mark the code all the way up to 02E8, which is a RETURN, at which point I'll stop again.

Now, 0200..021F and 02D4..02E9 are marked as code.

Which leaves only my SKIPIF at 0220.

I'll be blowing right through the next few SKIPIFs, as they don't have GOTOs after them, until 0266, so add 0268 to the SKIPIF list, and proceed to 0278.

At this point, 0200..0267 and 02D4..02E9 are marked as code.

And so on, until I have marked all of the code, and come to realize that 02EA..02F5 are the only data.

And this is all well and good, and relatively easy to do in my disassembler.

HOWEVER.

There is one opcode that I am concerned with...

GOTO V0+NNN

Add V0 to a number, and jump the program counter there.

Doh.

A nifty opcode, to be sure. Essentially the "ON A GOTO X,Y,Z" of CHIP8.

Kind of throws a monkeywrench into the works of the disassembler. I have to assume that any value of V0 (0..255) MIGHT be valid for this particular GOTO.

Doh Squared.

Anyway, should be fun, right?
Sign in to follow this  


1 Comment


Recommended Comments

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!