@@ -21,7 +21,7 @@ const (
2121//go:embed nestest.nes
2222var bootROM []byte
2323
24- func createSystem (joy * input.Joystick , romData []byte ) (* system.System , error ) {
24+ func create (joy * input.Joystick , romData []byte ) (* system.System , error ) {
2525 rom , err := ines .NewFromBuffer (romData )
2626 if err != nil {
2727 return nil , fmt .Errorf ("failed to load ROM: %v" , err )
@@ -38,10 +38,10 @@ func createSystem(joy *input.Joystick, romData []byte) (*system.System, error) {
3838}
3939
4040func main () {
41- log .SetFlags (0 )
41+ log .SetFlags (0 ) // disable timestamps
4242 joystick := input .NewJoystick ()
4343
44- nes , err := createSystem (joystick , bootROM )
44+ nes , err := create (joystick , bootROM )
4545 if err != nil {
4646 log .Fatalf ("[ERROR] failed to initialize: %v" , err )
4747 }
@@ -53,43 +53,43 @@ func main() {
5353 )
5454
5555 js .Global ().Set ("runFrame" , js .FuncOf (func (this js.Value , args []js.Value ) any {
56+ buttons := args [0 ].Int ()
5657 start := time .Now ()
57- frameBuf := args [0 ]
58- buttons := args [1 ].Int ()
58+ var framePtr uintptr
5959
6060 for {
6161 nes .Tick ()
6262 if nes .FrameReady () {
6363 frame := nes .Frame ()
64- frameBytes := unsafe .Slice ((* byte )(unsafe .Pointer (& frame [0 ])), len (frame )* 4 )
65- js .CopyBytesToJS (frameBuf , frameBytes ) // TODO: can we avoid copying here?
6664 joystick .SetButtons (uint8 (buttons ))
65+ framePtr = uintptr (unsafe .Pointer (& frame [0 ]))
6766 break
6867 }
6968 }
7069
7170 if debugFrameTime {
72- frameTimeSum += time .Since (start )
71+ frameTime := time .Since (start )
72+ frameTimeSum += frameTime
7373 frameCount ++
7474
7575 if frameCount % 120 == 0 {
7676 runtime .ReadMemStats (& mem )
77- elapsed := frameTimeSum / time .Duration (frameCount )
78- log .Printf ("[DEBUG ] frame time: %s , memory: %d" , elapsed , mem .Alloc )
77+ avgFrameTime := frameTimeSum / time .Duration (frameCount )
78+ log .Printf ("[INFO ] frame time: %v , memory: %d" , avgFrameTime , mem .HeapAlloc )
7979 frameTimeSum = 0
8080 frameCount = 0
8181 }
8282 }
8383
84- return nil
84+ return framePtr
8585 }))
8686
8787 js .Global ().Set ("uploadROM" , js .FuncOf (func (this js.Value , args []js.Value ) any {
8888 data := js .Global ().Get ("Uint8Array" ).New (args [0 ])
8989 romData := make ([]byte , data .Length ())
9090 js .CopyBytesToGo (romData , data )
9191
92- nes2 , err := createSystem (joystick , romData )
92+ nes2 , err := create (joystick , romData )
9393 if err != nil {
9494 log .Printf ("[ERROR] failed to initialize: %v" , err )
9595 return false
0 commit comments