-
Notifications
You must be signed in to change notification settings - Fork 0
/
level_play_live.ex
253 lines (212 loc) · 7.17 KB
/
level_play_live.ex
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
defmodule ElixirCtfWeb.LevelPlayLive do
use Phoenix.LiveView
use Phoenix.HTML
alias MSP430.CPU
alias MSP430.Memory
alias MSP430.IntelHex
alias ElixirCtf.Levels.Level
require Logger
use ElixirCtfWeb, :html
@max_ins_cnt_msg "Exceed maximum number of instructions, reset to continue"
defp update_assign_for_cpu(assigns, cpu) do
mem = cpu.memory
regs =
[:r4, :r5, :r6, :r7, :r8, :r9, :r10, :r11, :r12, :r13, :r14, :r15]
|> Enum.map(fn r -> {r, to_hex(Map.get(mem, r))} end)
cpu_on = CPU.is_on(cpu)
assigns =
assign(assigns,
cpu: cpu,
regs: regs,
pc: mem.pc,
sp: mem.sp,
sr: mem.sr,
ram_dump: Memory.dump_ram(mem, 8),
rom_dump: Memory.dump_rom(mem, 8),
cpu_on: cpu_on,
flags: Memory.get_flag(mem)
)
assigns
end
def render(assigns) do
~H"""
<.flash_group flash={@flash} />
<div class="flex h-screen text-sm">
<div class="w-1/2 m-2 space-y-2">
<.box id="objdump" title="Disassembly" class="h-1/2 overflow-auto">
<code>
<.codeline :for={{code, adr} <- @objdump} code={code} adr={adr} />
</code>
</.box>
<.box id="rom" title="ROM" class="overflow-auto h-1/2">
<.dataline :for={{adr, words} <- @rom_dump} adr={adr} words={words} />
</.box>
</div>
<div class="w-1/2 m-2 space-y-2">
<div class="flex flex-row space-x-2">
<.box id="register" title="Register" class="basis-1/2">
<table>
<tr>
<td>pc:</td>
<td id="pc" phx-hook="PC"><%= to_hex(@pc) %></td>
<td>sp:</td>
<td><%= to_hex(@sp) %></td>
<td>sr:</td>
<td><%= to_hex(@sr) %></td>
</tr>
<tr :for={regline <- Enum.chunk_every(@regs, 4)}>
<%= for {rname, rval} <- regline do %>
<td><%= rname %>:</td>
<td><%= rval %> </td>
<% end %>
</tr>
</table>
</.box>
<.box id="cpu_status" title="CPU Status" class="basis-1/2">
<span>
Power:
<%= if @cpu_on do %>
ON
<% else %>
OFF
<% end %>
</span>
<br />
<span>
Flags: V: <%= @flags[:v] %> N: <%= @flags[:n] %> Z: <%= @flags[:z] %> C: <%= @flags[:c] %>
</span>
<br />
<span>Instruction count: <%= @cpu.ins_cnt %></span>
</.box>
</div>
<.box id="stdout" title="Output" class="h-28 overflow-auto">
<pre><%= @cpu.stdout %></pre>
</.box>
<.control_panel cpu_on={@cpu_on} />
<.box id="ram" title="RAM" class="overflow-auto">
<.dataline :for={{adr, words} <- @ram_dump} adr={adr} words={words} />
</.box>
</div>
</div>
<%= if @cpu.require_input do %>
<.input_modal />
<% end %>
<.confirm_modal is_solving={@is_solving} unlocked={@cpu.unlocked} cpu_on={@cpu_on} />
<style>
#code-<%= Integer.to_string(@pc, 16) |> String.upcase %>{color: red; font-weight: bold;}
<%= for adr <- @breakpoints do %>
#code-<%= Integer.to_string(adr, 16) |> String.upcase %>{background: lightblue; font-weight: bold;}
<% end %>
</style>
"""
end
def mount(%{"id" => id}, session, socket) do
Logger.info("mount", session: session, id: id)
level = Level.get_level!(id)
{:ok, setup_for_level(socket, level)}
end
def handle_event("step", _value, socket) do
if socket.assigns.is_solving do
Logger.error("stepping not allowed in solve mode", assigns: socket.assigns)
{:noreply, socket}
else
case CPU.exec_single(socket.assigns.cpu) do
{:ok, cpu} -> {:noreply, update_assign_for_cpu(socket, cpu)}
{:error, _} -> {:noreply, put_flash(socket, :error, @max_ins_cnt_msg)}
end
end
end
def handle_event("run", _value, socket) do
if socket.assigns.is_solving do
Logger.error("running not allowed in solve mode", assigns: socket.assigns)
{:noreply, socket}
else
case CPU.exec_continuously(socket.assigns.cpu, socket.assigns.breakpoints) do
{:ok, cpu} -> {:noreply, update_assign_for_cpu(socket, cpu)}
{:error, _} -> {:noreply, put_flash(socket, :error, @max_ins_cnt_msg)}
end
end
end
def handle_event("provide_input", value, socket) do
input = Map.get(value, "input")
ishex = Map.get(value, "ishex")
input =
if ishex == "true",
do: Base.decode16(input, case: :mixed),
else: {:ok, input}
case input do
{:ok, input} ->
cpu = CPU.provide_input(socket.assigns.cpu, input)
if socket.assigns.is_solving do
# no breakpoints in solve mode
case CPU.exec_continuously(cpu) do
{:ok, cpu} -> {:noreply, update_assign_for_cpu(socket, cpu)}
{:error, _} -> {:noreply, put_flash(socket, :error, @max_ins_cnt_msg)}
end
else
{:noreply, update_assign_for_cpu(socket, cpu)}
end
_ ->
Logger.error("invalid input", input: input, ishex: ishex)
{:noreply, socket}
end
end
def handle_event("toggle_breakpoint", value, socket) do
adr = Map.get(value, "address")
if adr == nil do
Logger.error("invalid breakpoint address")
{:noreply, socket}
else
address = String.to_integer(adr, 16)
breakpoints =
if MapSet.member?(socket.assigns.breakpoints, address),
do: MapSet.delete(socket.assigns.breakpoints, address),
else: MapSet.put(socket.assigns.breakpoints, address)
socket = assign(socket, breakpoints: breakpoints)
{:noreply, socket}
end
end
def handle_event("solve", _value, socket) do
id = socket.assigns.level.id
Logger.info("solve", id: id)
level = Level.get_level!(id)
{:noreply, setup_for_level(socket, level, true)}
end
def handle_event("reset", _value, socket) do
id = socket.assigns.level.id
level = Level.get_level!(id)
socket = put_flash(socket, :error, nil)
{:noreply, setup_for_level(socket, level)}
end
defp setup_for_level(socket, level, is_solving \\ false) do
hex = String.split(level.hex, ~r{\n|\r\n}) |> Enum.filter(&(String.length(&1) > 0))
{:ok, words} = IntelHex.load(hex, Memory.rom_start())
mem = Memory.init(words)
cpu = CPU.init(mem)
cpu =
if is_solving do
{:ok, cpu} = CPU.exec_continuously(cpu)
cpu
else
cpu
end
regex = ~r/^[[:blank:]]+(?<address>[0-9a-f]{4}):[[:blank:]]+/i
objdump =
String.split(level.objdump, ~r{\n})
|> Enum.map(fn line ->
adr = Regex.named_captures(regex, line)["address"]
adr = if adr != nil, do: String.upcase(adr), else: nil
{line, adr}
end)
breakpoints = Map.get(socket.assigns, :breakpoints, MapSet.new())
socket =
assign(socket,
level: level,
objdump: objdump,
is_solving: is_solving,
breakpoints: breakpoints
)
socket = update_assign_for_cpu(socket, cpu)
socket
end
end