Skip to content

Chip-8 on the COSMAC VIP: The General Purpose Timer

This is part of a series of posts analysing the Chip-8 interpreter on the RCA COSMAC VIP computer. These posts may be useful if you are building a Chip-8 interpreter on another platform or if you have an interest in the operation of the COSMAC VIP. For other posts in the series refer to the index or instruction index.

INSTRUCTION GROUP: FX07
Get the current value of the timer into VX.

INSTRUCTION GROUP: FX15
Set the timer with the value of VX.

Chip-8 features a general purpose timer that can be used for timed events or introducing delays, for animation for example. While the timer contains a non-zero value it is decremented 60 times a second (in time with the display update). I analyse the interrupt code that controls the timer in an earlier post.

Note that when the timer reaches zero, nothing further happens. It simply stays at zero until it is set with another non-zero number.

Two instructions are provided for the programmer to make use of the timer, one to set it and one to get the current value. The value to be set must first be loaded into one of the Chip-8 variables. Similarly the value will be read into the one of the Chip-8 variables.

Note that because the variables are 8 bits, this gives a timer range of 0 to 255, which equates to 0 seconds to 4.25 seconds.

These instructions are both part of the FXXX instruction group, so they are subject to an additional stage of decoding. I discuss this in an earlier post.

Both the instruction groups simply transfer one variable into another, so I won’t bother with a flowchart for these, but here is the code for both of them:

Labels
Address (hex)

Code (hex)
Assembly

Comments

FX07:
0107

98
GHI 8

Get current value of timer (R8.1).

0108

56
STR 6

Store it in VX.

0109

D4
SEP 4

Return to the fetch and decode routine.

Labels
Address (hex)

Code (hex)
Assembly

Comments

FX15:
0115

06
LDN 6

Get the value of VX.

0116

B8
PHI 8

Store it in the timer (R8.1).

0117

D4
SEP 4

Return to the fetch and decode routine.

Both of these routines require six machine cycles (27.24 microseconds), but an overhead of four machine cycles (18.16 microseconds) needs to be added to each because of the second stage decoding for these instructions, giving a total execution time of ten machine cycles (45.4 microseconds).

Both of these instructions should be supported by a contemporary interpreter.

Published inProgrammingRetro Computing

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *