Skip to content

Generating WASM for TIC-80 #7

Description

@ion1

Thanks for the project!

I'm trying to generate WASM for TIC-80. I have encountered the following issues while trying to generate a hello world using wasmati:

  • I don't see how to import the memory. importMemory doesn't seem to take module/name parameters. My current result from wasmati looks like the following.

    (import "" "m0" (memory $mimport$0 4 4))

    One needs to do the following to gain access to the 256 KiB of memory TIC-80 provides.

    (import "env" "memory" (memory 4 4))
  • I don't see how to import functions. importFunc doesn't seem to take module/name parameters, and it takes a JavaScript function parameter which I can't provide. My current result from wasmati looks like the following:

     (import "" "f0" (func $fimport$0 (param i32)))

    Examples of importing functions provided by TIC-80:

    (import "env" "cls" (func $cls (param i32)))
    (import "env" "print"
      (func $print (param i32 i32 i32 i32 i32 i32 i32) (result i32)))
  • I don't see how to add data with a starting offset. The content parameter to importMemory seems to write starting from zero.

    In TIC-80, The first 96 KiB are reserved for things such as video RAM and I/O. To write data into the beginning of the free RAM, one needs to do the following.

    (data (i32.const 0x18000) "The beginning of the free RAM")

Apologies if I have missed how to do these things correctly.

A hello world in WAT
(module

(import "env" "memory" (memory 4 4))
(import "env" "cls" (func $cls (param i32)))
(import "env" "print"
  (func $print (param i32 i32 i32 i32 i32 i32 i32) (result i32)))

(export "TIC" (func $TIC))

(data (i32.const 0x18000) "Hello world!\00")

(func $TIC
  (call $cls (i32.const 13))

  (drop (call $print
    (i32.const 0x18000)
    (i32.const 84)
    (i32.const 61)
    (i32.const 15)
    (i32.const 0)
    (i32.const 1)
    (i32.const 0)))

)
My closest attempt at implementing it using wasmati
import {
  call,
  drop,
  func,
  i32,
  importFunc,
  importMemory,
  Module,
} from "wasmati";
import { writeFileSync } from "fs";

function main() {
  const memory = importMemory({ min: 4, max: 4 });

  const cls = importFunc({ in: [i32], out: [] }, () => {});
  const print = importFunc(
    { in: [i32, i32, i32, i32, i32, i32, i32], out: [i32] },
    () => {}
  );

  const TIC = func({ in: [], out: [], locals: [] }, ([], [], _ctx) => {
    call(cls, [13]);
    call(print, [0, 84, 61, 15, 0, 1, 0]);
    drop();
  });

  const module = Module({
    exports: { TIC },
    memory,
  });

  const wasm = module.toBytes();

  writeFileSync("dist/cart.wasm", wasm);
}

main();
The output from the above
(module
 (type $0 (func (param i32)))
 (type $1 (func (param i32 i32 i32 i32 i32 i32 i32) (result i32)))
 (type $2 (func))
 (import "" "m0" (memory $mimport$0 4 4))
 (import "" "f0" (func $fimport$0 (param i32)))
 (import "" "f1" (func $fimport$1 (param i32 i32 i32 i32 i32 i32 i32) (result i32)))
 (export "TIC" (func $0))
 (func $0
  (call $fimport$0
   (i32.const 13)
  )
  (drop
   (call $fimport$1
    (i32.const 0)
    (i32.const 84)
    (i32.const 61)
    (i32.const 15)
    (i32.const 0)
    (i32.const 1)
    (i32.const 0)
   )
  )
 )
)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions