-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlife_viz.tlv
176 lines (147 loc) · 6.36 KB
/
life_viz.tlv
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
\m4_TLV_version 1d: tl-x.org
\SV
// --------------------------------------------------------------------
//
// This example implements Conway's Game of Life.
// In this "game", a grid of cells are born and die based on the
// number of live neighbors they have in each step (clock cycle).
// A cell's neighbors are the surrounding 8 cells, which includes the
// diagonals.
// - A cell is born if exactly 3 neighbors are alive.
// - A cell dies from overcrowding or starvation if it have >3 or <2
// neighbors.
//
// Output shows the grid in each step of simulation.
//
// There is support here for multiple boards simulated simultaneously and
// mirrored symmetry.
//
// --------------------------------------------------------------------
m4_makerchip_module
// -------------------------
// Parameters
// Board size
m4_define_hier(['M4_XX'], 10, 0)
m4_define_hier(['M4_YY'], 10, 0)
/* verilator lint_off UNOPTFLAT */ // To silence Verilator warnings.
// Provide $init_alive to initialize to random 3x (8-section) mirroring.
\TLV snowflake_init()
|default
@1
/M4_YY_HIER
/M4_XX_HIER
// -----------
// Am I alive?
\SV_plus
localparam mirrored_x = (#xx >= (M4_XX_CNT / 2)) ? (M4_XX_MAX - #xx) \: #xx;
localparam mirrored_y = (#yy >= (M4_YY_CNT / 2)) ? (M4_YY_MAX - #yy) \: #yy;
localparam swap = mirrored_x > mirrored_y;
localparam ind_x = swap ? mirrored_y : mirrored_x;
localparam ind_y = swap ? mirrored_x : mirrored_y;
$init_alive = /yy[ind_y]/xx[ind_x]$rand;
\TLV life(/_top, _where)
/_top
\viz_js
box: {strokeWidth: 0},
where: {_where}
|default
// ======
// Design
// ======
@1
$reset = *reset;
/M4_YY_HIER
/M4_XX_HIER
// Cell logic
// -----------
// Population count ($cnt) of 3x3 square (with edge logic).
// Sum left + me + right.
$row_cnt[1:0] = {1'b0, (/xx[(xx + M4_XX_CNT-1) % M4_XX_CNT]>>1$alive & (xx > 0))} +
{1'b0, >>1$alive} +
{1'b0, (/xx[(xx + 1) % M4_XX_CNT]>>1$alive & (xx < M4_XX_CNT-1))};
// Sum three $row_cnt's: above + mine + below.
$cnt[3:0] = {2'b00, (/yy[(yy + M4_YY_CNT-1) % M4_YY_CNT]/xx$row_cnt & {2{(yy > 0)}})} +
{2'b00, $row_cnt[1:0]} +
{2'b00, (/yy[(yy + 1) % M4_YY_CNT]/xx$row_cnt & {2{(yy < M4_YY_CNT-1)}})};
// ----------
// Init state
//m4_rand($init_alive, 0, 0, (yy * xx) ^ ((3 * xx) + yy))
// -----------
// Am I alive?
$alive = |default$reset ? $init_alive : // init
>>1$alive ? ($cnt >= 3 && $cnt <= 4) : // stay alive
($cnt == 3); // born
// ==================
// Embedded Testbench
// ==================
//
// Declare success when total live cells was above 25% and remains below 6.25% for 20 cycles.
// Count live cells through accumulation, into $alive_cnt.
// Accumulate right-to-left, then bottom-to-top through >yy[0].
/tb
@2
/M4_YY_HIER
/M4_XX_HIER
$right_alive_accum[10:0] = (xx < M4_XX_MAX) ? /xx[xx + 1]$horiz_alive_accum : 11'b0;
$horiz_alive_accum[10:0] = $right_alive_accum + {10'b0, |default/yy/xx$alive};
$below_alive_accum[21:0] = (yy < M4_YY_MAX) ? /yy[yy + 1]$vert_alive_accum : 22'b0;
$vert_alive_accum[21:0] = $below_alive_accum + {11'b0, /xx[0]$horiz_alive_accum};
$alive_cnt[21:0] = /yy[0]$vert_alive_accum;
$above_min_start = $alive_cnt > (M4_XX_CNT * M4_YY_CNT) >> 2; // 1/4
$below_max_stop = $alive_cnt < (M4_XX_CNT * M4_YY_CNT) >> 4; // 1/16
$start_ok = |default$reset ? 1'b0 : (>>1$start_ok || $above_min_start);
$stop_cnt[7:0] = |default$reset ? 8'b0 :
$below_max_stop ? >>1$stop_cnt + 8'b1 :
8'b0;
$passed = >>1$start_ok && (($alive_cnt == '0) || (>>1$stop_cnt > 8'd20));
// ===
// VIZ
// ===
@1
/M4_YY_HIER
\viz_js
all: {
box: {
width: M4_XX_CNT * 20 + 20,
height: M4_YY_CNT * 20 + 20,
fill: "#505050",
strokeWidth: 0
}
},
where0: {left: 10, top: 10}
/M4_XX_HIER
\viz_js
box: {width: 20, height: 20,
fill: "lightgray",
strokeWidth: 0},
init() {
//debugger
return {
shadow: new fabric.Rect({
width: 4, height: 4,
fill: "lightgray",
left: 8, top: 8
})
}
},
render() {
//debugger
let background = ('$alive'.asBool()) ? "#10D080" : "#204030";
let prev_prop = ('>>1$alive'.asBool()) ? {fill: "#008000", opacity: 0.3} : {fill: null, opacity: 0};
this.getBox().set("fill", background);
this.getObjects().shadow.set(prev_prop);
},
layout: "horizontal",
\TLV
/board_y[0:0] // E.g., [2:0] for 3 boards high.
/board_x[0:0] // E.g., [2:0] for 3 boards across.
\viz_js
layout: "horizontal"
//m4+snowflake_init() // Uncomment to enable "snowflake" symmetry.
m4+life(/life, )
|tb
@2
// Determine passed by looking at top boards only.
*passed = | /top/board_y[0]/board_x[*]/life|default/tb<>0$passed;
\SV
endmodule