Skip to content

Commit

Permalink
add: Implement io8 to get and put char
Browse files Browse the repository at this point in the history
  • Loading branch information
yamatcha committed Jun 9, 2020
1 parent 72e2b37 commit b816667
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
20 changes: 20 additions & 0 deletions emulator/emulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ type RegisterID int
const (
RegistersCount = 8
MemorySize = 1024 * 1024
AL = 0
EDX = 2
ESP = 4
EBP = 5
CARRYFLAG uint32 = 1
Expand Down Expand Up @@ -88,6 +90,24 @@ func (reg *Registers) GetRegister32(index byte) uint32 {
return 0
}

func (reg *Registers) getRegister8(index byte) byte {
if index < 4 {
return byte(reg.GetRegister32(index) & 0xff)
} else {
return byte((reg.GetRegister32(index-4) >> 8) & 0xff)
}
}

func (reg *Registers) setRegister8(index byte, value byte) {
if index < 4 {
r := reg.GetRegister32(index) & 0xffffff00
reg.setRegister32(index, r|uint32(value))
} else {
r := reg.GetRegister32(index-4) & 0xffffff00
reg.setRegister32(index-4, r|uint32(value)<<8)
}
}

func GetCode8(emu *Emulator, index int) byte {
return emu.Memory[int(emu.Eip)+index]
}
Expand Down
18 changes: 18 additions & 0 deletions emulator/instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,20 @@ func shortJump(emu *Emulator) {
emu.Eip += uint32(diff + 2)
}

func inAlDx(emu *Emulator) {
address := emu.Registers.GetRegister32(EDX) & 0xffff
value := ioIn8(uint16(address))
emu.Registers.setRegister8(AL, value)
emu.Eip++
}

func outDxAl(emu *Emulator) {
address := emu.Registers.GetRegister32(EDX) & 0xffff
value := emu.Registers.getRegister8(AL)
ioOut8(uint16(address), value)
emu.Eip++
}

func Instructions(index byte) (func(emu *Emulator), error) {
switch {
case 0x01 == index:
Expand Down Expand Up @@ -305,6 +319,10 @@ func Instructions(index byte) (func(emu *Emulator), error) {
return nearJump, nil
case 0xeb == index:
return shortJump, nil
case 0xec == index:
return inAlDx, nil
case 0xee == index:
return outDxAl, nil
case 0xff == index:
return codeff, nil
}
Expand Down
25 changes: 25 additions & 0 deletions emulator/io.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package emulator

import (
"bufio"
"fmt"
"os"
)

func ioIn8(address uint16) uint8 {
switch address {
case 0x03f8:
reader := bufio.NewReader(os.Stdin)
input, _ := reader.ReadByte()
return input
default:
return 0
}
}

func ioOut8(address uint16, value uint8) {
switch address {
case 0x03f8:
fmt.Printf("%c", value)
}
}

0 comments on commit b816667

Please sign in to comment.