-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPixelGrid.scala
203 lines (160 loc) · 5.5 KB
/
PixelGrid.scala
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
package Core
import Chisel._
import java.io._
import scala.io.Source
// TODO move ALU out of PG
// TODO kernel in should also be moved out at some point
class PixelGrid(data_width: Int, cols: Int, rows: Int) extends Module {
val io = new Bundle {
val data_in = UInt(INPUT, data_width)
val data_out = UInt(OUTPUT, data_width)
}
val pixel_rows = Vec.fill(rows){ Module(new PixelArray(data_width, cols)).io }
val input_tree = Vec.fill(3){ Reg(init=UInt(0, width = data_width)) }
val shift_muxes = for(i <- 0 until 3) yield Module(new ShiftMux3(data_width, 3, default=((i + 1) % 3))).io
val pinger = Module(new Orchestrator(cols, rows)).io
//////////////////////////////////////
/////////// GRID
///////////
///////////
// wire input into first row input tree
for(i <- 0 until cols){
pixel_rows(0).data_in(i%3) := input_tree(i/3)
}
// Wire io between rows
for(i <- 1 until cols/3){
for(j <- 0 until rows){
pixel_rows(i).data_in(j) := pixel_rows(i-1).data_out(j)
}
}
// wire primary mux enablers
pixel_rows(0).ping_read := pinger.pings(1)
pixel_rows(0).ping_mux := pinger.pings(2)
pixel_rows(1).ping_read := pinger.pings(3)
pixel_rows(1).ping_mux := pinger.pings(4)
pixel_rows(2).ping_read := pinger.pings(5)
pixel_rows(2).ping_mux := pinger.pings(6)
// Wire shift signals to secondary muxes
for(i <- 0 until 3){
shift_muxes(i).shift := pinger.pings(0)
}
// Wire data from primary muxes to secondary muxes
for(i <- 0 until 3){
for(j <- 0 until 3){
shift_muxes(i).data_in(j) := pixel_rows(i).data_out(j)
}
}
//////////////////////////////////////
/////////// ALUs
///////////
///////////
val ALUs = Module(new ALUrow(data_width, cols, rows)).io
// Wire memory outputs to ALUs
for(i <- 0 until rows){
ALUs.data_in(i) := shift_muxes(i).data_out
}
io.data_out := ALUs.data_out
// Wire ctrl pings to ALUs
ALUs.accumulator_flush := pinger.pings(8)
ALUs.selector_shift_enable := pinger.pings(7)
// Ghetto kernel shit
val kernel_buffer = Vec.fill(2){ Reg(init=SInt(0, width=data_width)) }
val s0 :: s1 :: s2 :: s3 :: s4 :: s5 :: s6 :: s7 :: s8 :: done :: Nil = Enum(UInt(), 10)
val k_state = Reg(init=UInt(width=data_width))
when(k_state === done){
for(i <- 0 until 3){
input_tree(i) := io.data_in
kernel_buffer(0) := ALUs.kernel_out
}
}
.otherwise{
k_state := k_state + UInt(1)
kernel_buffer(0) := io.data_in
}
kernel_buffer(1) := kernel_buffer(0)
ALUs.kernel_in := kernel_buffer(1)
}
class image(c: PixelGrid, data_width: Int, cols: Int, rows: Int) extends Tester(c) {
import scala.collection.mutable.ListBuffer
val width = 640
val height = 480
val sweep_input_depth = 9
val sweep_output_depth = sweep_input_depth - 2
val sweeps = height/(sweep_output_depth)
val inputs_per_sweep = sweep_input_depth*width
val outputs_per_sweep = sweep_output_depth*width
var first_valid = 30
var total_pixels_collected = 0
var total_pixels_fed = 0
var pixels_fed = 0
var rows_swept = 0
def push_kernel(kernel: Array[Int]) : Unit = {
poke(c.io.data_in, kernel(0))
step(1)
poke(c.io.data_in, kernel(1))
step(1)
poke(c.io.data_in, kernel(2))
step(1)
poke(c.io.data_in, kernel(3))
step(1)
poke(c.io.data_in, kernel(4))
step(1)
poke(c.io.data_in, kernel(5))
step(1)
poke(c.io.data_in, kernel(6))
step(1)
poke(c.io.data_in, kernel(7))
step(1)
poke(c.io.data_in, kernel(8))
step(1)
}
def feed_row(y_offset: Int, img: Array[Array[Int]], conv_img: Array[Array[Int]]) : Unit = {
var count = 0
for(x <- 0 until width){
var collected = new ListBuffer[Int]()
for(y <- 0 until sweep_input_depth){
poke(c.io.data_in, img(y + y_offset)(x))
var out = peek(c.io.data_out).toInt
if(x+(y*9) >= 30){
if(out != 0){
collected += out
}
}
step(1)
}
if(collected.length == 7 && x < width - 2){
for(y <- 0 until 7){
var ty = (y + 6) % 7
var tx = x
if(ty == 6){ tx -= 1 }
conv_img(ty+y_offset)(tx) = collected(y)
}
}
}
}
val flat_array = Source.fromFile("Conv/disaster24dump.txt").getLines.toArray.map(_.toInt)
val img_array = Array.ofDim[Int](height, width)
var convoluted = Array.ofDim[Int](height, width)
for(y <- 0 until height){
for(x <- 0 until width){
img_array(y)(x) = flat_array(x + width*y)
convoluted(y)(x) = 0
}
}
def feed_img(img: Array[Array[Int]], conv_img: Array[Array[Int]]) : Unit = {
for(i <- 0 until sweeps-1){
feed_row(i*7, img, conv_img)
}
}
val kernel = Array[Int](1, 0, 1, 0, -4, 0, 1, 0, 1)
push_kernel(kernel)
// feed_row(0, img_array, convoluted)
feed_img(img_array, convoluted)
import java.io._
val w = new PrintWriter("Conv/big_disaster.txt")
for(y <- 0 until height){
val s = convoluted(y).mkString("\n")
w.write(s)
w.write("\n")
}
}