aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cpu.effekt2
-rw-r--r--src/main.effekt8
-rw-r--r--src/renderer.effekt4
-rw-r--r--src/renderers/js.effekt53
4 files changed, 38 insertions, 29 deletions
diff --git a/src/cpu.effekt b/src/cpu.effekt
index c3cb9aa..1d9fcfa 100644
--- a/src/cpu.effekt
+++ b/src/cpu.effekt
@@ -36,6 +36,8 @@ namespace cpu {
// Main loop
// Fetch, Decode, Execute, Update Timers, Update Display
r.log("Starting the CPU...")
+ r.fill("black")
+ // TODO: Implement the main loop with effects
()
}
}
diff --git a/src/main.effekt b/src/main.effekt
index 446f2ed..627ae8e 100644
--- a/src/main.effekt
+++ b/src/main.effekt
@@ -8,11 +8,9 @@ import bytearray
def main(): Unit = {
// Using the JS backend
def r = JSRenderer::makeRenderer
- try {
- r.init()
- } with start { rom =>
- log("ROM loaded to the functionnn!")
- resume(())
+ def test(rom: ByteArray) = {
+ cpu::run(rom) {r}
}
+ r.init(test)
()
} \ No newline at end of file
diff --git a/src/renderer.effekt b/src/renderer.effekt
index 7a78321..aa8490e 100644
--- a/src/renderer.effekt
+++ b/src/renderer.effekt
@@ -7,12 +7,12 @@ Every backend must implement this interface.
*/
import bytearray
-effect start(rom: ByteArray): Unit
interface Renderer {
- def init(): Unit / start
+ def init(run: (ByteArray) => Unit at {io, global}): Unit
def clear(): Unit
def draw(x: Int, y: Int): Unit
+ def fill(color: String): Unit
def update(): Unit
def log(msg: String): Unit
}
diff --git a/src/renderers/js.effekt b/src/renderers/js.effekt
index b331a60..59b5f21 100644
--- a/src/renderers/js.effekt
+++ b/src/renderers/js.effekt
@@ -7,7 +7,7 @@ import bytearray
// js ffi
extern type Node
extern io def getElementById(id: String): Node = jsWeb "document.getElementById(${id})"
-extern io def addListener(event: String, node: Node, handler: () => Unit at {io, global}): Unit = jsWeb "${node}.addEventListener(${event}, () => $effekt.runToplevel((ks) => ${handler}(ks)))"
+extern io def addListener(event: String, node: Node) {handler: () => Unit}: Unit = jsWeb "${node}.addEventListener(${event}, () => $effekt.runToplevel((ks) => ${box handler}(ks)))"
// Custom logging function that logs to the console and the page
extern io def log(msg: String): Unit = jsWeb "log(${msg});"
@@ -62,46 +62,55 @@ val scale = 10
extern io def renderPage(content: String): Unit = jsWeb "document.write(${content}); romCheck();"
// Initialize the screen
-def init() {run: () => Unit}: Unit = {
+def init(run: (ByteArray) => Unit at {io, global}) = {
renderPage(pageContent)
val startButton = getElementById("start")
- def eventHandler(): Unit = {
- log("Start button clicked! Loading ROM...")
- val rom: ByteArray = loadRom()
- if (rom.size() != 0) {
- log("ROM with size: " ++ show(rom.size()) ++ " loaded!")
- // TODO: Start the emulator somehow
- run()
- } else {
- log("No ROM loaded! Please select a ROM file.")
- }
- }
- addListener("click", startButton, eventHandler)
+
+ addListener("click", startButton) {eventHandler {run}}
+}
+def eventHandler {onClick: (ByteArray) => Unit}: Unit = {
+ log("Start button clicked! Loading ROM...")
+ val rom: ByteArray = loadRom()
+ if (rom.size() != 0) {
+ log("ROM with size: " ++ show(rom.size()) ++ " loaded!")
+ // TODO: Start the emulator somehow
+ onClick(rom)
+ } else {
+ log("No ROM loaded! Please select a ROM file.")
+ }
}
// Clear the screen (set black canvas)
-extern io def clear(): Unit = jsWeb """
- const canvas = document.getElementById('canvas');
- const ctx = canvas.getContext('2d');
- ctx.fillStyle = 'black';
- ctx.fillRect(0, 0, canvas.width, canvas.height);
-"""
+def clear(): Unit = fill("black")
// Draw at (x, y) on the screen
extern io def draw(x: Int, y: Int): Unit = jsWeb """
+ (() => {
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'white';
- ctx.fillRect(${x * 10}, ${y * 10}, 10, 10);
+ ctx.fillRect(10 * ${x}, 10 * ${y}, 10, 10);
+ })();
"""
+
+extern io def fill(color: String): Unit = jsWeb """
+ (() => {
+ const canvas = document.getElementById('canvas');
+ const ctx = canvas.getContext('2d');
+ ctx.fillStyle = ${color};
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
+ })();
+"""
+
namespace JSRenderer {
// make JS Renderer
def makeRenderer: Renderer = new Renderer {
- def init() = init()
+ def init(run) = init(run)
def clear() = clear()
def draw(x: Int, y: Int) = draw(x, y)
def update() = ()
def log(msg: String) = log(msg)
+ def fill(color: String) = fill(color)
}
}