summaryrefslogtreecommitdiff
path: root/bytepusher.scm
blob: e822073bf92f7cd1c1127a8f40b96b8c5bccaae6 (plain)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
(define-module (bytepusher)
  #:use-module (ice-9 binary-ports)
  #:use-module (rnrs bytevectors)
  #:use-module (sdl2 events)
  #:use-module (sdl2 image)
  #:use-module (sdl2 rect)
  #:use-module (sdl2 render)
  #:use-module (sdl2 surface)
  #:use-module (sdl2 video)
  #:use-module (sdl2)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-60))

(define width  256)
(define height 256)
(define scale 2)

(define key-ids (make-hash-table 16))
(hashq-set! key-ids 'x 0)
(hashq-set! key-ids '1 1)
(hashq-set! key-ids '2 2)
(hashq-set! key-ids '3 3)
(hashq-set! key-ids 'q 4)
(hashq-set! key-ids 'w 5)
(hashq-set! key-ids 'e 6)
(hashq-set! key-ids 'a 7)
(hashq-set! key-ids 's 8)
(hashq-set! key-ids 'd 9)
(hashq-set! key-ids 'z #xA)
(hashq-set! key-ids 'c #xB)
(hashq-set! key-ids '4 #xC)
(hashq-set! key-ids 'r #xD)
(hashq-set! key-ids 'f #xE)
(hashq-set! key-ids 'v #xF)

(define memory (make-bytevector (* 1024 1024 16)))


;; key control ----
(define (get-keys)
  (+ (ash (get-byte (+ 1 0))  0)
     (ash (get-byte      0)   8)))

(define (set-keys! keys)
  (set-byte! 1 (ash keys 0))
  (set-byte! 0 (ash keys -8)))

(define (press-key! index)
  (set-keys! (copy-bit index (get-keys) #t)))

(define (release-key! index)
  (set-keys! (copy-bit index (get-keys) #f)))
;; ---- key control


;; memory ----

(define (get-pc)
  (get-addr 2))

(define (set-pc! value)
  (set-byte! 4 (ash value 0))
  (set-byte! 3 (ash value -8))
  (set-byte! 2 (ash value -16)))

(define (get-addr pos)
  (+ (ash (get-byte (+ 2 pos))  0)
     (ash (get-byte (+ 1 pos))  8)
     (ash (get-byte      pos)  16)))

(define (get-initial-pixel)
  (ash (get-byte 5) 16))

(define (get-sample)
  (bytevector-u16-ref memory 6 (endianness big)))

(define (get-byte n)
  (bytevector-u8-ref memory n))

(define (set-byte! n v)
  (bytevector-u8-set! memory n (logand v #xFF)))

;; ---- memory


;; colors ----

; Only used to set the colormap, use the faster version is below
(define (pixel->color pixel)
  (if (> pixel 216)
    0
    (let* ((r (modulo pixel 6))
           (g (modulo (/ (- pixel (modulo pixel  6))  6) 6))
           (b (modulo (/ (- pixel (modulo pixel 36)) 36) 6)))
      (+ #xFF (ash (* #x33 (+ (ash r 16) (ash g 8) b)) 8)))))

(define colormap
  (uint-list->bytevector
    (map pixel->color (iota 256))
    (endianness big)
    4))

(define (pixel->color-fast pixel)
  (bytevector-u32-ref colormap (* 4 pixel) (endianness big)))

;; ---- colors


;; debug ----

(define (peek-memory start end)
  (for-each (lambda (i) (display "0x")
              (display (number->string i 16))
              (newline))
            (take (drop (bytevector->u8-list memory) start) (- end start))))

(define (display-state)
  (sdl-init)
  (call-with-window
    (make-window #:title "BytePusher"
                 #:size (list (* scale width) (* scale height)))
    (lambda (w)
      (call-with-renderer
        (make-renderer w)
        (lambda (renderer)
          (set-renderer-scale! renderer scale scale)
          (let ((texture (make-display-texture renderer)))
            (clear-renderer renderer)
            (load-display! texture)
            (render-copy renderer texture)
            (present-renderer renderer)
            (sleep 10))))))
  (sdl-quit))
;; ---- debug


;; rendering ----
(define (get-pixels)
  (let* ((initial   (get-initial-pixel))
         (len       (* width height))
         (base      (make-bytevector len))
         (final     (make-bytevector (* 4 len))))
    (bytevector-copy! memory initial base 0 len)
    (let loop ((i 0))
      (bytevector-u32-set! final
                           (* 4 i)
                           (pixel->color-fast (bytevector-u8-ref base i))
                           (endianness big))
      (unless (= i (- len 1)) (loop (+ 1 i))))
      final))

(define (make-display-texture renderer)
  (make-texture renderer 'argb8888 'streaming width height))

(define (load-display! texture)
  (update-texture texture
                  (make-rect 0 0 width height)
                  (get-pixels)
                  (* width 4)))
;; ---- rendering


;; main operation ----

(define (load-program! path)
  (call-with-input-file path
    (lambda (p)
      (let ((program (get-bytevector-all p)))
        (bytevector-copy! program 0 memory 0 (bytevector-length program))))))

(define (execute! instruction-addr)
  (call-with-values
    (lambda () (load-instruction instruction-addr))
    (lambda (a b c)
      (set-byte! b (get-byte a))
      (set-pc! c))))

(define (load-instruction instruction-addr)
  (let ((a (get-addr (+ 0 instruction-addr)))
        (b (get-addr (+ 3 instruction-addr)))
        (c (get-addr (+ 6 instruction-addr))))
      #;(display (string-append "a: " (number->string a 16) "  "))
      #;(display (string-append "b: " (number->string b 16) "  "))
      #;(display (string-append "c: " (number->string c 16) "\n"))
    (values a b c)))

(define (loop-frame!)
  (let loop ((count 65535))
    (execute! (get-pc))
    (unless (= count 0) (loop (- count 1)))))

(define (handle-key! action! key)
  (let ((index (hashq-ref key-ids key)))
    (when index (action! index))))

(define (handle-events!)
  (let ((event (poll-event)))
    (cond
      ((keyboard-down-event? event)
       (handle-key! press-key!   (keyboard-event-key event)))
      ((keyboard-up-event? event)
       (handle-key! release-key! (keyboard-event-key event)))
      ((quit-event? event)
       (exit 0)))
    (when event (handle-events!))))

(define (loop! renderer)
  (define texture (make-display-texture renderer))
  (set-renderer-scale! renderer scale scale)
  (let loop ()
    (let ((t0 (sdl-ticks)))
      (handle-events!)
      (loop-frame!)
      (clear-renderer renderer)
      (load-display! texture)
      (render-copy renderer texture)
      (present-renderer renderer)
      (let* ((t1         (sdl-ticks))
             (frame-time (exact->inexact 1000/60))
             (max-time   (+ t0 frame-time)))
        (when (> max-time t1)
          (usleep (inexact->exact (floor (* 1000 (- max-time t1))))))
        (display (string-append
                   "FPS: "
                   (number->string
                     (exact->inexact (/ 1000 (- (sdl-ticks) t0))))
                   "("
                   (number->string
                     (exact->inexact (/ 1000 (- t1 t0))))
                  ")\n"))))
    (loop)))

(define (main . args)
  (load-program! "tests/palette.bp")
  (sdl-init)
  (call-with-window
    (make-window #:title "BytePusher"
                 #:size (list (* scale width) (* scale height)))
    (lambda (w)
      (call-with-renderer (make-renderer w '(accelerated)) loop!)))
  (sdl-quit))

;; ---- main operation


;; tests ----
(define (test-pc)
  (for-each
    (lambda (addr)
      (set-pc! addr)
      (when (not (= (get-pc) addr))
        (error "Error in set pc")))
    (iota #x1000000)))
;; ---- tests