Example #1
0
// Function main is the main entry point in the program.
func main() {
	flag.Parse()

	if flag.NArg() < 1 {
		fmt.Fprintf(os.Stderr, "Not enough arguments\nusage: %s file.hex\n", os.Args[0])
		os.Exit(2)
	}

	f, err := os.Open(flag.Arg(0))
	die(err)
	defer f.Close()

	reader := bufio.NewReader(f)
	ix, err := ihex.ReadIHex(reader)
	die(err)
	program := ix.ExtractDataToEnd(0)

	em := k270emlib.NewEmulator()
	em.SetGetKey(getKey)
	em.SetMemory(program)

	if *trace {
		em.SetTraceFile(os.Stdout)
	}

	em.Run()
	fmt.Println("")
	em.DumpState()

	if *screenDump != "" {
		sf, err := os.Create(*screenDump)
		die(err)
		defer sf.Close()

		vmem := em.GetVideoMemory()

		for y := 0; y < k270emlib.VMEM_HEIGHT; y++ {
			for x := 0; x < k270emlib.VMEM_WIDTH; x++ {
				ch := vmem[(y*k270emlib.VMEM_WIDTH)+x]

				if ch.Char == 0 {
					_, err := sf.Write([]byte{' '})
					die(err)
				} else {
					_, err := sf.Write([]byte{ch.Char})
					die(err)
				}
			}

			_, err := sf.Write([]byte{'\n'})
			die(err)
		}

		fmt.Printf("Wrote '%s'\n", *screenDump)
	}
}
Example #2
0
File: dcpuem.go Project: iand/go
// Function loadHex loads all the bytes from the file named `filename` in Intel Hex format and
// returns it.
func loadHex(filename string) (program []byte) {
	f, err := os.Open(filename)
	die(err)
	defer f.Close()

	reader := bufio.NewReader(f)
	ix, err := ihex.ReadIHex(reader)
	die(err)
	program = ix.ExtractDataToEnd(0)

	return program
}
Example #3
0
func LoadHex(filename string) (data []byte, err error) {
	f, err := os.Open(filename)
	if err != nil {
		return nil, err
	}
	defer f.Close()

	reader := bufio.NewReader(f)
	ix, err := ihex.ReadIHex(reader)
	if err != nil {
		return nil, err
	}

	data = ix.ExtractDataToEnd(0)
	return data, nil
}
Example #4
0
File: k680em.go Project: iand/go
func main() {
	/*
	   defer func() {
	       if x := recover(); x != nil {
	           fmt.Fprintf(os.Stderr, "Runtime error: %s\n", x)
	           os.Exit(1)
	       }
	   }()
	*/

	flag.Parse()

	if flag.NArg() < 1 {
		fmt.Fprintf(os.Stderr, "Not enough arguments\nusage: %s file.hex\n", os.Args[0])
		os.Exit(2)
	}

	f, err := os.Open(flag.Arg(0))
	if err != nil {
		panic(err)
	}

	reader := bufio.NewReader(f)
	ix, err := ihex.ReadIHex(reader)
	if err != nil {
		panic(err)
	}
	program := ix.ExtractDataToEnd(0)

	em := k680emlib.NewEmulator()
	em.TraceFile = os.Stdout
	em.LoadProgram(program, 0)

	err = em.Run()
	if err != nil {
		panic(err)
	}

	em.DumpState()
}
Example #5
0
File: k270em.go Project: iand/go
// Function main is the main entry point in the program.
func main() {
	flag.Parse()

	if flag.NArg() < 1 {
		fmt.Fprintf(os.Stderr, "Not enough arguments\nusage: %s file.hex\n", os.Args[0])
		os.Exit(2)
	}

	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		panic(sdl.GetError())
	}

	defer sdl.Quit()

	if ttf.Init() != 0 {
		panic(sdl.GetError())
	}

	defer ttf.Quit()

	rm := resourcemanager.NewResourceManager("github.com/kierdavis/go/k270em")

	font := ttf.OpenFont(rm.GetFilename("FreeMono.ttf"), 12)
	if font == nil {
		panic(sdl.GetError())
	}

	defer font.Close()

	_, _, _, _, fontWidth, errID := font.GlyphMetrics('m')
	if errID != 0 {
		panic(sdl.GetError())
	}

	fontHeight := font.Height()

	fmt.Printf("h: %d, w: %d\n", fontHeight, fontWidth)

	screenHeight := fontHeight * 48
	screenWidth := fontWidth * 128

	screen := sdl.SetVideoMode(screenWidth, screenHeight, 32, sdl.RESIZABLE)
	if screen == nil {
		panic(sdl.GetError())
	}

	sdl.WM_SetCaption("K270 Emulator Display", "")

	f, err := os.Open(flag.Arg(0))
	die(err)
	defer f.Close()

	reader := bufio.NewReader(f)
	ix, err := ihex.ReadIHex(reader)
	die(err)
	program := ix.ExtractDataToEnd(0)

	em := k270emlib.NewEmulator()
	em.SetTraceFile(os.Stdout)
	em.SetMemory(program)

	running := true
	stopRequest := make(chan bool)
	vmem := em.GetVideoMemory()

	go em.Run()
	go func() {
		scanTicker := time.NewTicker(time.Second / 24.0) // 24 hz
		color := sdl.Color{255, 255, 255, 0}

		for {
			if !running {
				break
			}

			select {
			case <-scanTicker.C:
				address := uint16(0)
				for y := 0; y < 48; y++ {
					chars := make([]byte, 128)

					for x := 0; x < 128; x++ {
						c := vmem[address].Char
						if c == 0 {
							c = ' '
						}
						chars[x] = c
						address++
					}

					surf := ttf.RenderText_Solid(font, string(chars), color)
					screen.Blit(&sdl.Rect{0, int16(y * fontHeight), 0, 0}, surf, nil)
				}

			case <-stopRequest:
				return
			}
		}
	}()

	ticker := time.NewTicker(time.Second / 24.0) // 24 fps

	for running {
		select {
		case <-ticker.C:
			screen.Flip()

		case ev := <-sdl.Events:
			switch e := ev.(type) {
			case sdl.QuitEvent:
				running = false

			case sdl.KeyboardEvent:
				if e.Keysym.Sym == sdl.K_ESCAPE {
					running = false
				}

			case sdl.ResizeEvent:
				screen = sdl.SetVideoMode(int(e.W), int(e.H), 32, sdl.RESIZABLE)
				if screen == nil {
					panic(sdl.GetError())
				}
			}
		}
	}

	stopRequest <- true

	//fmt.Println("Locking...")
	em.Mutex.Lock()
	//fmt.Println("Locked!")
	em.SetRunning(false)
	//fmt.Println("Unlocking...")
	em.Mutex.Unlock()
	//fmt.Println("Unlocked!")
}