diff clojure/com/aurellem/run/image.clj @ 492:716752719a78

fleshing out image color calibration code.
author Robert McIntyre <rlm@mit.edu>
date Thu, 07 Jun 2012 22:52:30 -0500
parents 2304906d443b
children 783a09c84a28
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/run/image.clj	Thu May 24 17:33:25 2012 -0500
     1.2 +++ b/clojure/com/aurellem/run/image.clj	Thu Jun 07 22:52:30 2012 -0500
     1.3 @@ -2,7 +2,7 @@
     1.4    (:use (com.aurellem.gb saves gb-driver util constants
     1.5                           items vbm characters money
     1.6                           rlm-assembly))
     1.7 -  (:use (com.aurellem.run util title save-corruption
     1.8 +  (:use (com.aurellem.run util music title save-corruption
     1.9                            bootstrap-0 bootstrap-1))
    1.10    (:require clojure.string)
    1.11    (:import [com.aurellem.gb.gb_driver SaveState])
    1.12 @@ -113,44 +113,94 @@
    1.13  (def OAM 0xFE00)
    1.14  
    1.15  
    1.16 -(def bg-pallet-select 0xFF68)
    1.17 -(def bg-pallet-data   0xFF69)
    1.18 -
    1.19 -(def obj-palette-select 0xFF6A)
    1.20 -(def obj-palette-data   0xFF6B)
    1.21 -
    1.22 -
    1.23  
    1.24  (def video-bank-select-register 0xFF4F)
    1.25  
    1.26 -
    1.27 -
    1.28 -(defn gb-rgb->bits [r g b]
    1.29 -  (assert (< 0 r 32))
    1.30 -  (assert (< 0 g 32))
    1.31 -  (assert (< 0 b 32))
    1.32 +(defn gb-rgb->bits [[r g b]]
    1.33 +  (assert (<= 0 r 31))
    1.34 +  (assert (<= 0 g 31))
    1.35 +  (assert (<= 0 b 31))
    1.36    [(bit-and
    1.37      0xFF
    1.38      (+
    1.39       r
    1.40       (bit-shift-left g 5)))
    1.41 -
    1.42     (+
    1.43      (bit-shift-right g 3)
    1.44      (bit-shift-left  b 2))])
    1.45 -      
    1.46 -      
    1.47 -   
    1.48  
    1.49 -  )
    1.50 - 
    1.51 +
    1.52 +(def bg-palette-select 0xFF68)
    1.53 +(def bg-palette-data   0xFF69)
    1.54 +
    1.55 +(def obj-palette-select 0xFF6A)
    1.56 +(def obj-palette-data   0xFF6B)
    1.57 +
    1.58 +(def max-palettes 8)
    1.59 +
    1.60 +(defn write-data [target data]
    1.61 +    (flatten
    1.62 +     [0x3E ;; load literal to A
    1.63 +      data
    1.64 +      0xEA ;; load A into target
    1.65 +      (disect-bytes-2 target)]))
    1.66 +
    1.67 +(defn begin-sequential-palette-write
    1.68 +  [palette-num palette-select-address]
    1.69 +  (assert (<= 0 palette-num max-palettes))
    1.70 +  (assert
    1.71 +   (or (= palette-select-address bg-palette-select)
    1.72 +       (= palette-select-address obj-palette-select)))
    1.73 +  (let [palette-write-data
    1.74 +        (Integer/parseInt
    1.75 +         (str "1" ;; auto increment
    1.76 +              "0" ;; not used
    1.77 +              (format
    1.78 +               "%03d"
    1.79 +               (Integer/parseInt
    1.80 +                (Integer/toBinaryString palette-num) 10))
    1.81 +              "00" ;; color num
    1.82 +              "0"  ;; H/L
    1.83 +              ) 2)]
    1.84 +    (write-data palette-select-address palette-write-data)))
    1.85 +        
    1.86 +(defn set-palettes [palette-select palette-data palettes]
    1.87 +  (assert (<= (count palettes)) max-palettes)
    1.88 +  (flatten 
    1.89 +   [(begin-sequential-palette-write 0 palette-select)
    1.90 +    (map (partial write-data palette-data)
    1.91 +         (flatten (map gb-rgb->bits palettes)))]))
    1.92 +
    1.93  (defn display-one-color
    1.94    "Displayes a single color onto the gameboy screen. input rgb in
    1.95     gameboy rgb."
    1.96 -  [r g b]
    1.97 +  [[r g b]]
    1.98 +  ;; construct a kernel that displays a single color
    1.99 +  (let [palettes (repeat 8 [r g b])
   1.100 +        kernel-address 0xC000
   1.101 +        kernel
   1.102 +        (flatten
   1.103 +         [0xF3 ;; disable interrupts
   1.104 +          (frame-metronome)
   1.105 +          (set-palettes
   1.106 +           obj-palette-select
   1.107 +           obj-palette-data
   1.108 +           palettes)
   1.109 +          (set-palettes
   1.110 +           bg-palette-select
   1.111 +           bg-palette-data
   1.112 +           palettes)
   1.113 +          (infinite-loop)])]
   1.114 +    (-> (set-memory-range (second (music-base))
   1.115 +                          kernel-address kernel)
   1.116 +        (PC! kernel-address))))
   1.117 +
   1.118 +
   1.119 +
   1.120    
   1.121 +(defn write-palette-color [palette-num r g b]
   1.122 +  (let [[byte-1 byte-2] (gb-rgb->bits r g b)]
   1.123  
   1.124  
   1.125 +    ))
   1.126  
   1.127 -
   1.128 -  )
   1.129 \ No newline at end of file