From 9d08d82145e5db73a26ad9706b42c7f83c8a2347 Mon Sep 17 00:00:00 2001 From: yamatcha Date: Mon, 15 Jun 2020 01:56:41 +0900 Subject: [PATCH] add: Implement simple BIOS --- emulator/bios.go | 37 +++++++++++++++++++++++++++++++++++++ emulator/emulator.go | 4 +++- emulator/instruction.go | 15 +++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 emulator/bios.go diff --git a/emulator/bios.go b/emulator/bios.go new file mode 100644 index 0000000..5c171db --- /dev/null +++ b/emulator/bios.go @@ -0,0 +1,37 @@ +package emulator + +import "fmt" + +var biosToTerminal []int = []int{ + 30, 34, 32, 36, 31, 35, 33, 37} + +func putString(s string) { + for _, i := range s { + ioOut8(0x03f8, uint8(i)) + } +} + +func biosVideoTeletype(emu *Emulator) { + color := emu.Registers.getRegister8(BL) & 0x0f + ch := emu.Registers.getRegister8(AL) + var buf string + var bright int + terminalColor := biosToTerminal[color&0x07] + if color&0x08 == 1 { + bright = 1 + } else { + bright = 0 + } + buf = fmt.Sprintf("\x1b[%d;%dm%c\x1b[0m", bright, terminalColor, ch) + putString(buf) +} + +func biosVideo(emu *Emulator) { + function := emu.Registers.getRegister8(AH) + switch function { + case 0x0e: + biosVideoTeletype(emu) + default: + fmt.Printf("not implemented BIOS video function 0x%02x\n", function) + } +} diff --git a/emulator/emulator.go b/emulator/emulator.go index c8abf3b..30a8c04 100644 --- a/emulator/emulator.go +++ b/emulator/emulator.go @@ -6,6 +6,8 @@ const ( RegistersCount = 8 MemorySize = 1024 * 1024 AL = 0 + BL = 3 + AH = 4 EAX = 0 EDX = 2 ESP = 4 @@ -104,7 +106,7 @@ func (reg *Registers) setRegister8(index byte, value byte) { r := reg.GetRegister32(index) & 0xffffff00 reg.setRegister32(index, r|uint32(value)) } else { - r := reg.GetRegister32(index-4) & 0xffffff00 + r := reg.GetRegister32(index-4) & 0xffff00ff reg.setRegister32(index-4, r|uint32(value)<<8) } } diff --git a/emulator/instruction.go b/emulator/instruction.go index a642071..d1939ac 100644 --- a/emulator/instruction.go +++ b/emulator/instruction.go @@ -307,6 +307,17 @@ func outDxAl(emu *Emulator) { emu.Eip++ } +func swi(emu *Emulator) { + intIndex := GetCode8(emu, 1) + emu.Eip += 2 + switch intIndex { + case 0x10: + biosVideo(emu) + default: + fmt.Printf("unknown interrupt: 0x%02x\n", intIndex) + } +} + func Instructions(index byte) (func(emu *Emulator), error) { switch { case 0x01 == index: @@ -369,6 +380,10 @@ func Instructions(index byte) (func(emu *Emulator), error) { return movRm32Imm32, nil case 0xc9 == index: return leave, nil + + case 0xcd == index: + return swi, nil + case 0xe8 == index: return callRel32, nil case 0xe9 == index: