1 ! Copyright (C) 2007 Gavin Harrison
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel math sequences kernel.private namespaces arrays io io.files
4 splitting io.binary math.functions vectors quotations sequences.private ;
12 : call-nth ( n array -- )
13 >r >fixnum r> 2dup nth quotation?
14 [ dispatch ] [ "Not a quotation" throw ] if ; inline
16 : reg-val ( m -- n ) regs get nth ;
18 : set-reg ( val n -- ) regs get set-nth ;
20 : arr-val ( index loc -- z )
23 : set-arr ( val index loc -- )
24 arrays get nth set-nth ;
26 : get-op ( num -- op )
27 -28 shift BIN: 1111 bitand ;
29 : get-value ( platter -- register )
32 : >32bit ( m -- n ) HEX: ffffffff bitand ; inline
34 : get-a ( platter -- register )
35 -6 shift BIN: 111 bitand ; inline
37 : get-b ( platter -- register )
38 -3 shift BIN: 111 bitand ; inline
40 : get-c ( platter -- register )
41 BIN: 111 bitand ; inline
43 : get-cb ( platter -- b c ) [ get-c ] keep get-b ;
44 : get-cba ( platter -- c b a ) [ get-cb ] keep get-a ;
45 : get-special ( platter -- register )
46 -25 shift BIN: 111 bitand ; inline
49 get-cba rot reg-val zero? [
55 : binary-op ( quot -- ? )
57 swap >r >r [ reg-val ] 2apply swap r> call r>
61 [ swap arr-val ] binary-op ;
64 get-cba >r [ reg-val ] 2apply r> reg-val set-arr f ;
67 [ + >32bit ] binary-op ;
70 [ * >32bit ] binary-op ;
76 [ bitand HEX: ffffffff swap - ] binary-op ;
78 : new-array ( size location -- )
79 >r 0 <array> r> arrays get set-nth ;
81 : ?grow-storage ( -- )
82 open-arrays get dup empty? [
83 >r arrays get length r> push
90 get-cb >r reg-val open-arrays get pop [ new-array ] keep r>
94 get-c reg-val dup open-arrays get push
95 f swap arrays get set-nth f ;
97 : op10 ( opcode -- ? )
98 get-c reg-val write1 flush f ;
100 : op11 ( opcode -- ? )
103 : op12 ( opcode -- ? )
104 get-cb reg-val dup zero? [
107 arrays get [ nth clone 0 ] keep set-nth
108 ] if reg-val finger set f ;
110 : op13 ( opcode -- ? )
111 [ get-value ] keep get-special set-reg f ;
113 : advance ( -- val opcode )
114 finger get arrays get first nth
115 finger inc dup get-op ;
120 [ op0 ] [ op1 ] [ op2 ] [ op3 ]
121 [ op4 ] [ op5 ] [ op6 ] [ drop t ]
122 [ op8 ] [ op9 ] [ op10 ] [ op11 ]
126 : exec-loop ( bool -- )
127 [ run-op exec-loop ] unless ;
129 : load-platters ( path -- )
130 file-contents 4 group [ be> ] map
131 0 arrays get set-nth ;
135 2 16 ^ <vector> arrays set
137 V{ } clone open-arrays set
140 : run-prog ( path -- )
144 "extra/icfp/2006/sandmark.umz" resource-path run-prog ;