-
Notifications
You must be signed in to change notification settings - Fork 0
/
mul.rs
181 lines (144 loc) · 4.47 KB
/
mul.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use std::error::Error;
use std::ops::Deref;
use std::time::{Duration, Instant};
use lc3tools_sys::root::lc3::shims::{noOpInputShim, noOpPrintShim};
use lc3tools_sys::root::lc3::sim as Sim;
use lc3tools_sys::root::{free_sim, get_mem, load_program, run_program, State};
use lc3_isa::{
program,
util::{AssembledProgram, LoadableIterator},
};
use pretty_assertions::assert_eq as eq;
trait LoadProgram {
fn load<L, P>(&mut self, prog: P) -> Result<(), Box<dyn Error>>
where
P: Deref<Target = L>,
for<'l> &'l L: LoadableIterator;
}
impl LoadProgram for Sim {
fn load<L, P>(&mut self, prog: P) -> Result<(), Box<dyn Error>>
where
P: Deref<Target = L>,
for<'l> &'l L: LoadableIterator,
{
for (addr, word) in &*prog {
unsafe { self.setMem(addr, word) }
}
Ok(())
}
}
fn time<R>(func: impl FnOnce() -> R) -> (R, Duration) {
let start = Instant::now();
let res = func();
(res, start.elapsed())
}
fn main() {
#[rustfmt::skip]
let prog_gen = |a: u16, b: u16| program! {
.ORIG #0x3000;
BRnzp @START;
// Calculates a * b
@A .FILL #a;
@B .FILL #b;
@START
AND R0, R0, #0; // R0 as acc
LD R1, @A; // R1 as inc
LD R2, @B; // R2 as count
// TODO: if b is negative, flip the signs of a and b.
// i.e. 3 * -4 → -3 * 4
//
// For now, we'll just do unsigned numbers though.
@LOOP
BRz @END;
ADD R0, R0, R1;
ADD R2, R2, #-1;
BRnzp @LOOP;
@END
ST R0, @RES;
HALT;
.ORIG #0x3020;
@RES .FILL #0;
}.into();
c_interface(&prog_gen);
println!();
#[cfg(feature = "cpp-interface-example")]
cpp_interface(&prog_gen);
}
fn c_interface(prog_gen: &impl Fn(u16, u16) -> AssembledProgram) {
let test = |a: u16, b: u16| {
print!("{:5} x {:5}: ", a, b);
let prog: AssembledProgram = prog_gen(a, b);
let expected = a
.checked_mul(b)
.expect("multiplication does not overflow");
let (mut addrs, mut words) = (Vec::new(), Vec::new());
for (addr, word) in &prog {
addrs.push(addr);
words.push(word);
}
// If these were stable:
// let (addrs, len, _) = addrs.into_raw_parts();
// let (words, _, _) = words.into_raw_parts();
let addrs_ptr = addrs.as_ptr();
let words_ptr = words.as_ptr();
let len = addrs.len();
let sim = unsafe { load_program(len as u16, addrs_ptr, words_ptr) };
drop((addrs, words));
let (state, elapsed) = time(|| unsafe { run_program(sim, 0x3000) });
println!("[in {:?}]", elapsed);
let State { success, .. } = state;
let got = unsafe { get_mem(sim, 0x3020) };
unsafe { free_sim(sim) };
assert!(success);
eq!(expected, got, "Expected `{}`, got `{:?}`.", expected, state);
};
test(0, 0);
test(0, 8);
test(9, 0);
test(1, 1);
test(1, 50);
test(30, 50);
test(6, 7); // → 42
test(1, 65535); // This one has the worst runtime.
}
#[cfg(feature = "cpp-interface-example")]
fn cpp_interface(prog_gen: &impl Fn(u16, u16) -> AssembledProgram) {
let mut printer = Box::new(unsafe { noOpPrintShim() });
let mut input = Box::new(unsafe { noOpInputShim() });
let mut test = |a: u16, b: u16| {
print!("{:5} x {:5}: ", a, b);
let mut sim = Box::new(unsafe {
Sim::new(
&mut printer._base as *mut _,
&mut input._base as *mut _,
true,
0,
false,
)
});
let prog: AssembledProgram = prog_gen(a, b);
let expected = a
.checked_mul(b)
.expect("multiplication does not overflow");
unsafe {
sim.reinitialize();
}
sim.load::<AssembledProgram, _>(&prog).unwrap();
unsafe {
sim.setPC(0x3000);
}
let (success, elapsed) = time(|| unsafe { sim.runUntilHalt() });
assert!(success);
println!("[in {:?}]", elapsed);
let got = unsafe { sim.getMem(0x3020) };
eq!(expected, got, "Expected `{}`, got `{}`.", expected, got);
};
test(0, 0);
test(0, 8);
test(9, 0);
test(1, 1);
test(1, 50);
test(30, 50);
test(6, 7); // → 42
test(1, 65535); // This one has the worst runtime.
}