#Code Review Request

20 messages · Page 1 of 1 (latest)

pearl orchid
#

Hey all, so I just got finished following this tutorial https://www.jmeiners.com/lc3-vm/ and I ported it over to Go. This is not for any schoolwork or anything like that I just wanted to have some fun over the weekend before I go back to work and write boring code.

I was wondering if you could provide tips on how to make this implementation more idiomatic to Go?

https://github.com/TorNATO-PRO/lc3/blob/main/pkg/cpu/cpu.go

GitHub

A pure Go implementation of the LC3 virtual CPU. Contribute to TorNATO-PRO/lc3 development by creating an account on GitHub.

glad palm
#

Welcome to the world of emulator developmment 🙂

#

I'm still new to Go. But some changes I would recommend.

#

Remove blanks between function and testing for error, this kind of shows they are related.

#

The other is incrProgramCounter can be done as c.register[registers.RPC] += 1

pearl orchid
#

great, thanks!

#

yeah i didn't realize how fun emulators were lol

glad palm
#

You can check out my emulators

#

You might also join the emudev server.

pearl orchid
glad palm
#

S370 is a go emulator... but not finished

pearl orchid
#

what are your thoughts on Go vs C for emulator development?

glad palm
#

I am moving to C++, but I really seem to like Go... but haven't gotten far enough in S370 to truely compare.

gritty cliff
#

nice job on no dep, though having go 1.22.2 in your go mod is kinda bad

either put go 1.22 ie the language version
or go 1.22.5 the current patch level

[][math.MaxUint16 + 1]uint16

that looks odd... why +1 and why so big arrays?

also math.MaxUint16 + 1 repeated all over is bad

const pageSize = math.MaxUint16 + 1

(but then again the +1 is suspicious)

using m for such key thing makes it hard to spot (in readImage) use something longer,
also why returning an empty (large) one instead of nil for all error case

I think the +1 could have been the length but... doesn't seem it is

#

get rid of pkg/ dir, it's useless

#

oh I get the +1, you basically want 2^16 / 1<<16

#

but don't pepper that all over, it's terrible

#
const PageSize = 1<<16 // 64k max memory/pages
vague patrol
#

I'd go a step further and do

type Memory [1<<16]uint16

and similarly for the registers

#

I see that you are setting the cpu's memory inside a member variable of the cpu struct when you call Run on it. That implies state so Run is not thread safe. I don't know if you care or not, just pointing it out.