commit 48eee4d0f59c955b9bcf1e043e9050b7a836b327
parent 02ea8cdc060d78c7b76099ee7a59477d5477f966
Author: Gabriel G. de Brito <gabrielgbrito@icloud.com>
Date: Wed, 7 Feb 2024 17:22:43 -0300
More instructions for MIPS
Diffstat:
| M | main.go | | | 8 | ++++---- |
| M | mips/mips.go | | | 221 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- |
2 files changed, 206 insertions(+), 23 deletions(-)
diff --git a/main.go b/main.go
@@ -1,15 +1,15 @@
package main
import (
+ "bufio"
"flag"
- "log"
"fmt"
- "os"
- "bufio"
"io"
+ "log"
+ "os"
- "github.com/gboncoffee/egg/riscv"
"github.com/gboncoffee/egg/machine"
+ "github.com/gboncoffee/egg/riscv"
)
// Put new architetures here...
diff --git a/mips/mips.go b/mips/mips.go
@@ -2,7 +2,11 @@
package mips
import (
- "github.com/gboncoffee/egg/assembler"
+ "errors"
+ "fmt"
+ "math"
+ "math/bits"
+
"github.com/gboncoffee/egg/machine"
)
@@ -15,11 +19,76 @@ const (
type Mips struct {
// 32 and 33 are HI and LO.
registers [34]uint32
- pc uint32
- mem [math.MaxUint32 + 1]uint8
+ pc uint32
+ mem [math.MaxUint32 + 1]uint8
}
//
+// Instructions
+//
+// add DONE
+// addi DONE
+// addiu DONE
+// addu DONE
+// clo DONE
+// clz DONE
+// lui
+// seb DONE
+// seh DONE
+// sub DONE
+// subu DONE
+// sll DONE
+// sllv DONE
+// sra DONE
+// srav DONE
+// srl DONE
+// srlv DONE
+// and DONE
+// andi
+// nor DONE
+// or DONE
+// ori
+// xor DONE
+// xori
+// movn DONE
+// movz DONE
+// slt DONE
+// slti
+// sltiu
+// sltu DONE
+// div DONE
+// mult DONE
+// mfhi DONE
+// mflo DONE
+// mthi DONE
+// mtlo DONE
+// beq
+// bgez
+// bgtz
+// blez
+// bltz
+// bne
+// break
+// syscall
+// j
+// jal
+// jalr
+// jr
+// lb
+// lbu
+// lh
+// lhu
+// lw
+// lwl
+// lwr
+// sb
+// sh
+// sw
+// swl
+// swr
+//
+
+//
// A lot of the functions are straight-outta copied from the RISC-V
// implementation: both architetures are kinda similar.
//
@@ -47,23 +116,27 @@ func parseJ(i uint32) uint32 {
// - rd
// - shamt
// - funct
-func parseR(i uint32) (uint8, uint8, uint8. uint8, uint8) {
- rs := i & 0b11111000000000000000000000
- rt := i & 0b111110000000000000000
- rd := i & 0b1111100000000000
- shamt := i & 0b11111000000
- funct := i & 0b111111
+func parseR(i uint32) (uint8, uint8, uint8, uint8, uint8) {
+ rs := uint8(i & 0b11111000000000000000000000)
+ rt := uint8(i & 0b111110000000000000000)
+ rd := uint8(i & 0b1111100000000000)
+ shamt := uint8(i & 0b11111000000)
+ funct := uint8(i & 0b111111)
return rs, rt, rd, shamt, funct
}
-func (m *Mips) execArithmeticInstruction(rs, rt, rd, shamt, funct uint8) {
- rsv64, _ := m.GetRegister(rs)
- rtv64, _ := m.GetRegister(rt)
+func (m *Mips) execSpecial(rs, rt, rd, shamt, funct uint8) {
+ rsv64, _ := m.GetRegister(uint64(rs))
+ rtv64, _ := m.GetRegister(uint64(rd))
rsv := int32(rsv64)
rtv := int32(rtv64)
+ // A lot of instructions conditionally or do not change the RD, so we
+ // init r with it's value.
var r int32
+ rdv64, _ := m.GetRegister(uint64(rd))
+ r = int32(rdv64)
switch funct {
// add addu
// sub subu
@@ -75,7 +148,7 @@ func (m *Mips) execArithmeticInstruction(rs, rt, rd, shamt, funct uint8) {
case 0x20:
r = rsv + rtv
case 0x21:
- r = uint32(uint32(rsv) + uint32(rtv))
+ r = int32(uint32(rsv) + uint32(rtv))
case 0x22:
r = rsv - rtv
case 0x23:
@@ -100,27 +173,137 @@ func (m *Mips) execArithmeticInstruction(rs, rt, rd, shamt, funct uint8) {
r = rsv ^ rtv
case 0x27:
r = ^(rsv | rtv)
+ // sll
+ // sllv
+ // sra
+ // srav
+ // srl
+ // srlv
+ case 0:
+ r = rtv << shamt
+ case 4:
+ r = rtv << rsv
+ case 3:
+ r = rtv >> shamt
+ case 7:
+ r = rtv >> rsv
+ case 2:
+ r = int32(uint32(rtv) >> uint32(shamt))
+ case 6:
+ r = int32(uint32(rtv) >> uint32(rsv))
// mult
+ // div
case 0x18:
res := rsv64 * rtv64
- m.SetRegister(32, uint64(res >> 32))
- m.SetRegister(33, uint64(res & 0x00000000ffffffff))
- // TODO mul muh mulu muhu
+ m.SetRegister(HI, uint64(res>>32))
+ m.SetRegister(LO, uint64(res&0x00000000ffffffff))
+ case 0x1a:
+ m.SetRegister(HI, uint64(rsv%rtv))
+ m.SetRegister(LO, uint64(rsv/rtv))
// mfhi
+ // mflo
+ // mthi
+ // mtlo
case 0x10:
- hi, _ := m.GetRegister(32)
+ hi, _ := m.GetRegister(HI)
r = int32(hi)
+ case 0x12:
+ lo, _ := m.GetRegister(LO)
+ r = int32(lo)
+ case 0x11:
+ m.SetRegister(HI, uint64(rsv))
+ case 0x13:
+ m.SetRegister(LO, uint64(rsv))
+ // movz
+ // movn
+ case 0xa:
+ if rtv == 0 {
+ r = rsv
+ }
+ case 0xb:
+ if rtv != 0 {
+ r = rsv
+ }
+ }
+
+ m.SetRegister(uint64(rd), uint64(r))
+}
+
+func (m *Mips) execSpecial2(rs, rt, rd, shamt, funct uint8) {
+ rsv64, _ := m.GetRegister(uint64(rs))
+ rsv := int32(rsv64)
+
+ var r int32
+ switch funct {
+ // clz
+ // clo
+ case 16:
+ r = int32(bits.LeadingZeros32(uint32(rsv)))
+ case 17:
+ r = int32(bits.LeadingZeros32(uint32(^rsv)))
+ }
+
+ m.SetRegister(uint64(rd), uint64(r))
+}
+
+func (m *Mips) execSpecial3(rs, rt, rd, shamt, funct uint8) {
+ rsv64, _ := m.GetRegister(uint64(rs))
+ rsv := int32(rsv64)
+
+ var r int32
+ switch funct {
+ case 32:
+ switch shamt {
+ // seb
+ // seh
+ case 16:
+ rsvb := rsv & 0xff
+ sign := rsvb >> 7
+ sign = (^(sign - 1)) << 8
+ r = int32(rsvb | sign)
+ case 24:
+ rsvb := rsv & 0xff
+ sign := rsvb >> 15
+ sign = (^(sign - 1)) << 16
+ r = int32(rsvb | sign)
+ }
}
m.SetRegister(uint64(rd), uint64(r))
}
+func (m *Mips) executeAddi(rs, rt uint8, imm uint32) {
+ rsv64, _ := m.GetRegister(uint64(rs))
+ r := int32(rsv64) + int32(imm)
+ m.SetRegister(uint64(rt), uint64(r))
+}
+
+func (m *Mips) executeAddiu(rs, rt uint8, imm uint32) {
+ rsv64, _ := m.GetRegister(uint64(rs))
+ r := uint32(rsv64) + imm
+ m.SetRegister(uint64(rt), uint64(r))
+}
+
func (m *Mips) execute(i uint32) (*machine.Call, error) {
opcode := i & 0b111111
switch opcode {
case 0:
rs, rt, rd, shamt, funct := parseR(i)
- m.execArithmeticInstruction(rs, rt, rd, shamt, funct)
+ m.execSpecial(rs, rt, rd, shamt, funct)
+ case 28:
+ rs, rt, rd, shamt, funct := parseR(i)
+ m.execSpecial2(rs, rt, rd, shamt, funct)
+ case 31:
+ rs, rt, rd, shamt, funct := parseR(i)
+ m.execSpecial3(rs, rt, rd, shamt, funct)
+ // addi
+ case 8:
+ rs, rt, imm := parseI(i)
+ m.executeAddi(rs, rt, imm)
+ // addiu
+ case 9:
+ rs, rt, imm := parseI(i)
+ m.executeAddiu(rs, rt, imm)
default:
return nil, errors.New(fmt.Sprintf("Unknown opcode: %b", opcode))
}
@@ -138,7 +321,7 @@ func (m *Mips) GetRegister(reg uint64) (uint64, error) {
func (m *Mips) SetRegister(reg uint64, content uint64) error {
if reg >= 34 {
- return 0, errors.New(fmt.Sprintf("No such register: %d. MIPS-I has only 32 general purpouse registers and two special registers for multiplication and division (HI and LO, 32 and 33).", reg))
+ return errors.New(fmt.Sprintf("No such register: %d. MIPS-I has only 32 general purpouse registers and two special registers for multiplication and division (HI and LO, 32 and 33).", reg))
}
if reg != 0 {