-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfrog_maze.tlv
283 lines (271 loc) · 13.2 KB
/
frog_maze.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
\m4_TLV_version 1d: tl-x.org
\SV
m4_include_lib(['https://raw.githubusercontent.com/stevehoover/tlv_lib/db48b4c22c4846c900b3fa307e87d9744424d916/fundamentals_lib.tlv'])
m4_makerchip_module // (Expanded in Nav-TLV pane.)
/* verilator lint_on WIDTH */
\TLV frog_maze(/_top, _where)
/_top
|pipe
@1
$reset = *reset;
m4_define(['M4_MAZE_NAME'], ['dev']) // original, dev
m4+ifelse(M4_MAZE_NAME, ['original'],
\TLV
m4_define_hier(M4_YY, 24, 0)
m4_define_hier(M4_XX, 38, 0)
m4_define(M4_FROG_START_XX, 1)
m4_define(M4_FROG_START_YY, M4_YY_MAX-2)
\SV_plus
logic [M4_YY_RANGE][M4_XX_RANGE] maze;
assign maze = '{
38'b11111111111111111111111111111111111111,
38'b10010010000110001000000000100100000000,
38'b10001000000000000000000000000001100000,
38'b10000000010000000011001000001000000101,
38'b10000000000001100110000100000000000001,
38'b11001000010010000000000000000001000001,
38'b10001100000001000000000110011011010011,
38'b10000010000001000001100100000000000001,
38'b10001001001010011001100000000001000001,
38'b10000000000000000000000001001000100101,
38'b10010001000000000000001001001100000001,
38'b10000000010010010000000000000000000011,
38'b10000000000000000100100000000100100001,
38'b10010100100000000000010010100000010001,
38'b10000000100101001000000000000000000001,
38'b10000100000000000110010000000000010001,
38'b10001100000001000000001010010011000001,
38'b10001000100110100100000000001001000001,
38'b11000001000000000001000100000000001001,
38'b10000000000000000011000000100000000001,
38'b10011000001100110000001000110000000001,
38'b10000000000000000000000000000000001001,
38'b10000000110000010010000000001010100011,
38'b11111111111111111111111111111111111111
};
,
\TLV
// Default small maze for development.
m4_define_hier(M4_YY, 11, 0)
m4_define_hier(M4_XX, 10, 0)
m4_define(M4_FROG_START_XX, 1)
m4_define(M4_FROG_START_YY, M4_YY_MAX-2)
\SV_plus
logic [M4_YY_RANGE][M4_XX_RANGE] maze;
assign maze = '{
10'b1111111111,
10'b1000000101,
10'b1000000001,
10'b1000000000,
10'b1100110000,
10'b1000010001,
10'b1000000011,
10'b1000000001,
10'b1000000001,
10'b1000000001,
10'b1111111111
};
)
m4_define(M4_UP, 2'b00)
m4_define(M4_DOWN, 2'b10)
m4_define(M4_LEFT, 2'b11)
m4_define(M4_RIGHT, 2'b01)
// 1 while solving until the frog location knows where to go.
$solved = /yy[/frog$Yy]/xx[/frog$Xx]$Solved;
/M4_YY_HIER
/M4_XX_HIER
$wall = *maze\[M4_YY_MAX - #yy\]\[M4_XX_MAX - #xx\];
// Can the upper-left corner of the frog be at these coordinates?
$FrogOk <= (! $wall) &&
( ! (/yy/xx[(#xx + 1) % M4_XX_CNT]$wall)) &&
( ! (/yy[(#yy + 1) % M4_YY_CNT]/xx$wall)) &&
( ! (/yy[(#yy + 1) % M4_YY_CNT]/xx[(#xx + 1) % M4_XX_CNT]$wall));
// $Solved: 1 once it is known how to get the frog to the exit from this location.
// $Dir: directiton to solve from this space if solved. Otherwise, 00, or 11 for exit location.
$Solved <= $next_solved;
$Dir[1:0] <= $next_dir;
{$next_solved, $next_dir[1:0]} =
// On reset, we have a solution for all FrogOk positions (presumably one) that are at the edge.
|pipe$reset ? ($FrogOk && (#xx == 0 || #yy == 0 || #xx >= M4_XX_MAX-1 || #yy >= M4_YY_MAX-1) ? {1'b1, #xx == 0 ? 2'b11 : #yy == 0 ? 2'b00 : #xx >= M4_XX_MAX-1 ? 2'b01 : 2'b10} : {1'b0, 2'b00}
) :
! $FrogOk ? {1'b0, 2'b00} :
$Solved ? {1'b1, $Dir} :
// solve by going left?
(/yy/xx[(#xx + M4_XX_CNT - 2) % M4_XX_CNT]$Solved ||
(! /yy/xx[(#xx + M4_XX_CNT - 2) % M4_XX_CNT]$FrogOk && /yy/xx[(#xx + M4_XX_CNT - 1) % M4_XX_CNT]$Solved)
) ? {1'b1, M4_LEFT} :
// solve by going right?
(/yy/xx[(#xx + 2) % M4_XX_CNT]$Solved ||
(! /yy/xx[(#xx + 2) % M4_XX_CNT]$FrogOk && /yy/xx[(#xx + 1) % M4_XX_CNT]$Solved)
) ? {1'b1, M4_RIGHT} :
// solve by going up?
(/yy[(#yy + M4_YY_CNT - 2) % M4_YY_CNT]/xx$Solved ||
(! /yy[(#yy + M4_YY_CNT - 2) % M4_YY_CNT]/xx$FrogOk && /yy[(#yy + M4_YY_CNT - 1) % M4_YY_CNT]/xx$Solved)
) ? {1'b1, M4_UP} :
// solve by going down?
(/yy[(#yy + 2) % M4_YY_CNT]/xx$Solved ||
(! /yy[(#yy + 2) % M4_YY_CNT]/xx$FrogOk && /yy[(#yy + 1) % M4_YY_CNT]/xx$Solved)
) ? {1'b1, M4_DOWN} :
{1'b0, 2'b00};
/frog
$reset = |pipe$reset;
// Make one hop per cycle in the following direction.
$dir[1:0] = |pipe/yy[$Yy]/xx[$Xx]$Dir;
// Determine the +1 and +2 position of the frog based on $dir.
$hop1_x[M4_XX_INDEX_RANGE] =
$dir == M4_RIGHT ? $Xx + 1 :
$dir == M4_LEFT ? $Xx - 1 :
$Xx;
$hop1_y[M4_YY_INDEX_RANGE] =
$dir == M4_DOWN ? $Yy + 1 :
$dir == M4_UP ? $Yy - 1 :
$Yy;
$hop2_x[M4_XX_INDEX_RANGE] =
$dir == M4_RIGHT ? $Xx + 2 :
$dir == M4_LEFT ? $Xx - 2 :
$Xx;
$hop2_y[M4_YY_INDEX_RANGE] =
$dir == M4_DOWN ? $Yy + 2 :
$dir == M4_UP ? $Yy - 2 :
$Yy;
// Hop by 0, 1 or 2.
$hop1_ok = |pipe/yy[$hop1_y]/xx[$hop1_x]$FrogOk;
$hop2_ok = |pipe/yy[$hop2_y]/xx[$hop2_x]$FrogOk;
$Xx[M4_XX_INDEX_RANGE] <=
$reset || ! |pipe$solved ? M4_FROG_START_XX :
$hop2_ok ? $hop2_x :
$hop1_ok ? $hop1_x :
$RETAIN;
$Yy[M4_YY_INDEX_RANGE] <=
$reset || ! |pipe$solved ? M4_FROG_START_YY :
$hop2_ok ? $hop2_y :
$hop1_ok ? $hop1_y :
$RETAIN;
// Assert these to end simulation (before Makerchip cycle limit).
|pipe
@1
/frog
$done = ($Xx == 0 || $Yy == 0 || $Xx >= M4_XX_MAX-1 || $Yy >= M4_YY_MAX-1);
// Visualization
\viz_js
box: {strokeWidth: 0},
where: {_where}
|pipe
@1
\viz_js
strokeWidth: 0,
// Board background
template() {
//debugger
let objects = {}
let TILE_SIZE = 2
for (let x = 0; x < M4_XX_HIGH; x = x + TILE_SIZE) {
for (let y = 0; y < M4_YY_HIGH; y = y + TILE_SIZE) {
objects[`b_${x}_${y}`] = ["Rect",
{left: x * 10,
top: y * 10,
width: (x + TILE_SIZE > M4_XX_HIGH ? M4_XX_HIGH - x : TILE_SIZE) * 10,
height: (y + TILE_SIZE > M4_YY_HIGH ? M4_YY_HIGH - y : TILE_SIZE) * 10,
fill: (((x + y) % (TILE_SIZE * 2)) == 0) ? "#102020" : "#1C2C2C",
strokeWidth: 0,
}
]
}
}
return objects
}
/M4_YY_HIER
\viz_js
box: {height: 10, strokeWidth: 0},
layout: {top: 10}
/M4_XX_HIER
\viz_js
layout: {left: 10},
box: {
width: 10, height: 10,
fill: "#A030A0",
strokeWidth: 0,
visible: false,
}, // (TODO: arrowhead is outside the box, which is bad form.)
template: {
arrowhead: ["Triangle",
{left: 10, top: 10,
width: 6, height: 6,
originX: "center", originY: "center",
fill: "gray",
visible: false,
}
]
},
render() {
this.getBox().set({visible: '$wall'.asBool()})
this.getObjects().arrowhead.set({visible: '$Solved'.asBool() && ! '|pipe$solved'.asBool(),
angle: '$Dir'.asInt() * 90})
}
/frog
\viz_js
box: {width: 10 * M4_XX_HIGH, height: 10 * M4_YY_HIGH, strokeWidth: 0},
init() {
//debugger
let frog_circle = new fabric.Circle(
{originX: "center", originY: "center",
radius: 10,
fill: "#ffffff10"
}
)
frog_image = this.newImageFromURL(
"https://raw.githubusercontent.com/stevehoover/makerchip_examples/master/viz_imgs/frog.png",
"",
{originX: "center", originY: "center",
left: 0, top: 0,
scaleX: 0.03, scaleY: 0.03,
angle: -7,
}
)
let frog = new fabric.Group([frog_circle, frog_image],
{originX: "center", originY: "center",
angle: 0,
width: 20, height: 20,
})
return {frog}
},
render() {
//debugger
// Keep count of the number of renders so we can terminate an animation after another is started.
this.render_cnt = this.render_cnt ? this.render_cnt++ : 1
render_cnt = this.render_cnt
// Determine old and new angles and adjust to make sure frog rotates no more than 180.
let old_angle = this.getInitObjects().frog.angle
let new_angle = '>>1$dir'.asInt() * 90
if (old_angle == 0 && new_angle == 270) {
old_angle = 360
} else if (old_angle == 270 && new_angle == 0) {
old_angle = -90
}
this.getInitObjects().frog.set({angle: old_angle})
// Animate
this.getInitObjects().frog.animate(
{angle: new_angle},
{onChange: this.global.canvas.renderAll.bind(this.global.canvas),
onComplete: () => {
if (render_cnt == this.render_cnt) {
// Keep animating. Jump.
this.getInitObjects().frog.animate(
{left: ('$Xx'.asInt() + 1) * 10, top: ('$Yy'.asInt() + 1) * 10},
{onChange: this.global.canvas.renderAll.bind(this.global.canvas),
duration: '>>1$hop2_ok'.asBool() ? 400 : '>>1$hop1_ok'.asBool() ? 200 : 0
}
)
} else {
console.log("render canceled")
}
}
}
)
}
\TLV
m4+frog_maze(/maze)
*passed = *cyc_cnt > 10 && /maze|pipe/frog>>2$done;
*failed = 1'b0;
\SV
endmodule