Skip to content

Commit

Permalink
Lazy commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsso committed Aug 5, 2021
1 parent f0a9d96 commit 71d0ad6
Show file tree
Hide file tree
Showing 12 changed files with 450 additions and 70 deletions.
2 changes: 1 addition & 1 deletion mips/src/ast/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def_stmt!(
(Ceil, "ceil", 2, "reg,num", [R, N]),
(Cos, "cos", 2, "reg,num", [R, N]),
(Div, "div", 3, "reg,num,num", [R, N, N]),
(Exp, "expr", 2, "reg,num", [R, N]),
(Exp, "exp", 2, "reg,num", [R, N]),
(Floor, "floor", 2, "reg,num", [R, N]),
(Log, "log", 2, "reg,num", [R, N]),
(Max, "max", 3, "reg,num,num", [R, N, N]),
Expand Down
81 changes: 40 additions & 41 deletions mips/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,11 @@ impl Mips {
}
}

// Define/alias/tag replacement pass
// Define/alias/tag replacement and comment removal pass
for (_i, line) in mips.lines.iter_mut().enumerate() {
if conf.remove_comments {
line.comment_opt = None;
}
match line {
// Replace defines
Line {
Expand Down Expand Up @@ -465,56 +468,52 @@ impl Mips {
}
}
// Argument and comment replacement pass
if conf.remove_comments {
for line in mips.lines.iter_mut() {
line.comment_opt = None;

for arg in line.stmt.iter_args_mut() {
match arg {
Arg::LineAbs(LineAbs(num)) if conf.remove_tags => {
if let Some(key) = num.as_alias() {
let i = tag_lines.get(key).expect(key);
*num = Num::Lit(*i as f64);
}
for line in mips.lines.iter_mut() {
for arg in line.stmt.iter_args_mut() {
match arg {
Arg::LineAbs(LineAbs(num)) if conf.remove_tags => {
if let Some(key) = num.as_alias() {
let i = tag_lines.get(key).expect(key);
*num = Num::Lit(*i as f64);
}
Arg::Num(num) => {
if let Some(key) = num.as_alias() {
if let Some(alias) = mips.aliases.get(key) {
match alias {
Alias::Num(n) => {
if conf.remove_defines {
*arg = Arg::Num(Num::Lit(*n));
}
}
Alias::Reg(reg_base) => {
if conf.remove_reg_aliases {
*arg = Arg::Reg(Reg::Base(*reg_base));
}
}
Arg::Num(num) => {
if let Some(key) = num.as_alias() {
if let Some(alias) = mips.aliases.get(key) {
match alias {
Alias::Num(n) => {
if conf.remove_defines {
*arg = Arg::Num(Num::Lit(*n));
}
Alias::Dev(_) => {
panic!();
}
Alias::Reg(reg_base) => {
if conf.remove_reg_aliases {
*arg = Arg::Reg(Reg::Base(*reg_base));
}
}
Alias::Dev(_) => {
panic!();
}
}
// *num = Num::Lit()
}
// *num = Num::Lit()
}
Arg::Reg(Reg::Alias { key, .. }) => {
if conf.remove_reg_aliases {
let reg_base = mips.aliases.try_get_reg_base(key).unwrap();
*arg = Arg::Reg(Reg::Base(reg_base));
}
}
Arg::Reg(Reg::Alias { key, .. }) => {
if conf.remove_reg_aliases {
let reg_base = mips.aliases.try_get_reg_base(key).unwrap();
*arg = Arg::Reg(Reg::Base(reg_base));
}
Arg::Dev(Dev::Alias(key)) => {
if conf.remove_dev_aliases {
let dev_base = mips.aliases.try_get_dev_base(key).unwrap();
*arg = Arg::Dev(Dev::Base(dev_base));
}
}
Arg::Dev(Dev::Alias(key)) => {
if conf.remove_dev_aliases {
let dev_base = mips.aliases.try_get_dev_base(key).unwrap();
*arg = Arg::Dev(Dev::Base(dev_base));
}
// Arg::String(key) => {
// }
_ => {}
}
// Arg::String(key) => {
// }
_ => {}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions myps/src/ast/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def_func!(
(Abs, 1, "abs", "expr", [E ]),
(Acos, 1, "acos", "nexpr", [E ]),
(Asin, 1, "asin", "expr", [E ]),
(Atan, 1, "atan", "expr", [E ]),
(Ceil, 1, "ceil", "expr", [E ]),
(Cos, 1, "cos", "expr", [E ]),
(Exp, 1, "exp", "expr", [E ]),
Expand Down
135 changes: 135 additions & 0 deletions myps/test-scripts/elmo/furnace.mips
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
alias WastePump d0
alias CoolingPump d1
alias HeatPump d2
alias WasteAnalyzer d3
alias SPTemp d4
alias SPPressure d5

alias pk r0
alias tk r1
alias tw r2
alias pk1 r3
alias tk1 r4
alias vs r5
alias vo r6
alias vc r7
alias vh r8
alias vw r9
alias tct r10
alias tc r11
alias pp r12
alias th r13

move r13 2500
move r11 150

Start:
yield
l r10 db Setting
lb r0 545937711 Pressure 0
lb r1 545937711 Temperature 0
l r3 d5 Setting
l r4 d4 Setting
l r2 d3 Temperature
l r12 d3 Pressure
sgt r14 r10 r1
select r13 r14 r10 r13
slt r14 r10 r1
select r11 r14 r10 r11
move r10 r13
jal CalcTemp

move r8 r10
move r10 r11
jal CalcTemp

move r7 r10
mul r14 0.9 500
sub r15 r3 r0
mul r14 r14 r15
div r14 r14 r12
sub r14 r14 r8
sub r14 r14 r7
max r5 0 r14
mul r7 0.9 r7
mul r8 0.9 r8
move r10 r13
jal CalcVE

bltz r6 c
bgt r6 100 Scale
bgt r9 100 Scale
add r8 r8 r6
j Continue

c:
move r10 r11
jal CalcVE
bgt r6 100 Scale
bgt r9 100 Scale
add r7 r7 r6

Continue:
min r7 r7 100
min r8 r8 100
min r9 r9 100
add r14 r7 r8
add r5 r14 r9
# vs = vs / OutputFactor # VS factor
sub r14 r0 r3
mul r14 500 r14
mul r15 r12 r5
add r14 r14 r15
mul r6 500 r14
mul r5 r5 r12
mul r14 0.9 r6
mul r15 500 r0
add r15 r15 r5
div r14 r14 r15
min r14 100 r14
max r6 0 r14

lb r14 545937711 On 0
s d0 On r14
s d2 On r14
s d1 On r14

s d0 Setting r9
s d2 Setting r8
s d1 Setting r7

sb 545937711 SettingOutput r6
j Start

Scale:
max r14 r6 r9
div r14 100 r14
mul r5 r5 r14

CalcVE:
mul r14 r10 r5
sub r15 r1 r2
mul r6 r14 r15
sub r14 r10 r2
mul r14 r1 r14
div r6 r6 r14
mul r14 r2 r5
sub r15 r10 r1
mul r9 r14 r15
sub r14 r10 r2
mul r14 r1 r14
div r9 r9 r14
j ra

CalcTemp:
mul r14 500 r10
mul r14 r14 r0
sub r15 r1 r4
mul r14 r14 r15
mul r15 r12 r1
sub r16 r4 r10
mul r15 r15 r16
div r14 r14 r15
max r10 0 r14
j ra

107 changes: 107 additions & 0 deletions myps/test-scripts/elmo/furnace.myps
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
WastePump = d0
CoolingPump = d1
HeatPump = d2
WasteAnalyzer = d3
SPTemp = d4
SPPressure = d5

def InputFactor = 0.9
def OutputFactor = 0.9
def VSMax = 100
def FurnaceHash = 545937711

# alias pk r1
# alias tk r2
# alias tw r3
# alias pk1 r4
# alias tk1 r5
# alias vs r6
# alias vo r7
# alias vc r8
# alias vh r9
# alias vw r10
# alias vo r11
# alias tct r12
# alias tc r13
# alias pp r14
# alias th r15

fix pk, tk, tw, pk1, tk1, vs, vo, vc, vh, vw, tct, tc, pp, th

th = 2500
tc = 150

tag Start:
yield()
tct = db.Setting
pk = FurnaceHash.all.Pressure.0
tk = FurnaceHash.all.Temperature.0
pk1 = SPPressure.Setting
tk1 = SPTemp.Setting
tw = WasteAnalyzer.Temperature
pp = WasteAnalyzer.Pressure
th = (tct > tk) ? tct : th
tc = (tct < tk) ? tct : tc
tct = th
jal(CalcTemp)

vh = tct
tct = tc
jal(CalcTemp)

vc = tct
vs = max(0, InputFactor * 500 * (pk1 - pk) / pp - vh - vc)
vc = InputFactor * vc
vh = InputFactor * vh
tct = th
jal(CalcVE)

bltz(vo, c)
bgt(vo, VSMax, Scale)
bgt(vw, VSMax, Scale)
vh += vo
j(Continue)

tag c:
tct = tc
jal(CalcVE)
bgt(vo, VSMax, Scale)
bgt(vw, VSMax, Scale)
vc += vo

tag Continue:
vc = min(vc, 100)
vh = min(vh, 100)
vw = min(vw, 100)
vs = vc + vh + vw
# vs = vs / OutputFactor # VS factor
vo = 500 * (500 * (pk - pk1) + pp * vs)
vs *= pp
vo = max(0, min(100, OutputFactor * vo / (500 * pk + vs)))

furnaceOn = FurnaceHash.all.On.0
WastePump .On = furnaceOn
HeatPump .On = furnaceOn
CoolingPump .On = furnaceOn

WastePump .Setting = vw
HeatPump .Setting = vh
CoolingPump .Setting = vc

FurnaceHash.all.SettingOutput = vo
j(Start)

tag Scale:
vs *= (VSMax / max(vo, vw))

tag CalcVE:
vo = tct * vs * (tk - tw)
vo = vo / (tk * (tct - tw))
vw = tw * vs * (tct - tk)
vw = vw / (tk * (tct- tw))
j(ra)

tag CalcTemp:
tct = max(0, (500 * tct * pk * (tk - tk1)) / (pp * tk * (tk1 - tct)))
j(ra)

Loading

0 comments on commit 71d0ad6

Please sign in to comment.