hi, i am trying to design chip 8 emulator on visual basic, i have found source code of it but the problem is i am not getting the idea how the code works, i understand some part of it.. please help me.. here is the full source code..
Dim ram(8192) As Byte
Dim stack(256) As Integer
Dim v(256), keydef(16) As Byte
Dim pc, Index, key_choice As Integer
Dim opcode1, opcode2, opcode3, opcode4, sp, key_up, key_down, delaytimer, soundtimer, x, y, i, j, k, c1, c2, c3 As Byte
Dim dbg, Pause, ExtendedScreen As Boolean
Private Declare Function Beep Lib "kernel32" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long
Private Sub About_Click()
msg = "VB Chip8 - The first Visual Basic implementation" + vbCrLf
msg = msg + "of a Chip8 Virtual Machine." + vbCrLf
msg = msg + "Thanks to unknown autor of original C source code." + vbCrLf
msg = msg + "This release is beta and won't run many games, yet." + vbCrLf
msg = msg + "Made by VMA in 2004. This is freeware!" + vbCrLf
MsgBox msg, vbOKOnly, "About VB Chip-8"
End Sub
//it indicates to debug or not
Private Sub Check1_Click()
If Check1.Value = 1 Then
Check1.Value = 1
Else
dbg = False
End If
End Sub
Private Sub Command1_Click(Index As Integer)
key_choice = 0
Label1.Caption = "Waiting for key..."
While key_choice = 0
DoEvents
Wend
keydef(Index + 1) = key_choice
Command1(Index).Caption = Chr(key_choice)
Label1.Caption = ""
End Sub
Private Sub Exit_Click()
End
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
For a = 1 To 16
If KeyCode = keydef(a) Then key_down = a
Next a
key_choice = KeyCode
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
key_down = 0
End Sub
// I dont Know what is here happening????????
Private Sub Form_Load()
keydef(1) = 103
keydef(2) = 104
keydef(3) = 105
keydef(4) = 100
keydef(5) = 101
keydef(6) = 102
keydef(7) = 97
keydef(8) = 98
keydef(9) = 99
keydef(10) = 96
keydef(11) = 111
keydef(12) = 106
keydef(13) = 107
keydef(14) = 13
keydef(15) = 103
keydef(16) = 110
c1 = 0
c2 = 200
c3 = 255
KeyPreview = True
Randomize
Timer1.Enabled = True
Check1.Value = 1
End Sub
Private Sub Form_Unload(Cancel As Integer)
End
End Sub
//this punction open the dialouge box to load the ROM file
Private Sub OpenROM_Click()
Dim b As Byte
CommonDialog1.ShowOpen
nome = CommonDialog1.FileName
If nome <> "" Then
Open nome For Binary As #1
While EOF(1) = False
a = a + 1
Get #1, a, b
ram(&H200 - 1 + a) = b
Wend
Close #1
End If
Reset_Click
End Sub
Private Sub pausex_Click()
If Pause = True Then
Pause = False
Else
Pause = True
End If
End Sub
// I dont Know what is here happening????????
Private Sub Reset_Click()
Picture1.Cls
pc = &H200
ram(&H1000) = 96
ram(&H1001) = 144
ram(&H1002) = 144
ram(&H1003) = 144
ram(&H1004) = 96
ram(&H1005) = 96
ram(&H1006) = 224
ram(&H1007) = 96
ram(&H1008) = 96
ram(&H1009) = 240
ram(&H100A) = 96
ram(&H100B) = 144
ram(&H100C) = 32
ram(&H100D) = 64
ram(&H100E) = 240
ram(&H100F) = 224
ram(&H1010) = 16
ram(&H1011) = 96
ram(&H1012) = 16
ram(&H1013) = 224
ram(&H1014) = 160
ram(&H1015) = 160
ram(&H1016) = 240
ram(&H1017) = 32
ram(&H1018) = 32
ram(&H1019) = 240
ram(&H101A) = 128
ram(&H101B) = 240
ram(&H101C) = 16
ram(&H101D) = 240
ram(&H101E) = 240
ram(&H101F) = 128
ram(&H1020) = 240
ram(&H1021) = 144
ram(&H1022) = 240
ram(&H1023) = 240
ram(&H1024) = 16
ram(&H1025) = 32
ram(&H1026) = 64
ram(&H1027) = 128
ram(&H1028) = 96
ram(&H1029) = 144
ram(&H102A) = 96
ram(&H102B) = 144
ram(&H102C) = 96
ram(&H102D) = 240
ram(&H102E) = 144
ram(&H102F) = 240
ram(&H1030) = 16
ram(&H1031) = 240
ram(&H1032) = 96
ram(&H1033) = 144
ram(&H1034) = 240
ram(&H1035) = 144
ram(&H1036) = 144
ram(&H1037) = 224
ram(&H1038) = 144
ram(&H1039) = 224
ram(&H103A) = 144
ram(&H103B) = 224
ram(&H103C) = 112
ram(&H103D) = 128
ram(&H103E) = 128
ram(&H103F) = 128
ram(&H1040) = 112
ram(&H1041) = 224
ram(&H1042) = 144
ram(&H1043) = 144
ram(&H1044) = 144
ram(&H1045) = 224
ram(&H1046) = 240
ram(&H1047) = 128
ram(&H1048) = 224
ram(&H1049) = 128
ram(&H104A) = 240
ram(&H104B) = 240
ram(&H104C) = 128
ram(&H104D) = 224
ram(&H104E) = 128
ram(&H104F) = 128
ExtendedScreen = False
start_cpu
End Sub
Private Sub timer1_Timer()
If delaytimer > 0 Then delaytimer = delaytimer - 1
If soundtimer > 0 Then
Beep 500, soundtimer * 5
soundtimer = 0
End If
End Sub
Sub start_cpu()
10:
If delaytimer > 1 Then delaytimer = delaytimer - 1
If Pause = False Then
Interpret
End If
For a = 1 To HScroll1.Value
DoEvents
Next a
GoTo 10
End Sub
// I dont Know what is here happening????????
Sub getopcode()
opcode1 = (ram(pc) And &HF0) / 16
opcode2 = ram(pc) And &HF
opcode3 = (ram(pc + 1) And &HF0) / 16
opcode4 = ram(pc + 1) And &HF
pc = pc + 2
End Sub
// I dont Know what is here happening???????? and this is the most important part which i have to understand pleasehelp me in this thread to understand all this
Sub Interpret()
getopcode
If Check1.Value = 1 Then Text1.Text = Text1.Text + Hex(pc - 2) + ": " + Hex(opcode1) + " " + Hex(opcode2) + " " + Hex(opcode3) + " " + Hex(opcode4) + " -> "
Select Case opcode1
Case &H0
Select Case (opcode3)
Case &HC
If Check1.Value = 1 Then Text1.Text = Text1.Text + "ScrollDown"
ScrollDown
Case &HE
Select Case (opcode4)
Case &H0:
If Check1.Value = 1 Then Text1.Text = Text1.Text + "ClearScreen"
ClearScreen
Case &HE:
If Check1.Value = 1 Then Text1.Text = Text1.Text + "ReturnFromSub"
ReturnFromSub
End Select
Case &HF
Select Case (opcode4)
Case &HB
If Check1.Value = 1 Then Text1.Text = Text1.Text + "ScrollRight"
ScrollRight
Case &HC
If Check1.Value = 1 Then Text1.Text = Text1.Text + "ScrollLeft"
ScrollLeft
Case &HE
If Check1.Value = 1 Then Text1.Text = Text1.Text + "DisableExtendedScreen"
DisableExtendedScreen
Case &HF
If Check1.Value = 1 Then Text1.Text = Text1.Text + "EnableExtendedScreen"
EnableExtendedScreen
End Select
End Select
Case &H1
If Check1.Value = 1 Then Text1.Text = Text1.Text + "JumpToAddress"
JumpToAddress
Case &H2
If Check1.Value = 1 Then Text1.Text = Text1.Text + "JumpToSub"
JumpToSub
Case &H3
If Check1.Value = 1 Then Text1.Text = Text1.Text + "SkipIfRegEqual"
SkipIfRegEqual
Case &H4:
If Check1.Value = 1 Then Text1.Text = Text1.Text + "SkipIfRegNotEqual"
SkipIfRegNotEqual
Case &H5
If Check1.Value = 1 Then Text1.Text = Text1.Text + "SkipIfRegEqualReg"
SkipIfRegEqualReg
Case &H6
If Check1.Value = 1 Then Text1.Text = Text1.Text + "LoadRegWithConstant"
LoadRegWithConstant
Case &H7
If Check1.Value = 1 Then Text1.Text = Text1.Text + "AddConstantToReg"
AddConstantToReg
Case &H8
Select Case (opcode4)
Case &H0
If Check1.Value = 1 Then Text1.Text = Text1.Text + "LoadRegWithReg"
LoadRegWithReg
Case &H1
If Check1.Value = 1 Then Text1.Text = Text1.Text + "OrRegWIthReg"
OrRegWithReg
Case &H2
If Check1.Value = 1 Then Text1.Text = Text1.Text + "AndRegWithReg"
AndRegWithReg
Case &H3
If Check1.Value = 1 Then Text1.Text = Text1.Text + "XorRegWithReg"
XorRegWithReg
Case &H4
If Check1.Value = 1 Then Text1.Text = Text1.Text + "AddRegToReg"
AddRegToReg
Case &H5
If Check1.Value = 1 Then Text1.Text = Text1.Text + "SubRegFromReg"
SubRegFromReg
Case &H6
If Check1.Value = 1 Then Text1.Text = Text1.Text + "ShiftRegRight"
ShiftRegRight
Case &H7
If Check1.Value = 1 Then Text1.Text = Text1.Text + "RSubRegFromReg"
RSubRegFromReg
Case &HE
If Check1.Value = 1 Then Text1.Text = Text1.Text + "ShiftRegLeft"
ShiftRegLeft
End Select
Case &H9
If Check1.Value = 1 Then Text1.Text = Text1.Text + "SkipIfRegNotEqualReg"
SkipIfRegNotEqualReg
Case &HA
If Check1.Value = 1 Then Text1.Text = Text1.Text + "LoadIndexWithConstant"
LoadIndexWithConstant
Case &HB
If Check1.Value = 1 Then Text1.Text = Text1.Text + "JumpToAddressPlusReg0"
JumpToAddressPlusReg0
Case &HC
If Check1.Value = 1 Then Text1.Text = Text1.Text + "CreateRandomNumber"
CreateRandomNumber
Case &HD
If Check1.Value = 1 Then Text1.Text = Text1.Text + "DrawSprite"
DrawSprite
Case &HE
Select Case (opcode3)
Case &H9
If Check1.Value = 1 Then Text1.Text = Text1.Text + "SkipIfKeyPressed"
SkipIfKeyPressed
Case &HA
If Check1.Value = 1 Then Text1.Text = Text1.Text + "SkipIfKeyNotPressed"
SkipIfKeyNotPressed
End Select
Case &HF
Select Case (opcode3)
Case &H0
Select Case (opcode4)
Case &H7
If Check1.Value = 1 Then Text1.Text = Text1.Text + "GetDelayTimerIntoReg"
GetDelayTimerIntoReg
Case &HA
If Check1.Value = 1 Then Text1.Text = Text1.Text + "WaitForKeyPress"
WaitForKeyPress
End Select
Case &H1
Select Case (opcode4)
Case &H5
If Check1.Value = 1 Then Text1.Text = Text1.Text + "SetDelayTimer"
SetDelayTimer
Case &H8
If Check1.Value = 1 Then Text1.Text = Text1.Text + "SetSoundTimer"
SetSoundTimer
Case &HE
If Check1.Value = 1 Then Text1.Text = Text1.Text + "AddRegToIndex"
AddRegToIndex
End Select
Case &H2
If Check1.Value = 1 Then Text1.Text = Text1.Text + "PointIndexToFont"
PointIndexToFont
Case &H3
If Check1.Value = 1 Then Text1.Text = Text1.Text + "StoreBCD"
StoreBCD
Case &H5
If Check1.Value = 1 Then Text1.Text = Text1.Text + "StoreRegsAtIndex"
StoreRegsAtIndex
Case &H6
If Check1.Value = 1 Then Text1.Text = Text1.Text + "LoadRegsFromIndex"
LoadRegsFromIndex
End Select
End Select
If Check1.Value = 1 Then
Text1.Text = Text1.Text + vbCrLf
Text1.SelLength = Len(Text1.Text)
End If
End Sub
Sub ScrollDown()
For z = 0 To opcode4
Picture2.PaintPicture Picture1.Image, 0, 2, , , 0, 0
Picture2.Line (0, 0)-(256, 1), RGB(0, 0, 0), BF
Picture1.PaintPicture Picture2.Image, 0, 0
Next z
End Sub
Sub ScrollRight()
For z = 0 To 3
Picture2.PaintPicture Picture1.Image, 2, 0, , , 0, 0
Picture2.Line (0, 0)-(1, 128), RGB(0, 0, 0), BF
Picture1.PaintPicture Picture2.Image, 0, 0
Next z
End Sub
Sub ScrollLeft()
For z = 0 To 3
Picture2.PaintPicture Picture1.Image, 0, 0, , , 2, 0
Picture2.Line (255, 0)-(256, 128), RGB(0, 0, 0), BF
Picture1.PaintPicture Picture2.Image, 0, 0
Next z
End Sub
Sub DisableExtendedScreen()
ExtendedScreen = False
End Sub
Sub EnableExtendedScreen()
ExtendedScreen = True
End Sub
Sub ClearScreen()
DoEvents
Form1.Picture1.Cls
End Sub
Sub ReturnFromSub()
DoEvents
sp = sp - 1
pc = stack(sp)
End Sub
Sub JumpToAddress()
DoEvents
opcode2 = opcode2 * 256
opcode3 = opcode3 * 16
pc = opcode2 + opcode3 + opcode4
End Sub
Sub JumpToSub()
DoEvents
stack(sp) = pc
sp = sp + 1
opcode2 = opcode2 * 256
opcode3 = opcode3 * 16
pc = opcode2 + opcode3 + opcode4
End Sub
Sub SkipIfRegEqual()
DoEvents
opcode3 = opcode3 * 16
If v(opcode2) = (opcode3 + opcode4) Then pc = pc + 2
End Sub
Sub SkipIfRegNotEqual()
DoEvents
opcode3 = opcode3 * 16
If v(opcode2) <> (opcode3 + opcode4) Then pc = pc + 2
End Sub
Sub SkipIfRegEqualReg()
DoEvents
If v(opcode2) = v(opcode3) Then pc = pc + 2
End Sub
Sub SkipIfRegNotEqualReg()
DoEvents
If v(opcode2) <> v(opcode3) Then pc = pc + 2
End Sub
Sub LoadRegWithConstant()
DoEvents
opcode3 = opcode3 * 16
v(opcode2) = (opcode3 + opcode4)
End Sub
Sub AddConstantToReg()
DoEvents
opcode3 = opcode3 * 16
x = v(opcode2) + (opcode3 + opcode4)
If x > 255 Then x = x - 256
v(opcode2) = x
End Sub
Sub LoadRegWithReg()
DoEvents
v(opcode2) = v(opcode3)
End Sub
Sub OrRegWithReg()
DoEvents
v(opcode2) = v(opcode2) Or v(opcode3)
End Sub
Sub AndRegWithReg()
DoEvents
v(opcode2) = v(opcode2) And v(opcode3)
End Sub
Sub XorRegWithReg()
DoEvents
v(opcode2) = v(opcode2) Xor v(opcode3)
End Sub
Sub AddRegToReg()
DoEvents
Dim x As Integer
If v(opcode2) And v(opcode3) Then
v(&HF) = 1
Else
v(&HF) = 0
End If
x = v(opcode2)
x = x + v(opcode3)
If x > 255 Then x = x - 256
v(opcode2) = x
End Sub
Sub SubRegFromReg()
DoEvents
Dim x As Single
If v(opcode2) >= v(opcode3) Then
v(&HF) = 1
Else
v(&HF) = 0
End If
x = v(opcode2)
x = x - v(opcode3)
If x < 0 Then x = x + 256
v(opcode2) = x
End Sub
Sub ShiftRegRight()
DoEvents
If v(opcode2) And 1 Then
v(&HF) = 1
Else
v(&HF) = 0
End If
v(opcode2) = v(opcode2) / 2
End Sub
Sub RSubRegFromReg()
DoEvents
Dim x As Single
If v(opcode2) >= v(opcode3) Then
v(&HF) = 1
Else
v(&HF) = 0
End If
x = v(opcode3)
x = x - v(opcode2)
If x < 0 Then x = x + 256
v(opcode2) = x
End Sub
Sub ShiftRegLeft()
DoEvents
If v(opcode2) And 128 Then
v(&HF) = 1
Else
v(&HF) = 0
End If
x = v(opcode2) * 2
If x > 255 Then x = x - 256
v(opcode2) = x
End Sub
Sub LoadIndexWithConstant()
DoEvents
opcode2 = opcode2 * 256
opcode3 = opcode3 * 16
Index = opcode2 + opcode3 + opcode4
End Sub
Sub JumpToAddressPlusReg0()
DoEvents
opcode2 = opcode2 * 256
opcode3 = opcode3 * 16
pc = v(0) + opcode2 + opcode3 + opcode4
End Sub
Sub CreateRandomNumber()
DoEvents
opcode3 = opcode3 * 16
v(opcode2) = Int(Rnd * 256) And (opcode3 + opcode4)
End Sub
Sub DrawSprite()
v(&HF) = 0
If ExtendedScreen = False Then
x = v(opcode2) * 4
y = v(opcode3) * 4
For j = 0 To opcode4 - 1
datax = ram(Index + j)
i = 0
If datax >= 128 Then
DrawScreen
datax = datax - 128
End If
i = i + 1
If datax >= 64 Then
DrawScreen
datax = datax - 64
End If
i = i + 1
If datax >= 32 Then
DrawScreen
datax = datax - 32
End If
i = i + 1
If datax >= 16 Then
DrawScreen
datax = datax - 16
End If
i = i + 1
If datax >= 8 Then
DrawScreen
datax = datax - 8
End If
i = i + 1
If datax >= 4 Then
DrawScreen
datax = datax - 4
End If
i = i + 1
If datax >= 2 Then
DrawScreen
datax = datax - 2
End If
i = i + 1
If datax >= 1 Then
DrawScreen
datax = datax - 1
End If
Next j
Else
If opcode4 = 0 Then
x = v(opcode2) * 2
y = v(opcode3) * 2
For k = 0 To 1
For j = 0 To 15
i = 0
datax = ram(Index + j * 2 + k)
If datax >= 128 Then
DrawSChipSprite
datax = datax - 128
End If
i = i + 1
If datax >= 64 Then
DrawSChipSprite
datax = datax - 64
End If
i = i + 1
If datax >= 32 Then
DrawSChipSprite
datax = datax - 32
End If
i = i + 1
If datax >= 16 Then
DrawSChipSprite
datax = datax - 16
End If
i = i + 1
If datax >= 8 Then
DrawSChipSprite
datax = datax - 8
End If
i = i + 1
If datax >= 4 Then
DrawSChipSprite
datax = datax - 4
End If
i = i + 1
If datax >= 2 Then
DrawSChipSprite
datax = datax - 2
End If
i = i + 1
If datax >= 1 Then
DrawSChipSprite
datax = datax - 1
End If
i = i + 1
Next j
Next k
Else
x = v(opcode2) * 2
y = v(opcode3) * 2
For j = 0 To opcode4 - 1
i = 0
datax = ram(Index + j)
If datax >= 128 Then
DrawScreen_SChip
datax = datax - 128
End If
i = i + 1
If datax >= 64 Then
DrawScreen_SChip
datax = datax - 64
End If
i = i + 1
If datax >= 32 Then
DrawScreen_SChip
datax = datax - 32
End If
i = i + 1
If datax >= 16 Then
DrawScreen_SChip
datax = datax - 16
End If
i = i + 1
If datax >= 8 Then
DrawScreen_SChip
datax = datax - 8
End If
i = i + 1
If datax >= 4 Then
DrawScreen_SChip
datax = datax - 4
End If
i = i + 1
If datax >= 2 Then
DrawScreen_SChip
datax = datax - 2
End If
i = i + 1
If datax >= 1 Then
DrawScreen_SChip
datax = datax - 1
End If
i = i + 1
Next j
End If
End If
End Sub
Sub DrawScreen()
If Picture1.Point(1 + x + (i * 4), y + (j * 4)) = RGB(c1, c2, c3) Then
v(&HF) = 1
Picture1.Line (1 + x + (i * 4), y + (j * 4))-(1 + x + (i * 4) + 3, y + (j * 4) + 3), RGB(0, 0, 0), BF
Else
Picture1.Line (1 + x + (i * 4), y + (j * 4))-(1 + x + (i * 4) + 3, y + (j * 4) + 3), RGB(c1, c2, c3), BF
End If
End Sub
Sub DrawScreen_SChip()
If Picture1.Point(1 + x + (i * 2), y + (j * 2)) = RGB(c1, c2, c3) Then
v(&HF) = 1
Picture1.Line (1 + x + (i * 2), y + (j * 2))-(1 + x + (i * 2) + 1, y + (j * 2) + 1), RGB(0, 0, 0), BF
Else
Picture1.Line (1 + x + (i * 2), y + (j * 2))-(1 + x + (i * 2) + 1, y + (j * 2) + 1), RGB(c1, c2, c3), BF
End If
End Sub
Sub DrawSChipSprite()
If Picture1.Point(1 + x + k * 16 + (i * 2), y + (j * 2)) = RGB(c1, c2, c3) Then
v(&HF) = 1
Picture1.Line (1 + x + k * 16 + (i * 2), y + (j * 2))-(1 + x + k * 16 + (i * 2) + 1, y + (j * 2) + 1), RGB(0, 0, 0), BF
Else
Picture1.Line (1 + x + k * 16 + (i * 2), y + (j * 2))-(1 + x + k * 16 + (i * 2) + 1, y + (j * 2) + 1), RGB(c1, c2, c3), BF
End If
End Sub
Sub SkipIfKeyPressed()
DoEvents
If key_down = v(opcode2) Then pc = pc + 2
End Sub
Sub SkipIfKeyNotPressed()
DoEvents
If key_down <> v(opcode2) Then pc = pc + 2
End Sub
Sub WaitForKeyPress()
DoEvents
key_down = 0
Label1.Caption = "Waiting for key..."
While key_down = 0
DoEvents
Wend
v(opcode2) = key_down
Label1.Caption = ""
End Sub
Sub SetDelayTimer()
DoEvents
delaytimer = v(opcode2)
End Sub
Sub GetDelayTimerIntoReg()
DoEvents
v(opcode2) = delaytimer
End Sub
Sub SetSoundTimer()
DoEvents
soundtimer = v(opcode2)
End Sub
Sub AddRegToIndex()
DoEvents
Index = Index + v(opcode2)
End Sub
Sub PointIndexToFont()
DoEvents
Index = &H1000 + (v(opcode2) * &H5)
End Sub
Sub StoreBCD()
DoEvents
num = v(opcode2)
For i = 3 To 1 Step -1
ram(Index + (i - 1)) = num Mod 10
num = num / 10
Next i
End Sub
Sub StoreRegsAtIndex()
DoEvents
For i = 0 To opcode2
ram(Index + i) = v(i)
Next i
End Sub
Sub LoadRegsFromIndex()
DoEvents
For i = 0 To opcode2
v(i) = ram(Index + i)
Next i
End Sub
i want to understand three main function..
1. interupt()
2. getopcode()
3. Reset_Click()