Last active
August 29, 2015 14:04
-
-
Save cryptix/2848e048fe4635b35e30 to your computer and use it in GitHub Desktop.
Java>Go PSHDL Simulator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package SimpleAlu | |
type regUpdate struct { | |
internal, offset int | |
} | |
type SimpleAluGenerator struct { | |
varIdx map[string]int | |
regUpdates []regUpdate | |
regUpdatePos int | |
disableEdge bool | |
disableRegOutputLogic bool | |
epsCycle int64 | |
deltaCycle int64 | |
timeStamp int64 | |
_Pred_alu_if0_fresh bool | |
_Pred_alu_switch0_case0_fresh bool | |
_Pred_alu_switch0_case1_fresh bool | |
_Pred_alu_switch0_case2_fresh bool | |
_Pred_alu_rst_fresh bool | |
_Pred_alu_if0 bool | |
_Pred_alu_switch0_case0 bool | |
_Pred_alu_switch0_case1 bool | |
_Pred_alu_switch0_case2 bool | |
_Pred_alu_rst bool | |
_Pred_alu_if0_update int64 | |
_Pred_alu_switch0_case0_update int64 | |
_Pred_alu_switch0_case1_update int64 | |
_Pred_alu_switch0_case2_update int64 | |
_Pred_alu_rst_update int64 | |
alu_clk_pos_active bool | |
alu_clk_pos_handled bool | |
alu_tmp_res_1_OutRead__reg int64 | |
alu_tmp_res_1_OutRead int64 | |
alu_switch0 int64 | |
alu_clk_prev int64 | |
alu_clk_update int64 | |
alu_clk int64 | |
alu_instr int64 | |
alu_isZero int64 | |
alu_op1 int64 | |
alu_op2 int64 | |
alu_res int64 | |
alu_rst int64 | |
} | |
func NewSimpleAluGenerator() *SimpleAluGenerator { | |
return NewSimpleAluGeneratorWithArgs(false, false) | |
} | |
func NewSimpleAluGeneratorWithArgs(disableEdge, disableRegOutputLogic bool) *SimpleAluGenerator { | |
var s = SimpleAluGenerator{ | |
disableEdge: disableEdge, | |
disableRegOutputLogic: disableRegOutputLogic, | |
} | |
s.regUpdates = make([]regUpdate, 4) | |
s.varIdx = make(map[string]int, 14) | |
s.varIdx["$Pred_alu.@if0"] = 1 | |
s.varIdx["$Pred_alu.@switch0.@case0"] = 2 | |
s.varIdx["$Pred_alu.@switch0.@case1"] = 3 | |
s.varIdx["$Pred_alu.@switch0.@case2"] = 4 | |
s.varIdx["$Pred_alu.rst"] = 5 | |
s.varIdx["alu.$tmp_res_1_OutRead"] = 6 | |
s.varIdx["alu.@switch0"] = 7 | |
s.varIdx["alu.clk"] = 8 | |
s.varIdx["alu.instr"] = 9 | |
s.varIdx["alu.isZero"] = 10 | |
s.varIdx["alu.op1"] = 11 | |
s.varIdx["alu.op2"] = 12 | |
s.varIdx["alu.res"] = 13 | |
s.varIdx["alu.rst"] = 14 | |
return &s | |
} | |
func (s *SimpleAluGenerator) skipEdge(local int64) bool { | |
var dc = local >> 16 // zero-extended shift | |
if dc < s.deltaCycle { | |
return false | |
} | |
if (dc == s.deltaCycle) && ((local & 0xFFFF) == s.epsCycle) { | |
return false | |
} | |
return true | |
} | |
func (s *SimpleAluGenerator) updateRegs() { | |
for _, r := range s.regUpdates { | |
switch r.internal { | |
case 6: | |
s.alu_tmp_res_1_OutRead = s.alu_tmp_res_1_OutRead__reg | |
} | |
} | |
} | |
func (s *SimpleAluGenerator) s000_frame_0004() { | |
var t0, t1 int64 | |
t0 = 0 //const0 | |
t1 = (t0) & 1 //cast_uint[targetSize=1,currentSize=32] | |
s.alu_isZero = t1 //Assign value | |
} | |
func (s *SimpleAluGenerator) s001_frame_0002() { | |
var t0 int64 | |
t0 = s.alu_tmp_res_1_OutRead //loadInternal[alu.$tmp_res_1_OutRead] | |
s.alu_res = t0 //Assign value | |
} | |
func (s *SimpleAluGenerator) s001_frame_001E() { | |
var t0 int64 = s.alu_tmp_res_1_OutRead //loadInternal[alu.$tmp_res_1_OutRead] | |
var t1 int64 = 0 //const0 | |
var t2 int64 = (t1) & 0x0000001F //cast_uint[targetSize=5,currentSize=32] | |
var t3 bool = t0 == t2 //eq | |
s._Pred_alu_if0 = t3 //Assign value | |
s._Pred_alu_if0_update = s.timeStamp //update timestamp | |
} | |
func (s *SimpleAluGenerator) s001_frame_000D() { | |
var t0, t1 int64 | |
t0 = s.alu_instr //loadInternal[alu.instr] | |
t1 = (t0 >> 0) & 0x0000000F //bitAccessSingleRange[from=3,to=0] | |
s.alu_switch0 = t1 //Assign value | |
} | |
func (s *SimpleAluGenerator) s001_frame_0025() { | |
var t0, t1 int64 | |
var t2 bool | |
t0 = s.alu_rst //loadInternal[alu.rst] | |
t1 = 0 //const0 | |
t2 = t0 != t1 //not_eq | |
s._Pred_alu_rst = t2 //Assign value | |
s._Pred_alu_rst_update = s.timeStamp //update timestamp | |
} | |
func (s *SimpleAluGenerator) s003_frame_0021() { | |
var t1, t2 int64 | |
//posPredicate[$Pred_alu.@if0] | |
t1 = 0 //const0 | |
t2 = (t1) & 1 //cast_uint[targetSize=1,currentSize=32] | |
s.alu_isZero = t2 //Assign value | |
} | |
func (s *SimpleAluGenerator) s003_frame_000E() { | |
var t0, t1 int64 | |
var t2 bool | |
t0 = s.alu_switch0 //loadInternal[alu.@switch0] | |
t1 = 0 //loadConstant[constantIdx=0] | |
t2 = t0 == t1 //eq | |
s._Pred_alu_switch0_case0 = t2 //Assign value | |
s._Pred_alu_switch0_case0_update = s.timeStamp //update timestamp | |
} | |
func (s *SimpleAluGenerator) s003_frame_0013() { | |
var t0, t1 int64 | |
var t2 bool | |
t0 = s.alu_switch0 //loadInternal[alu.@switch0] | |
t1 = 1 //loadConstant[constantIdx=0] | |
t2 = t0 == t1 //eq | |
s._Pred_alu_switch0_case1 = t2 //Assign value | |
s._Pred_alu_switch0_case1_update = s.timeStamp //update timestamp | |
} | |
func (s *SimpleAluGenerator) s004_frame_0023() { | |
var t1, t2 int64 | |
//negPredicate[$Pred_alu.@if0] | |
t1 = 1 //const1 | |
t2 = (t1) & 1 //cast_uint[targetSize=1,currentSize=32] | |
s.alu_isZero = t2 //Assign value | |
} | |
func (s *SimpleAluGenerator) s005_frame_000F() { | |
var t3, t4, t5, t5_cpy int64 | |
//posPredicate[$Pred_alu.@switch0.@case0] | |
s.alu_clk_update = s.timeStamp //isRisingEdge[internalIdx=11] | |
//negPredicate[$Pred_alu.rst] | |
t3 = s.alu_op1 //loadInternal[alu.op1] | |
t4 = s.alu_op2 //loadInternal[alu.op2] | |
t5 = (t3 + t4) & 0x0000001F //plus[targetSizeWithType=10] | |
t5_cpy = s.alu_tmp_res_1_OutRead //Backup of current value | |
s.alu_tmp_res_1_OutRead__reg = t5 //Assign value | |
if t5_cpy != s.alu_tmp_res_1_OutRead__reg { | |
s.regUpdates[s.regUpdatePos] = regUpdate{6, 0} | |
s.regUpdatePos++ | |
} | |
} | |
func (s *SimpleAluGenerator) s005_frame_0018() { | |
var t2, t3 int64 | |
var t4 bool | |
//negPredicate[$Pred_alu.@switch0.@case0] | |
//negPredicate[$Pred_alu.@switch0.@case1] | |
t2 = 1 //const1 | |
t3 = 1 //const1 | |
t4 = t2 == t3 //eq | |
s._Pred_alu_switch0_case2 = t4 //Assign value | |
s._Pred_alu_switch0_case2_update = s.timeStamp //update timestamp | |
} | |
func (s *SimpleAluGenerator) s006_frame_0014() { | |
var t3, t4, t5, t5_cpy int64 | |
//posPredicate[$Pred_alu.@switch0.@case1] | |
s.alu_clk_update = s.timeStamp //isRisingEdge[internalIdx=11] | |
//negPredicate[$Pred_alu.rst] | |
t3 = s.alu_op1 //loadInternal[alu.op1] | |
t4 = s.alu_op2 //loadInternal[alu.op2] | |
t5 = (t3 - t4) & 0x0000001F //minus[targetSizeWithType=10] | |
t5_cpy = s.alu_tmp_res_1_OutRead //Backup of current value | |
s.alu_tmp_res_1_OutRead__reg = t5 //Assign value | |
if t5_cpy != s.alu_tmp_res_1_OutRead__reg { | |
s.regUpdates[s.regUpdatePos] = regUpdate{6, 0} | |
s.regUpdatePos++ | |
} | |
} | |
func (s *SimpleAluGenerator) s007_frame_0019() { | |
var t3, t4, t5, t6, t6_cpy int64 | |
//posPredicate[$Pred_alu.@switch0.@case2] | |
s.alu_clk_update = s.timeStamp //isRisingEdge[internalIdx=11] | |
//negPredicate[$Pred_alu.rst] | |
t3 = s.alu_op1 //loadInternal[alu.op1] | |
t4 = s.alu_op2 //loadInternal[alu.op2] | |
t5 = (t3 * t4) & 0x000003FF //mul[targetSizeWithType=20] | |
t6 = (t5) & 0x0000001F //cast_uint[targetSize=5,currentSize=10] | |
t6_cpy = s.alu_tmp_res_1_OutRead //Backup of current value | |
s.alu_tmp_res_1_OutRead__reg = t6 //Assign value | |
if t6_cpy != s.alu_tmp_res_1_OutRead__reg { | |
s.regUpdates[s.regUpdatePos] = regUpdate{6, 0} | |
s.regUpdatePos++ | |
} | |
} | |
func (s *SimpleAluGenerator) s009_frame_000A() { | |
var t2, t3, t3_cpy int64 | |
//posPredicate[$Pred_alu.rst] | |
s.alu_clk_update = s.timeStamp //isRisingEdge[internalIdx=11] | |
t2 = 0 //const0 | |
t3 = (t2) & 0x0000001F //cast_uint[targetSize=5,currentSize=32] | |
t3_cpy = s.alu_tmp_res_1_OutRead //Backup of current value | |
s.alu_tmp_res_1_OutRead__reg = t3 //Assign value | |
if t3_cpy != s.alu_tmp_res_1_OutRead__reg { | |
s.regUpdates[s.regUpdatePos] = regUpdate{6, 0} //writeInternal[internal=6] | |
s.regUpdatePos++ | |
} | |
// t5 = 0 //const0 | |
//Write to #null | |
} | |
func (s *SimpleAluGenerator) initConstants() { | |
s.epsCycle = 0 | |
s.timeStamp = (s.deltaCycle << 16) | s.epsCycle | |
} | |
func (s *SimpleAluGenerator) Run() { | |
s.epsCycle = 0 | |
s.deltaCycle += 1 | |
for { | |
s.regUpdatePos = 0 | |
s.timeStamp = (s.deltaCycle << 16) | s.epsCycle | |
s.stage0000() | |
s.stage0001() | |
s.stage0003() | |
s.stage0004() | |
s.stage0005() | |
s.stage0006() | |
s.stage0007() | |
s.stage0009() | |
s.updateRegs() | |
s.epsCycle += 1 | |
// WARNING: go hat keine do-while loop | |
if len(s.regUpdates) > 0 && !s.disableRegOutputLogic { | |
break | |
} | |
} | |
s.alu_clk_prev = s.alu_clk | |
} | |
func (s *SimpleAluGenerator) stage0000() { | |
s.s000_frame_0004() | |
} | |
func (s *SimpleAluGenerator) stage0001() { | |
s.s001_frame_0002() | |
s.s001_frame_001E() | |
s.s001_frame_000D() | |
s.s001_frame_0025() | |
} | |
func (s *SimpleAluGenerator) stage0003() { | |
s._Pred_alu_if0_fresh = s._Pred_alu_if0_update == s.timeStamp | |
if s._Pred_alu_if0 && | |
s._Pred_alu_if0_fresh { | |
s.s003_frame_0021() | |
} | |
s.s003_frame_000E() | |
s.s003_frame_0013() | |
} | |
func (s *SimpleAluGenerator) stage0004() { | |
if !s._Pred_alu_if0 && | |
s._Pred_alu_if0_fresh { | |
s.s004_frame_0023() | |
} | |
} | |
func (s *SimpleAluGenerator) stage0005() { | |
s.alu_clk_pos_active = ((s.alu_clk_prev == 0) || s.disableEdge) && (s.alu_clk == 1) | |
s.alu_clk_pos_handled = s.skipEdge(s.alu_clk_update) | |
s._Pred_alu_rst_fresh = s._Pred_alu_rst_update == s.timeStamp | |
s._Pred_alu_switch0_case0_fresh = s._Pred_alu_switch0_case0_update == s.timeStamp | |
s._Pred_alu_switch0_case1_fresh = s._Pred_alu_switch0_case1_update == s.timeStamp | |
if s.alu_clk_pos_active && | |
!s.alu_clk_pos_handled && | |
!s._Pred_alu_rst && | |
s._Pred_alu_rst_fresh && | |
s._Pred_alu_switch0_case0 && | |
s._Pred_alu_switch0_case0_fresh { | |
s.s005_frame_000F() | |
} | |
if !s._Pred_alu_switch0_case0 && | |
s._Pred_alu_switch0_case0_fresh && | |
!s._Pred_alu_switch0_case1 && | |
s._Pred_alu_switch0_case1_fresh { | |
s.s005_frame_0018() | |
} | |
} | |
func (s *SimpleAluGenerator) stage0006() { | |
if s.alu_clk_pos_active && | |
!s.alu_clk_pos_handled && | |
!s._Pred_alu_rst && | |
s._Pred_alu_rst_fresh && | |
s._Pred_alu_switch0_case1 && | |
s._Pred_alu_switch0_case1_fresh { | |
s.s006_frame_0014() | |
} | |
} | |
func (s *SimpleAluGenerator) stage0007() { | |
s._Pred_alu_switch0_case2_fresh = s._Pred_alu_switch0_case2_update == s.timeStamp | |
if s.alu_clk_pos_active && | |
!s.alu_clk_pos_handled && | |
!s._Pred_alu_rst && | |
s._Pred_alu_rst_fresh && | |
s._Pred_alu_switch0_case2 && | |
s._Pred_alu_switch0_case2_fresh { | |
s.s007_frame_0019() | |
} | |
} | |
func (s *SimpleAluGenerator) stage0009() { | |
if s.alu_clk_pos_active && | |
!s.alu_clk_pos_handled && | |
s._Pred_alu_rst && | |
s._Pred_alu_rst_fresh { | |
s.s009_frame_000A() | |
} | |
} | |
// go hat kein overloading | |
func (s *SimpleAluGenerator) SetInputWithName(name string, value int64, arrayIdx ...int) { | |
s.SetInput(s.GetIndex(name), value, arrayIdx...) | |
} | |
func (s *SimpleAluGenerator) SetInput(idx int, value int64, arrayIdx ...int) { | |
switch idx { | |
case 1: | |
s._Pred_alu_if0 = value != 0 | |
break | |
case 2: | |
s._Pred_alu_switch0_case0 = value != 0 | |
break | |
case 3: | |
s._Pred_alu_switch0_case1 = value != 0 | |
break | |
case 4: | |
s._Pred_alu_switch0_case2 = value != 0 | |
break | |
case 5: | |
s._Pred_alu_rst = value != 0 | |
break | |
case 6: | |
s.alu_tmp_res_1_OutRead = value | |
break | |
case 7: | |
s.alu_switch0 = value | |
break | |
case 8: | |
s.alu_clk = value | |
break | |
case 9: | |
s.alu_instr = value | |
break | |
case 10: | |
s.alu_isZero = value | |
break | |
case 11: | |
s.alu_op1 = value | |
break | |
case 12: | |
s.alu_op2 = value | |
break | |
case 13: | |
s.alu_res = value | |
break | |
case 14: | |
s.alu_rst = value | |
break | |
default: | |
panic("Not a valid index") | |
} | |
} | |
func (s *SimpleAluGenerator) GetIndex(name string) int { | |
idx, ok := s.varIdx[name] | |
if !ok { | |
panic("The name:" + name + " is not a valid index") | |
} | |
return idx | |
} | |
func (s *SimpleAluGenerator) GetName(idx int) string { | |
switch idx { | |
case 1: return "$Pred_alu.@if0" | |
case 2: return "$Pred_alu.@switch0.@case0" | |
case 3: return "$Pred_alu.@switch0.@case1" | |
case 4: return "$Pred_alu.@switch0.@case2" | |
case 5: return "$Pred_alu.rst" | |
case 6: return "alu.$tmp_res_1_OutRead" | |
case 7: return "alu.@switch0" | |
case 8: return "alu.clk" | |
case 9: return "alu.instr" | |
case 10: return "alu.isZero" | |
case 11: return "alu.op1" | |
case 12: return "alu.op2" | |
case 13: return "alu.res" | |
case 14: return "alu.rst" | |
default: panic("Not a valid index:") | |
} | |
} | |
func (s *SimpleAluGenerator) GetOutputWithName(name string, arrayIdx ...int) int64 { | |
return s.GetOutput(s.GetIndex(name), arrayIdx...) | |
} | |
func (s *SimpleAluGenerator) GetOutput(idx int, arrayIdx ...int) int64 { | |
switch idx { | |
case 1: | |
return bool2int(s._Pred_alu_if0) | |
case 2: | |
return bool2int(s._Pred_alu_switch0_case0) | |
case 3: | |
return bool2int(s._Pred_alu_switch0_case1) | |
case 4: | |
return bool2int(s._Pred_alu_switch0_case2) | |
case 5: | |
return bool2int(s._Pred_alu_rst) | |
case 6: | |
return s.alu_tmp_res_1_OutRead & 0x0000001F | |
case 7: | |
return s.alu_switch0 & 0x0000000F | |
case 8: | |
return s.alu_clk & 1 | |
case 9: | |
return s.alu_instr & 0x0000000F | |
case 10: | |
return s.alu_isZero & 1 | |
case 11: | |
return s.alu_op1 & 0x0000001F | |
case 12: | |
return s.alu_op2 & 0x0000001F | |
case 13: | |
return s.alu_res & 0x0000001F | |
case 14: | |
return s.alu_rst & 1 | |
default: | |
panic("Not a valid index:") | |
} | |
} | |
func (s *SimpleAluGenerator) getDeltaCycle() int64 { | |
return s.deltaCycle | |
} | |
func bool2int(in bool) int64 { | |
if in { | |
return 1 | |
} else { | |
return 0 | |
} | |
} | |
// func (s *SimpleAluGenerator) close() throws Exception{ | |
// } | |
// func (s *SimpleAluGenerator) setFeature(Feature feature, Object value) { | |
// switch (feature) { | |
// case disableOutputRegs: | |
// disableRegOutputLogic = (boolean) value; | |
// break; | |
// case disableEdges: | |
// diableEdge = (boolean) value; | |
// break; | |
// } | |
// } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package SimpleAlu | |
import "testing" | |
func newSim() *SimpleAluGenerator { | |
r := NewSimpleAluGenerator() | |
r.SetInputWithName("alu.rst", 1) | |
r.Run() | |
r.SetInputWithName("alu.rst", 0) | |
return r | |
} | |
func TestInstrAdd(t *testing.T) { | |
r := NewSimpleAluGenerator() | |
var oneCycle = func() { | |
r.SetInputWithName("alu.clk", 1) | |
r.Run() | |
r.SetInputWithName("alu.clk", 0) | |
r.Run() | |
} | |
r.SetInputWithName("alu.instr", 0) | |
var i int64 | |
for i = 0; i < 16; i++ { | |
r.SetInputWithName("alu.op1", i) | |
r.SetInputWithName("alu.op2", i) | |
oneCycle() | |
res := r.GetOutputWithName("alu.res") | |
if i+i != res { | |
t.Errorf("Wanted %d got %d", i+i, res) | |
} | |
} | |
} | |
func TestInstrSub(t *testing.T) { | |
r := NewSimpleAluGenerator() | |
var oneCycle = func() { | |
r.SetInputWithName("alu.clk", 1) | |
r.Run() | |
r.SetInputWithName("alu.clk", 0) | |
r.Run() | |
} | |
r.SetInputWithName("alu.instr", 1) | |
var i int64 | |
for i = 0; i < 16; i++ { | |
r.SetInputWithName("alu.op1", i) | |
r.SetInputWithName("alu.op2", i) | |
oneCycle() | |
res := r.GetOutputWithName("alu.res") | |
if i-i != res { | |
t.Errorf("Wanted %d got %d", i+i, res) | |
} | |
} | |
} | |
func BenchmarkAddWithName(b *testing.B) { | |
r := NewSimpleAluGenerator() | |
var oneCycle = func() { | |
r.SetInputWithName("alu.clk", 1) | |
r.Run() | |
r.SetInputWithName("alu.clk", 0) | |
r.Run() | |
} | |
var i int64 | |
for i = 0; i < int64(b.N); i++ { | |
r.SetInputWithName("alu.op1", i) | |
r.SetInputWithName("alu.op2", i) | |
oneCycle() | |
_ = r.GetOutputWithName("alu.res") | |
} | |
} | |
func BenchmarkAddWithIdx(b *testing.B) { | |
r := NewSimpleAluGenerator() | |
idxClk := r.GetIndex("alu.clk") | |
idxOp1 := r.GetIndex("alu.op1") | |
idxOp2 := r.GetIndex("alu.op2") | |
idxRes := r.GetIndex("alu.res") | |
var oneCycle = func() { | |
r.SetInput(idxClk, 1) | |
r.Run() | |
r.SetInput(idxClk, 0) | |
r.Run() | |
} | |
var i int64 | |
for i = 0; i < int64(b.N); i++ { | |
r.SetInput(idxOp1, i) | |
r.SetInput(idxOp2, i) | |
oneCycle() | |
_ = r.GetOutput(idxRes) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.util.*; | |
import java.math.*; | |
import org.pshdl.interpreter.*; | |
import java.util.concurrent.*; | |
public class testSimpleAluGenerator | |
implements IHDLInterpreter | |
{ | |
private Map<String, Integer> varIdx=new HashMap<String, Integer>(); | |
public long _Pred_alu_if0_update = 0; | |
private boolean _Pred_alu_if0_fresh = false; | |
public boolean _Pred_alu_if0 = false; | |
public long _Pred_alu_switch0_case0_update = 0; | |
private boolean _Pred_alu_switch0_case0_fresh = false; | |
public boolean _Pred_alu_switch0_case0 = false; | |
public long _Pred_alu_switch0_case1_update = 0; | |
private boolean _Pred_alu_switch0_case1_fresh = false; | |
public boolean _Pred_alu_switch0_case1 = false; | |
public long _Pred_alu_switch0_case2_update = 0; | |
private boolean _Pred_alu_switch0_case2_fresh = false; | |
public boolean _Pred_alu_switch0_case2 = false; | |
public long _Pred_alu_rst_update = 0; | |
private boolean _Pred_alu_rst_fresh = false; | |
public boolean _Pred_alu_rst = false; | |
private long alu_tmp_res_1_OutRead$reg = 0; | |
public long alu_tmp_res_1_OutRead = 0; | |
public long alu_switch0 = 0; | |
private long alu_clk_prev = 0; | |
private boolean alu_clk_pos_active = false; | |
private boolean alu_clk_pos_handled = false; | |
public long alu_clk_update = 0; | |
public long alu_clk = 0; | |
public long alu_instr = 0; | |
public long alu_isZero = 0; | |
public long alu_op1 = 0; | |
public long alu_op2 = 0; | |
public long alu_res = 0; | |
public long alu_rst = 0; | |
public long epsCycle = 0; | |
public long deltaCycle = 0; | |
public long timeStamp = 0; | |
public boolean diableEdge = false; | |
public boolean disableRegOutputLogic = false; | |
private Set<RegUpdate> regUpdates=new LinkedHashSet<RegUpdate>(); | |
/** | |
* Constructs an instance with no debugging and disabledEdge as well as disabledRegOutputlogic are false | |
*/ | |
public testSimpleAluGenerator() { | |
this(false, false); | |
} | |
public testSimpleAluGenerator(boolean diableEdge, boolean disableRegOutputLogic) { | |
this.diableEdge=diableEdge; | |
this.disableRegOutputLogic=disableRegOutputLogic; | |
varIdx.put("$Pred_alu.@if0", 1); | |
varIdx.put("$Pred_alu.@switch0.@case0", 2); | |
varIdx.put("$Pred_alu.@switch0.@case1", 3); | |
varIdx.put("$Pred_alu.@switch0.@case2", 4); | |
varIdx.put("$Pred_alu.rst", 5); | |
varIdx.put("alu.$tmp_res_1_OutRead", 6); | |
varIdx.put("alu.@switch0", 7); | |
varIdx.put("alu.clk", 8); | |
varIdx.put("alu.instr", 9); | |
varIdx.put("alu.isZero", 10); | |
varIdx.put("alu.op1", 11); | |
varIdx.put("alu.op2", 12); | |
varIdx.put("alu.res", 13); | |
varIdx.put("alu.rst", 14); | |
} | |
public boolean skipEdge(long local) { | |
long dc = local >>> 16l; | |
// Register was updated in previous delta cylce, that is ok | |
if (dc < deltaCycle) | |
return false; | |
// Register was updated in this delta cycle but it is the same eps, | |
// that is ok as well | |
if ((dc == deltaCycle) && ((local & 0xFFFF) == epsCycle)) | |
return false; | |
// Don't update | |
return true; | |
} | |
private void updateRegs() { | |
for (RegUpdate reg : regUpdates) { | |
switch (reg.internalID) { | |
case 6: | |
alu_tmp_res_1_OutRead = alu_tmp_res_1_OutRead$reg; break; | |
} | |
} | |
} | |
private final void s000_frame_0004() { | |
long t0 = 0; //const0 | |
long t1 = (t0) & 1; //cast_uint[targetSize=1,currentSize=32] | |
alu_isZero = t1; //Assign value | |
} | |
private final void s001_frame_0002() { | |
long t0 = alu_tmp_res_1_OutRead; //loadInternal[alu.$tmp_res_1_OutRead] | |
alu_res = t0; //Assign value | |
} | |
private final void s001_frame_001E() { | |
long t0 = alu_tmp_res_1_OutRead; //loadInternal[alu.$tmp_res_1_OutRead] | |
long t1 = 0; //const0 | |
long t2 = (t1) & 0x0000001FL; //cast_uint[targetSize=5,currentSize=32] | |
boolean t3 = t0 == t2; //eq | |
_Pred_alu_if0 = t3; //Assign value | |
_Pred_alu_if0_update = timeStamp; //update timestamp | |
} | |
private final void s001_frame_000D() { | |
long t0 = alu_instr; //loadInternal[alu.instr] | |
long t1 = (t0 >> 0) & 0x0000000FL; //bitAccessSingleRange[from=3,to=0] | |
alu_switch0 = t1; //Assign value | |
} | |
private final void s001_frame_0025() { | |
long t0 = alu_rst; //loadInternal[alu.rst] | |
long t1 = 0; //const0 | |
boolean t2 = t0 != t1; //not_eq | |
_Pred_alu_rst = t2; //Assign value | |
_Pred_alu_rst_update = timeStamp; //update timestamp | |
} | |
private final void s003_frame_0021() { | |
//posPredicate[$Pred_alu.@if0] | |
long t1 = 0; //const0 | |
long t2 = (t1) & 1; //cast_uint[targetSize=1,currentSize=32] | |
alu_isZero = t2; //Assign value | |
} | |
private final void s003_frame_000E() { | |
long t0 = alu_switch0; //loadInternal[alu.@switch0] | |
long t1 = 0; //loadConstant[constantIdx=0] | |
boolean t2 = t0 == t1; //eq | |
_Pred_alu_switch0_case0 = t2; //Assign value | |
_Pred_alu_switch0_case0_update = timeStamp; //update timestamp | |
} | |
private final void s003_frame_0013() { | |
long t0 = alu_switch0; //loadInternal[alu.@switch0] | |
long t1 = 1; //loadConstant[constantIdx=0] | |
boolean t2 = t0 == t1; //eq | |
_Pred_alu_switch0_case1 = t2; //Assign value | |
_Pred_alu_switch0_case1_update = timeStamp; //update timestamp | |
} | |
private final void s004_frame_0023() { | |
//negPredicate[$Pred_alu.@if0] | |
long t1 = 1; //const1 | |
long t2 = (t1) & 1; //cast_uint[targetSize=1,currentSize=32] | |
alu_isZero = t2; //Assign value | |
} | |
private final void s005_frame_000F() { | |
//posPredicate[$Pred_alu.@switch0.@case0] | |
alu_clk_update = timeStamp; //isRisingEdge[internalIdx=11] | |
//negPredicate[$Pred_alu.rst] | |
long t3 = alu_op1; //loadInternal[alu.op1] | |
long t4 = alu_op2; //loadInternal[alu.op2] | |
long t5 = (t3 + t4) & 0x0000001FL; //plus[targetSizeWithType=10] | |
long t5_cpy = alu_tmp_res_1_OutRead; //Backup of current value | |
alu_tmp_res_1_OutRead$reg = t5; //Assign value | |
if (t5_cpy!=alu_tmp_res_1_OutRead$reg) | |
regUpdates.add(new RegUpdate(6, 0)); | |
} | |
private final void s005_frame_0018() { | |
//negPredicate[$Pred_alu.@switch0.@case0] | |
//negPredicate[$Pred_alu.@switch0.@case1] | |
long t2 = 1; //const1 | |
long t3 = 1; //const1 | |
boolean t4 = t2 == t3; //eq | |
_Pred_alu_switch0_case2 = t4; //Assign value | |
_Pred_alu_switch0_case2_update = timeStamp; //update timestamp | |
} | |
private final void s006_frame_0014() { | |
//posPredicate[$Pred_alu.@switch0.@case1] | |
alu_clk_update = timeStamp; //isRisingEdge[internalIdx=11] | |
//negPredicate[$Pred_alu.rst] | |
long t3 = alu_op1; //loadInternal[alu.op1] | |
long t4 = alu_op2; //loadInternal[alu.op2] | |
long t5 = (t3 - t4) & 0x0000001FL; //minus[targetSizeWithType=10] | |
long t5_cpy = alu_tmp_res_1_OutRead; //Backup of current value | |
alu_tmp_res_1_OutRead$reg = t5; //Assign value | |
if (t5_cpy!=alu_tmp_res_1_OutRead$reg) | |
regUpdates.add(new RegUpdate(6, 0)); | |
} | |
private final void s007_frame_0019() { | |
//posPredicate[$Pred_alu.@switch0.@case2] | |
alu_clk_update = timeStamp; //isRisingEdge[internalIdx=11] | |
//negPredicate[$Pred_alu.rst] | |
long t3 = alu_op1; //loadInternal[alu.op1] | |
long t4 = alu_op2; //loadInternal[alu.op2] | |
long t5 = (t3 * t4) & 0x000003FFL; //mul[targetSizeWithType=20] | |
long t6 = (t5) & 0x0000001FL; //cast_uint[targetSize=5,currentSize=10] | |
long t6_cpy = alu_tmp_res_1_OutRead; //Backup of current value | |
alu_tmp_res_1_OutRead$reg = t6; //Assign value | |
if (t6_cpy!=alu_tmp_res_1_OutRead$reg) | |
regUpdates.add(new RegUpdate(6, 0)); | |
} | |
private final void s009_frame_000A() { | |
//posPredicate[$Pred_alu.rst] | |
alu_clk_update = timeStamp; //isRisingEdge[internalIdx=11] | |
long t2 = 0; //const0 | |
long t3 = (t2) & 0x0000001FL; //cast_uint[targetSize=5,currentSize=32] | |
long t3_cpy = alu_tmp_res_1_OutRead; //Backup of current value | |
alu_tmp_res_1_OutRead$reg = t3; //Assign value | |
if (t3_cpy!=alu_tmp_res_1_OutRead$reg) | |
regUpdates.add(new RegUpdate(6, 0));//writeInternal[internal=6] | |
long t5 = 0; //const0 | |
//Write to #null | |
} | |
public void initConstants() { | |
epsCycle = 0; | |
timeStamp = (deltaCycle << 16) | epsCycle; | |
} | |
public void run() { | |
epsCycle = 0; | |
deltaCycle = deltaCycle + 1; | |
do { | |
regUpdates.clear(); | |
timeStamp = (deltaCycle << 16) | epsCycle; | |
stage0000(); | |
stage0001(); | |
stage0003(); | |
stage0004(); | |
stage0005(); | |
stage0006(); | |
stage0007(); | |
stage0009(); | |
updateRegs(); | |
epsCycle = epsCycle + 1; | |
} while (!regUpdates.isEmpty() && !disableRegOutputLogic); | |
alu_clk_prev = alu_clk; | |
} | |
public void stage0000(){ | |
s000_frame_0004(); | |
} | |
public void stage0001(){ | |
s001_frame_0002(); | |
s001_frame_001E(); | |
s001_frame_000D(); | |
s001_frame_0025(); | |
} | |
public void stage0003(){ | |
_Pred_alu_if0_fresh = _Pred_alu_if0_update == timeStamp; | |
if (_Pred_alu_if0 && | |
_Pred_alu_if0_fresh) { | |
s003_frame_0021(); | |
} | |
s003_frame_000E(); | |
s003_frame_0013(); | |
} | |
public void stage0004(){ | |
if (!_Pred_alu_if0 && | |
_Pred_alu_if0_fresh) { | |
s004_frame_0023(); | |
} | |
} | |
public void stage0005(){ | |
alu_clk_pos_active = ((alu_clk_prev == 0) || diableEdge) && (alu_clk == 1); | |
alu_clk_pos_handled = skipEdge(alu_clk_update); | |
_Pred_alu_rst_fresh = _Pred_alu_rst_update == timeStamp; | |
_Pred_alu_switch0_case0_fresh = _Pred_alu_switch0_case0_update == timeStamp; | |
_Pred_alu_switch0_case1_fresh = _Pred_alu_switch0_case1_update == timeStamp; | |
if (alu_clk_pos_active && | |
!alu_clk_pos_handled && | |
!_Pred_alu_rst && | |
_Pred_alu_rst_fresh && | |
_Pred_alu_switch0_case0 && | |
_Pred_alu_switch0_case0_fresh) { | |
s005_frame_000F(); | |
} | |
if (!_Pred_alu_switch0_case0 && | |
_Pred_alu_switch0_case0_fresh && | |
!_Pred_alu_switch0_case1 && | |
_Pred_alu_switch0_case1_fresh) { | |
s005_frame_0018(); | |
} | |
} | |
public void stage0006(){ | |
if (alu_clk_pos_active && | |
!alu_clk_pos_handled && | |
!_Pred_alu_rst && | |
_Pred_alu_rst_fresh && | |
_Pred_alu_switch0_case1 && | |
_Pred_alu_switch0_case1_fresh) { | |
s006_frame_0014(); | |
} | |
} | |
public void stage0007(){ | |
_Pred_alu_switch0_case2_fresh = _Pred_alu_switch0_case2_update == timeStamp; | |
if (alu_clk_pos_active && | |
!alu_clk_pos_handled && | |
!_Pred_alu_rst && | |
_Pred_alu_rst_fresh && | |
_Pred_alu_switch0_case2 && | |
_Pred_alu_switch0_case2_fresh) { | |
s007_frame_0019(); | |
} | |
} | |
public void stage0009(){ | |
if (alu_clk_pos_active && | |
!alu_clk_pos_handled && | |
_Pred_alu_rst && | |
_Pred_alu_rst_fresh) { | |
s009_frame_000A(); | |
} | |
} | |
@Override | |
public void setInput(String name, long value, int... arrayIdx) { | |
setInput(getIndex(name), value, arrayIdx); | |
} | |
@Override | |
public void setInput(int idx, long value, int... arrayIdx) { | |
switch (idx) { | |
case 1: | |
_Pred_alu_if0 = value!=0; | |
break; | |
case 2: | |
_Pred_alu_switch0_case0 = value!=0; | |
break; | |
case 3: | |
_Pred_alu_switch0_case1 = value!=0; | |
break; | |
case 4: | |
_Pred_alu_switch0_case2 = value!=0; | |
break; | |
case 5: | |
_Pred_alu_rst = value!=0; | |
break; | |
case 6: | |
alu_tmp_res_1_OutRead = value; | |
break; | |
case 7: | |
alu_switch0 = value; | |
break; | |
case 8: | |
alu_clk = value; | |
break; | |
case 9: | |
alu_instr = value; | |
break; | |
case 10: | |
alu_isZero = value; | |
break; | |
case 11: | |
alu_op1 = value; | |
break; | |
case 12: | |
alu_op2 = value; | |
break; | |
case 13: | |
alu_res = value; | |
break; | |
case 14: | |
alu_rst = value; | |
break; | |
default: | |
throw new IllegalArgumentException("Not a valid index:" + idx); | |
} | |
} | |
@Override | |
public int getIndex(String name) { | |
Integer idx=varIdx.get(name); | |
if (idx==null) | |
throw new IllegalArgumentException("The name:"+name+" is not a valid index"); | |
return idx; | |
} | |
@Override | |
public String getName(int idx) { | |
switch (idx) { | |
case 1: return "$Pred_alu.@if0"; | |
case 2: return "$Pred_alu.@switch0.@case0"; | |
case 3: return "$Pred_alu.@switch0.@case1"; | |
case 4: return "$Pred_alu.@switch0.@case2"; | |
case 5: return "$Pred_alu.rst"; | |
case 6: return "alu.$tmp_res_1_OutRead"; | |
case 7: return "alu.@switch0"; | |
case 8: return "alu.clk"; | |
case 9: return "alu.instr"; | |
case 10: return "alu.isZero"; | |
case 11: return "alu.op1"; | |
case 12: return "alu.op2"; | |
case 13: return "alu.res"; | |
case 14: return "alu.rst"; | |
default: | |
throw new IllegalArgumentException("Not a valid index:" + idx); | |
} | |
} | |
@Override | |
public long getOutputLong(String name, int... arrayIdx) { | |
return getOutputLong(getIndex(name), arrayIdx); | |
} | |
@Override | |
public long getOutputLong(int idx, int... arrayIdx) { | |
switch (idx) { | |
case 1: return _Pred_alu_if0?1:0; | |
case 2: return _Pred_alu_switch0_case0?1:0; | |
case 3: return _Pred_alu_switch0_case1?1:0; | |
case 4: return _Pred_alu_switch0_case2?1:0; | |
case 5: return _Pred_alu_rst?1:0; | |
case 6: return alu_tmp_res_1_OutRead & 0x0000001FL; | |
case 7: return alu_switch0 & 0x0000000FL; | |
case 8: return alu_clk & 1; | |
case 9: return alu_instr & 0x0000000FL; | |
case 10: return alu_isZero & 1; | |
case 11: return alu_op1 & 0x0000001FL; | |
case 12: return alu_op2 & 0x0000001FL; | |
case 13: return alu_res & 0x0000001FL; | |
case 14: return alu_rst & 1; | |
default: | |
throw new IllegalArgumentException("Not a valid index:" + idx); | |
} | |
} | |
@Override | |
public long getDeltaCycle() { | |
return deltaCycle; | |
} | |
@Override | |
public void close() throws Exception{ | |
} | |
@Override | |
public void setFeature(Feature feature, Object value) { | |
switch (feature) { | |
case disableOutputRegs: | |
disableRegOutputLogic = (boolean) value; | |
break; | |
case disableEdges: | |
diableEdge = (boolean) value; | |
break; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"log" | |
"os" | |
"runtime" | |
) | |
func main() { | |
sim := NewSimpleAluGenerator() | |
fmt.Println("#Ready") | |
scanner := bufio.NewScanner(os.Stdin) | |
var ( | |
idx, offset int | |
value int64 | |
) | |
for scanner.Scan() { | |
line := scanner.Text() | |
switch line[0] { | |
case '#': | |
continue | |
case 'a': // assert | |
switch line[1] { | |
case 'n': | |
fmt.Sscanf(line[2:], "%d %x", &idx, &value) | |
assert(value == sim.GetOutput(idx)) | |
fmt.Println(">an") | |
case 'a': | |
fmt.Sscanf(line[2:], "%d %d %x", &idx, &offset, &value) | |
assert(value == sim.GetOutput(idx, offset)) | |
fmt.Println(">aa") | |
} | |
case 'd': | |
switch line[1] { | |
case 'j': | |
fmt.Println(">dj", sim.GetJsonDesc()) | |
case 'c': | |
fmt.Printf(">dc %d\n", sim.GetDeltaCycle()) | |
case 'e': | |
fmt.Sscanf(line[2:], "%d", &idx) | |
sim.SetDisableEdge(idx > 0) | |
fmt.Printf("#set disabled edges to %v\n", idx > 0) | |
fmt.Println(">de") | |
case 'r': | |
fmt.Sscanf(line[2:], "%d", &idx) | |
sim.SetDisableRegOutputLogic(idx > 0) | |
fmt.Printf("#setdisabled register output logic to %v\n", idx > 0) | |
fmt.Println(">dr") | |
} | |
case 'g': // get | |
switch line[1] { | |
case 'n': | |
fmt.Sscanf(line[2:], "%d", &idx) | |
fmt.Printf(">gn %d %x\n", idx, sim.GetOutput(idx)) | |
case 'a': | |
fmt.Sscanf(line[2:], "%d %d", &idx, &offset) | |
fmt.Printf(">ga %d %d %x\n", idx, offset, sim.GetOutput(idx, offset)) | |
} | |
case 'i': // init | |
switch line[1] { | |
case 'c': | |
sim.InitConstants() | |
fmt.Println(">ic") | |
} | |
case 'r': // run | |
switch line[1] { | |
case 'r': | |
sim.Run() | |
fmt.Println(">rr") | |
} | |
case 's': // set | |
switch line[1] { | |
case 'n': | |
fmt.Sscanf(line[2:], "%d %x", &idx, &value) | |
fmt.Printf("#Setting %d to %x\n", idx, value) | |
sim.SetInput(idx, value) | |
fmt.Println(">sn") | |
case 'a': | |
fmt.Sscanf(line[2:], "%d %d %x", &idx, &offset, &value) | |
fmt.Printf("#Setting %d[%d] to %x\n", idx, offset, value) | |
sim.SetInput(idx, value, offset) | |
fmt.Println(">sa") | |
} | |
case 'x': | |
switch line[1] { | |
case 'n': | |
fmt.Println(">xn") | |
os.Exit(0) | |
case 'e': | |
fmt.Println(">xe") | |
os.Exit(1) | |
} | |
} | |
} | |
if err := scanner.Err(); err != nil { | |
fmt.Fprintf(os.Stderr, "Error during Scan(): %s\n", err) | |
os.Exit(-1) | |
} | |
os.Exit(2) | |
} | |
func assert(expr bool) { | |
if !expr { | |
_, file, line, _ := runtime.Caller(1) | |
log.Fatalf("assert failed on <%s:%d>\n", file, line) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment