Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit73351b1

Browse files
committed
Minor comment & whitespace tweaks.
1 parent8413400 commit73351b1

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

‎main.go‎

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"math/rand"
2323
"os"
2424
"os/exec"
25+
"path/filepath"
2526
"regexp"
2627
"strconv"
2728
"time"
@@ -205,7 +206,7 @@ func NewCPU() *CPU {
205206
}
206207

207208
// Reset sets the CPU into a known-good state, by setting the IP to zero,
208-
// and emptying all registers (i.e. setting them to zero too.)
209+
// and emptying all registers (i.e. setting them to zero too).
209210
func (c*CPU)Reset() {
210211
fori:=0;i<16;i++ {
211212
c.regs[i].SetInt(0)
@@ -241,22 +242,23 @@ func (c *CPU) Load(path string) {
241242
// Read a string from the IP position
242243
// Strings are prefixed by their lengths (two-bytes).
243244
func (c*CPU)readString()string {
244-
// Read the length of the string
245+
// Read the length of the string we expect
245246
len:=c.read2Val()
246247

247-
debugPrintf("string length(%04X);\n",len)
248-
249248
// Now build up the body of the string
250249
s:=""
251250
fori:=0;i<len;i++ {
252251
s+=string(c.mem[c.ip+i])
253252
}
254253

254+
// Jump the IP over the length of the string.
255255
c.ip+= (len)
256256
returns
257257
}
258258

259259
// Read a two-byte number from the current IP.
260+
// i.e This reads two bytes and returns a 16-bit value to the caller,
261+
// skipping over both bytes in the IP.
260262
func (c*CPU)read2Val()int {
261263
l:=int(c.mem[c.ip])
262264
c.ip+=1
@@ -269,19 +271,17 @@ func (c *CPU) read2Val() int {
269271

270272
// Run launches our intepreter.
271273
func (c*CPU)Run() {
272-
273-
c.ip=0
274274
run:=true
275275
forrun {
276276

277277
instruction:=c.mem[c.ip]
278-
279278
debugPrintf("About to execute instruction %02X\n",instruction)
280279

281280
switchinstruction {
282281
case0x00:
283282
debugPrintf("EXIT\n")
284283
run=false
284+
285285
case0x01:
286286
debugPrintf("INT_STORE\n")
287287

@@ -293,6 +293,7 @@ func (c *CPU) Run() {
293293

294294
debugPrintf("\tSet register %02X to %04X\n",reg,val)
295295
c.regs[reg].SetInt(val)
296+
296297
case0x02:
297298
debugPrintf("INT_PRINT\n")
298299
// register
@@ -301,6 +302,7 @@ func (c *CPU) Run() {
301302

302303
fmt.Printf("%d",c.regs[reg].GetInt())
303304
c.ip+=1
305+
304306
case0x03:
305307
debugPrintf("INT_TOSTRING\n")
306308
// register
@@ -315,6 +317,7 @@ func (c *CPU) Run() {
315317

316318
// next instruction
317319
c.ip+=1
320+
318321
case0x04:
319322
debugPrintf("INT_RANDOM\n")
320323
// register
@@ -328,18 +331,21 @@ func (c *CPU) Run() {
328331
// New random number
329332
c.regs[reg].SetInt(r1.Intn(0xffff))
330333
c.ip+=1
334+
331335
case0x10:
332336
debugPrintf("JUMP\n")
333337
c.ip+=1
334338
addr:=c.read2Val()
335339
c.ip=addr
340+
336341
case0x11:
337342
debugPrintf("JUMP_Z\n")
338343
c.ip+=1
339344
addr:=c.read2Val()
340345
ifc.flags.z {
341346
c.ip=addr
342347
}
348+
343349
case0x12:
344350
debugPrintf("JUMP_NZ\n")
345351
c.ip+=1
@@ -377,6 +383,7 @@ func (c *CPU) Run() {
377383
a_val:=c.regs[a].GetInt()
378384
b_val:=c.regs[b].GetInt()
379385
c.regs[res].SetInt(a_val+b_val)
386+
380387
case0x22:
381388
debugPrintf("SUB\n")
382389
c.ip+=1
@@ -454,6 +461,7 @@ func (c *CPU) Run() {
454461
c.regs[reg].SetInt(c.regs[reg].GetInt()-1)
455462
// bump past that
456463
c.ip+=1
464+
457465
case0x27:
458466
debugPrintf("AND\n")
459467
c.ip+=1
@@ -558,6 +566,7 @@ func (c *CPU) Run() {
558566
iflen(err.String())>0 {
559567
fmt.Printf("%s",err.String())
560568
}
569+
561570
case0x34:
562571
debugPrintf("STRING_TOINT\n")
563572

@@ -599,6 +608,7 @@ func (c *CPU) Run() {
599608
c.flags.z=true
600609
}
601610
}
611+
602612
case0x41:
603613
debugPrintf("CMP_IMMEDIATE\n")
604614
// register
@@ -612,6 +622,7 @@ func (c *CPU) Run() {
612622
}else {
613623
c.flags.z=false
614624
}
625+
615626
case0x42:
616627
debugPrintf("CMP_STR\n")
617628
// register
@@ -627,6 +638,7 @@ func (c *CPU) Run() {
627638
}else {
628639
c.flags.z=false
629640
}
641+
630642
case0x43:
631643
debugPrintf("IS_STRING\n")
632644
// register
@@ -639,6 +651,7 @@ func (c *CPU) Run() {
639651
}else {
640652
c.flags.z=false
641653
}
654+
642655
case0x44:
643656
debugPrintf("IS_INT\n")
644657
// register
@@ -655,6 +668,7 @@ func (c *CPU) Run() {
655668
case0x50:
656669
debugPrintf("NOP\n")
657670
c.ip+=1
671+
658672
case0x51:
659673
debugPrintf("STORE\n")
660674

@@ -685,6 +699,7 @@ func (c *CPU) Run() {
685699
// store the contents of the given address
686700
c.regs[result].SetInt(int(c.mem[addr]))
687701
c.ip+=1
702+
688703
case0x61:
689704
debugPrintf("POKE\n")
690705

@@ -703,6 +718,7 @@ func (c *CPU) Run() {
703718

704719
debugPrintf("Writing %02X to %04X\n",val,addr)
705720
c.mem[addr]=byte(val)
721+
706722
case0x62:
707723
debugPrintf("MEMCPY\n")
708724

@@ -737,6 +753,7 @@ func (c *CPU) Run() {
737753
src_addr+=1
738754
i+=1
739755
}
756+
740757
case0x70:
741758
debugPrintf("PUSH\n")
742759

@@ -778,6 +795,7 @@ func (c *CPU) Run() {
778795

779796
// jump
780797
c.ip=addr
798+
781799
case0x73:
782800
debugPrintf("CALL\n")
783801
c.ip+=1
@@ -789,6 +807,7 @@ func (c *CPU) Run() {
789807

790808
// jump to the call address
791809
c.ip=addr
810+
792811
default:
793812
fmt.Printf("Unrecognized/Unimplemented opcode %02X at IP %04X\n",instruction,c.ip)
794813
os.Exit(1)
@@ -803,12 +822,14 @@ func (c *CPU) Run() {
803822

804823
// Main driver
805824
funcmain() {
825+
iflen(os.Args)<2 {
826+
fmt.Printf("Usage %s file1.raw file2.raw .. fileN.raw\n",
827+
filepath.Base(os.Args[0]))
828+
}
806829
for_,ent:=rangeos.Args[1:] {
807-
808-
fmt.Printf("Loading file %s\n",ent)
830+
fmt.Printf("Loading file: %s\n",ent)
809831
c:=NewCPU()
810832
c.Load(ent)
811-
812833
c.Run()
813834
}
814835

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp