wokwi / avr8js

Arduino (8-bit AVR) simulator, written in JavaScript and runs in the browser / Node.js
https://blog.wokwi.com/avr8js-simulate-arduino-in-javascript/
MIT License
481 stars 78 forks source link

AssemblyScript conflicts #43

Closed Dudeplayz closed 3 years ago

Dudeplayz commented 4 years ago

This issue collects conflicts of the current code base and AssemblyScript. See #35 and the research project.

Found problems:

  1. Data types must be specified explicitly
  2. AssemblyScript has a restricted data type set
    • The current types file can be adapted to keep JS compatibility
  3. Interfaces are currently not supported. Breaks e.g. ICPU and CPU. (further research required)
  4. Declaring variables as constructor parameters brings up memory access errors because the instantiation works differently. E.g. CPU with progMem, which has to be declared outside the constructor and the following instantiations have to be done in the constructor body:
    constructor(progMem: Uint16Array, private sramBytes: u64 = 8192) {
        this.progMem = progMem;
        this.progBytes = Uint8Array.wrap(this.progMem.buffer);
        this.pc22Bits = this.progBytes.length > 0x20000;
        this.reset();
    }

(The research is still ongoing.)

MaxGraey commented 4 years ago
  1. Some basic interfaces should be allowed in assemblyscript@nightly with recent PR
  2. this is known issue
MaxGraey commented 4 years ago
  1. fixed
urish commented 4 years ago

Thank @MaxGraey for your help here :-) It's really appreciated!

Dudeplayz commented 4 years ago

@MaxGraey There is still an error if a class variable is based on a constructor variable:

RuntimeError: memory access out of bounds
    at ~lib/arraybuffer/ArrayBuffer#get:byteLength (wasm-function[26]:0xfd6)
    at ~lib/typedarray/Uint8Array.wrap (wasm-function[31]:0x11c5)
    at ~lib/typedarray/Uint8Array.wrap@varargs (wasm-function[32]:0x12a6)
    at assembly/cpu/cpu/CPU#constructor (wasm-function[35]:0x1345)

I had to change this:

readonly progBytes : Uint8Array = Uint8Array.wrap(this.progMem.buffer);
constructor(public progMem: Uint16Array, private sramBytes: u64 = 8192) {
    this.reset();
}

To that:

readonly progBytes : Uint8Array;
constructor(public progMem: Uint16Array, private sramBytes: u64 = 8192) {
    this.progBytes = Uint8Array.wrap(this.progMem.buffer);
    this.reset();
}
MaxGraey commented 4 years ago

Could you create minimal example on WebAssembly Studio?

Dudeplayz commented 4 years ago

@MaxGraey

  1. Some basic interfaces should be allowed in assemblyscript@nightly with recent PR
  2. this is known issue

Is the mentioned PR already in 0.12.3? I tried to use the CPU Interface again, but it doesn't work with the error, that the variables are declared twice with the same name.

MaxGraey commented 4 years ago

Without code I can't tell what's wrong. Plz open issue with minimal reproducible example

Dudeplayz commented 4 years ago

Yes, I will open an issue.