diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2014-07-11 21:13:26 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2014-07-11 21:13:26 -0700 |
commit | ff8b3afd095b9341dec55f7e54c2b0ae9a989931 (patch) | |
tree | 70ba476f31013819f10390b4d51a43234ed6af81 | |
parent | d00338ee4ae139b284647c367bf69563a5b4767a (diff) | |
download | mu-ff8b3afd095b9341dec55f7e54c2b0ae9a989931.tar.gz |
14 - processor model now has a program counter
-rw-r--r-- | mu.arc | 94 |
1 files changed, 48 insertions, 46 deletions
diff --git a/mu.arc b/mu.arc index b73afa5c..4ed77070 100644 --- a/mu.arc +++ b/mu.arc @@ -12,52 +12,54 @@ (each (name . body) fns (= function*.name body))) -(def run (instrs (o fn-args) (o returned)) - (each instr instrs - (unless returned -;? (prn instr) -;? (prn memory*) - (let delim (or (pos '<- instr) -1) - (with (oarg (if (>= delim 0) - (cut instr 0 delim)) - op (instr (+ delim 1)) - arg (cut instr (+ delim 2))) -;? (prn op " " oarg) - (case op - loadi - (= (memory* oarg.0.1) arg.0) - add - (= (memory* oarg.0.1) - (+ (memory* arg.0.1) (memory* arg.1.1))) - sub - (= (memory* oarg.0.1) - (- (memory* arg.0.1) (memory* arg.1.1))) - mul - (= (memory* oarg.0.1) - (* (memory* arg.0.1) (memory* arg.1.1))) - div - (= (memory* oarg.0.1) - (/ (real (memory* arg.0.1)) (memory* arg.1.1))) - idiv - (= (memory* oarg.0.1) - (trunc:/ (memory* arg.0.1) (memory* arg.1.1)) - (memory* oarg.1.1) - (mod (memory* arg.0.1) (memory* arg.1.1))) - read - (= (memory* oarg.0.1) - ; hardcoded channel for now - (memory* pop.fn-args.1)) - reply - (= returned (annotate 'result arg)) - ; else user-defined function - (let results (run function*.op arg) -;? (prn "== " memory*) - (each o oarg -;? (prn o) - (= (memory* o.1) (memory* pop.results.1)))) - ))))) -;? (prn "return") - rep.returned) +(def run (instrs (o fn-args)) + (ret result nil + (for pc 0 (< pc len.instrs) (++ pc) + (let instr instrs.pc +;? (prn instr) +;? (prn memory*) + (let delim (or (pos '<- instr) -1) + (with (oarg (if (>= delim 0) + (cut instr 0 delim)) + op (instr (+ delim 1)) + arg (cut instr (+ delim 2))) +;? (prn op " " oarg) + (case op + loadi + (= (memory* oarg.0.1) arg.0) + add + (= (memory* oarg.0.1) + (+ (memory* arg.0.1) (memory* arg.1.1))) + sub + (= (memory* oarg.0.1) + (- (memory* arg.0.1) (memory* arg.1.1))) + mul + (= (memory* oarg.0.1) + (* (memory* arg.0.1) (memory* arg.1.1))) + div + (= (memory* oarg.0.1) + (/ (real (memory* arg.0.1)) (memory* arg.1.1))) + idiv + (= (memory* oarg.0.1) + (trunc:/ (memory* arg.0.1) (memory* arg.1.1)) + (memory* oarg.1.1) + (mod (memory* arg.0.1) (memory* arg.1.1))) + read + (= (memory* oarg.0.1) + ; hardcoded channel for now + (memory* pop.fn-args.1)) + reply + (do (= result arg) + (break)) + ; else user-defined function + (let results (run function*.op arg) +;? (prn "== " memory*) + (each o oarg +;? (prn o) + (= (memory* o.1) (memory* pop.results.1)))) + ))))) +;? (prn "return " result) + )) (awhen cdr.argv (each file it |