Added buch of stuff
This commit is contained in:
57
nodes/robots/coding/cpu.gd
Normal file
57
nodes/robots/coding/cpu.gd
Normal file
@@ -0,0 +1,57 @@
|
||||
class_name CPU
|
||||
extends Node
|
||||
|
||||
signal cpu_tick
|
||||
|
||||
var cobor_vm := CoborVirtualMachine.new()
|
||||
var running := false
|
||||
var register_size := 8
|
||||
var modules : Array[Module]
|
||||
|
||||
func _ready() -> void:
|
||||
cobor_vm.set_registers(register_size,[])
|
||||
var register_index := 1
|
||||
for child in get_children():
|
||||
if child is Module:
|
||||
var module := child as Module
|
||||
modules.push_back(module)
|
||||
register_size += module.set_registers(register_index)
|
||||
|
||||
func run_step() -> void:
|
||||
if not running:
|
||||
cobor_vm.set_registers(register_size,[])
|
||||
running = true
|
||||
_tick()
|
||||
|
||||
func run() -> void:
|
||||
if not running:
|
||||
cobor_vm.set_registers(register_size,[])
|
||||
running = true
|
||||
$CPUClock.start()
|
||||
|
||||
func _tick()->void:
|
||||
for module in modules:
|
||||
module.early_tick()
|
||||
var err := cobor_vm.run_step()
|
||||
if not err.is_empty():
|
||||
print(err)
|
||||
if cobor_vm.get_program_counter() >= cobor_vm.get_program_size():
|
||||
stop()
|
||||
for module in modules:
|
||||
module.late_tick()
|
||||
emit_signal("cpu_tick")
|
||||
|
||||
func pause()->void:
|
||||
$CPUClock.stop()
|
||||
|
||||
func stop()->void:
|
||||
running = false
|
||||
$CPUClock.stop()
|
||||
|
||||
func compile_code(source_code: String)->void:
|
||||
var errors := cobor_vm.parse_source_code(source_code)
|
||||
if errors.is_empty():
|
||||
print("Compiling Ok!")
|
||||
else:
|
||||
for error in errors:
|
||||
print(error)
|
||||
1
nodes/robots/coding/cpu.gd.uid
Normal file
1
nodes/robots/coding/cpu.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://b87ksbp5mjn2a
|
||||
10
nodes/robots/coding/cpu.tscn
Normal file
10
nodes/robots/coding/cpu.tscn
Normal file
@@ -0,0 +1,10 @@
|
||||
[gd_scene load_steps=2 format=3 uid="uid://crtis0eyywl61"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://b87ksbp5mjn2a" path="res://nodes/robots/coding/cpu.gd" id="1_mh3k1"]
|
||||
|
||||
[node name="CPU" type="Node"]
|
||||
script = ExtResource("1_mh3k1")
|
||||
|
||||
[node name="CPUClock" type="Timer" parent="."]
|
||||
|
||||
[connection signal="timeout" from="CPUClock" to="." method="_tick"]
|
||||
20
nodes/robots/coding/memory_entry.tscn
Normal file
20
nodes/robots/coding/memory_entry.tscn
Normal file
@@ -0,0 +1,20 @@
|
||||
[gd_scene format=3 uid="uid://dqwi5rekytyds"]
|
||||
|
||||
[node name="memory_entry" type="HBoxContainer"]
|
||||
|
||||
[node name="address" type="Label" parent="."]
|
||||
layout_mode = 2
|
||||
text = "/0 = "
|
||||
|
||||
[node name="value" type="SpinBox" parent="."]
|
||||
layout_mode = 2
|
||||
value = 4.0
|
||||
rounded = true
|
||||
allow_greater = true
|
||||
allow_lesser = true
|
||||
alignment = 1
|
||||
editable = false
|
||||
|
||||
[node name="name" type="Label" parent="."]
|
||||
layout_mode = 2
|
||||
text = "[X] Click"
|
||||
47
nodes/robots/coding/programming_ui.gd
Normal file
47
nodes/robots/coding/programming_ui.gd
Normal file
@@ -0,0 +1,47 @@
|
||||
extends PanelContainer
|
||||
|
||||
@onready var code_editor := $%Code as CodeEdit
|
||||
@onready var memory := $%Memory as VBoxContainer
|
||||
@onready var memory_entry := load("uid://dqwi5rekytyds") as PackedScene
|
||||
|
||||
@export var cpu : CPU
|
||||
|
||||
func _ready() -> void:
|
||||
for i in range(cpu.register_size):
|
||||
var entry := memory_entry.instantiate() as Control
|
||||
(entry.get_node("address") as Label).text = "[/" + str(i) + "]"
|
||||
(entry.get_node("value") as SpinBox).value = 0
|
||||
memory.add_child(entry)
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
# Set execution indicator
|
||||
for line in code_editor.get_executing_lines():
|
||||
code_editor.set_line_as_executing(line, false)
|
||||
code_editor.set_line_as_executing(cpu.cobor_vm.get_program_counter(), true)
|
||||
# Show register values
|
||||
var registers := cpu.cobor_vm.get_registers()
|
||||
for i in range(registers.size()):
|
||||
var entry := memory.get_child(i) as Control
|
||||
(entry.get_node("value") as SpinBox).value = registers[i]
|
||||
|
||||
func _on_compile_button_pressed() -> void:
|
||||
cpu.compile_code(code_editor.text)
|
||||
|
||||
func _on_stop_button_pressed() -> void:
|
||||
code_editor.editable = true
|
||||
cpu.stop()
|
||||
|
||||
func _on_play_button_pressed() -> void:
|
||||
if code_editor.editable:
|
||||
cpu.compile_code(code_editor.text)
|
||||
code_editor.editable = false
|
||||
cpu.run()
|
||||
|
||||
func _on_pause_button_pressed() -> void:
|
||||
cpu.pause()
|
||||
|
||||
func _on_step_button_pressed() -> void:
|
||||
if code_editor.editable:
|
||||
cpu.compile_code(code_editor.text)
|
||||
code_editor.editable = false
|
||||
cpu.run_step()
|
||||
1
nodes/robots/coding/programming_ui.gd.uid
Normal file
1
nodes/robots/coding/programming_ui.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://csub2er5wvqj3
|
||||
77
nodes/robots/coding/programming_ui.tscn
Normal file
77
nodes/robots/coding/programming_ui.tscn
Normal file
@@ -0,0 +1,77 @@
|
||||
[gd_scene load_steps=2 format=3 uid="uid://c2es6v8lb8tug"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://csub2er5wvqj3" path="res://nodes/robots/coding/programming_ui.gd" id="1_qk3c3"]
|
||||
|
||||
[node name="ProgrammingUI" type="PanelContainer"]
|
||||
anchors_preset = 9
|
||||
anchor_bottom = 1.0
|
||||
offset_right = 314.0
|
||||
grow_vertical = 2
|
||||
script = ExtResource("1_qk3c3")
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="."]
|
||||
layout_mode = 2
|
||||
theme_override_constants/separation = 10
|
||||
|
||||
[node name="Label" type="Label" parent="VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Programming"
|
||||
horizontal_alignment = 1
|
||||
|
||||
[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_vertical = 3
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="Label" type="Label" parent="VBoxContainer/HBoxContainer/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = " Source Code "
|
||||
|
||||
[node name="Code" type="CodeEdit" parent="VBoxContainer/HBoxContainer/VBoxContainer"]
|
||||
unique_name_in_owner = true
|
||||
custom_minimum_size = Vector2(200, 0)
|
||||
layout_mode = 2
|
||||
size_flags_vertical = 3
|
||||
placeholder_text = "Code here..."
|
||||
symbol_tooltip_on_hover = true
|
||||
gutters_draw_executing_lines = true
|
||||
gutters_draw_line_numbers = true
|
||||
gutters_zero_pad_line_numbers = true
|
||||
indent_automatic = true
|
||||
|
||||
[node name="Memory" type="VBoxContainer" parent="VBoxContainer/HBoxContainer"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
|
||||
[node name="HBoxContainer2" type="HBoxContainer" parent="VBoxContainer"]
|
||||
layout_mode = 2
|
||||
theme_override_constants/separation = 10
|
||||
alignment = 1
|
||||
|
||||
[node name="compileButton" type="Button" parent="VBoxContainer/HBoxContainer2"]
|
||||
layout_mode = 2
|
||||
text = "[C]"
|
||||
|
||||
[node name="StopButton" type="Button" parent="VBoxContainer/HBoxContainer2"]
|
||||
layout_mode = 2
|
||||
text = "[ ]"
|
||||
|
||||
[node name="PlayButton" type="Button" parent="VBoxContainer/HBoxContainer2"]
|
||||
layout_mode = 2
|
||||
text = ">"
|
||||
|
||||
[node name="PauseButton" type="Button" parent="VBoxContainer/HBoxContainer2"]
|
||||
layout_mode = 2
|
||||
text = "||"
|
||||
|
||||
[node name="StepButton" type="Button" parent="VBoxContainer/HBoxContainer2"]
|
||||
layout_mode = 2
|
||||
text = ">|"
|
||||
|
||||
[connection signal="pressed" from="VBoxContainer/HBoxContainer2/compileButton" to="." method="_on_compile_button_pressed"]
|
||||
[connection signal="pressed" from="VBoxContainer/HBoxContainer2/StopButton" to="." method="_on_stop_button_pressed"]
|
||||
[connection signal="pressed" from="VBoxContainer/HBoxContainer2/PlayButton" to="." method="_on_play_button_pressed"]
|
||||
[connection signal="pressed" from="VBoxContainer/HBoxContainer2/PauseButton" to="." method="_on_pause_button_pressed"]
|
||||
[connection signal="pressed" from="VBoxContainer/HBoxContainer2/StepButton" to="." method="_on_step_button_pressed"]
|
||||
Reference in New Issue
Block a user