Skip to content

Instantly share code, notes, and snippets.

@kencoba
Created April 1, 2014 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kencoba/9907682 to your computer and use it in GitHub Desktop.
Save kencoba/9907682 to your computer and use it in GitHub Desktop.
Simple CPU Simulator with Excel VBA ref: http://qiita.com/kencoba/items/25083937af095358be79
Option Explicit
Private cPC As Range ' ProgramCounter
Private cCR As Range ' CommandRegister
Private cAR As Range ' AddressRegister
Private cAA As Range ' AccumulatorA
Private cAB As Range ' AccumulatorB
Sub Init()
Set cPC = Worksheets(1).Cells(2, 4)
Set cCR = Worksheets(1).Cells(2, 5)
Set cAR = Worksheets(1).Cells(2, 6)
Set cAA = Worksheets(1).Cells(2, 7)
Set cAB = Worksheets(1).Cells(2, 8)
End Sub
Sub ExecuteStep()
Call Init
Call ReadOneCommand
Call IncrementProgramCounter
Call ExecuteOneCommand
End Sub
Sub ReadOneCommand()
cCR.Value2 = GetCP
cAR.Value2 = GetAP
End Sub
Function GetCP()
GetCP = Worksheets(1).Cells(cPC.Value2 - 98, 2)
End Function
Function GetAP()
GetAP = Worksheets(1).Cells(cPC.Value2 - 98, 3)
End Function
Sub IncrementProgramCounter()
cPC.Value2 = cPC.Value2 + 1
End Sub
Sub ExecuteOneCommand()
Select Case cCR.Value2
Case "読み込む"
Call CmdLoad
Case "加算する"
Call CmdAdd
Case "書き込み"
Call CmdStore
Case "終了"
Call CmdExit
End Select
End Sub
Sub CmdLoad()
cAA.Value2 = Worksheets(1).Cells(cAR.Value2 - 98, 2)
End Sub
Sub CmdAdd()
cAB.Value2 = Worksheets(1).Cells(cAR.Value2 - 98, 2)
cAA.Value2 = cAA.Value2 + cAB.Value2
End Sub
Sub CmdStore()
Worksheets(1).Cells(cAR.Value2 - 98, 2).Value2 = cAA.Value2
End Sub
Sub CmdExit()
MsgBox ("Exit")
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment