31 lines
531 B
Go
31 lines
531 B
Go
|
|
package vm
|
||
|
|
|
||
|
|
import "bytes"
|
||
|
|
|
||
|
|
import "github.com/lunixbochs/struc"
|
||
|
|
import "github.com/pkg/errors"
|
||
|
|
|
||
|
|
const InstructionBytes = 32
|
||
|
|
|
||
|
|
type Instruction struct {
|
||
|
|
OpCode OpCodeType
|
||
|
|
Reserved [4]byte
|
||
|
|
Operand1 Operand
|
||
|
|
Operand2 Operand
|
||
|
|
|
||
|
|
opHandler OpHandler `struc:"skip"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewInstruction(byteCode []byte) (*Instruction, error) {
|
||
|
|
instr := &Instruction{}
|
||
|
|
|
||
|
|
reader := bytes.NewReader(byteCode)
|
||
|
|
|
||
|
|
err := struc.Unpack(reader, instr)
|
||
|
|
if err != nil {
|
||
|
|
return nil, errors.Wrap(err, "Error decoding instruction")
|
||
|
|
}
|
||
|
|
|
||
|
|
return instr, nil
|
||
|
|
}
|