JavaScript "boot"

         
/**
* Warranty & Liability
* To the extent permitted by applicable law and unless explicitly
* otherwise agreed upon, XLOG Technologies AG makes no warranties
* regarding the provided information. XLOG Technologies AG assumes
* no liability that any problems might be solved with the information
* provided by XLOG Technologies AG.
*
* Rights & License
* All industrial property rights regarding the information - copyright
* and patent rights in particular - are the sole property of XLOG
* Technologies AG. If the company was not the originator of some
* excerpts, XLOG Technologies AG has at least obtained the right to
* reproduce, change and translate the information.
*
* Reproduction is restricted to the whole unaltered document. Reproduction
* of the information is only allowed for non-commercial uses. Selling,
* giving away or letting of the execution of the library is prohibited.
* The library can be distributed as part of your applications and libraries
* for execution provided this comment remains unchanged.
*
* Restrictions
* Only to be distributed with programs that add significant and primary
* functionality to the library. Not to be distributed with additional
* software intended to replace any components of the library.
*
* Trademarks
* Jekejeke is a registered trademark of XLOG Technologies AG.
*/
import {
fs
} from "../../nova/core.mjs";
import {
util
} from "./spin.mjs";
export let GROUP_SIZE = 32;
if (fs !== undefined) {
let webgpu = await import("webgpu");
Object.assign(globalThis, webgpu.globals);
globalThis.navigator.gpu = webgpu.create([]);
}
export function get_pipeline(device) {
let bootstrap = `
fn hack_get(instr: i32, pc: i32, base: i32) -> i32 {
switch ((instr>>28) & 0x0F) {
case 0: { /* cst */
let cst : i32 = (instr & 0x00FFFFFF) - 0x00800000;
return cst;
}
case 1: { /* get */
let mget : i32 = (instr & 0x00FFFFFF) - 0x00800000;
return state[base+mget];
}
case 2: { /* ext */
return code[pc+1];
}
case 3: { /* out */
let out : i32 = (instr & 0x00FFFFFF) - 0x00800000;
return extra_output(out, base);
}
case 4: { /* in */
let in : i32 = (instr & 0x00FFFFFF) - 0x00800000;
return extra_input(in, base);
}
case 5: { /* set */
return 0;
}
case 6: { /* bra */
return pc+1;
}
case 7: { /* brz */
return pc+1;
}
case 8: { /* jma */
return pc+1;
}
case 9: { /* jmz */
return pc+1;
}
default: { /* illegal mode */
return 0;
}
}
}
fn hack_set(instr: i32, accu: i32, pc: i32, base: i32) -> i32 {
switch ((instr>>28) & 0x0F) {
case 0: { /* cst */
return pc+1;
}
case 1: { /* get */
return pc+1;
}
case 2: { /* ext */
return pc+2;
}
case 3: { /* out */
return pc+1;
}
case 4: { /* in */
return pc+1;
}
case 5: { /* set */
let mset : i32 = (instr & 0x00FFFFFF) - 0x00800000;
state[base+mset] = accu;
return pc+1;
}
case 6: { /* bra */
let bra = (instr & 0x00FFFFFF) - 0x00800000;
return bra;
}
case 7: { /* brz */
let brz = (instr & 0x00FFFFFF) - 0x00800000;
return select(pc+1, brz, accu == 0);
}
case 8: { /* jma */
let jma = (instr & 0x00FFFFFF) - 0x00800000;
return state[base+jma];
}
case 9: { /* jmz */
let jmz = (instr & 0x00FFFFFF) - 0x00800000;
return select(pc+1, state[base+jmz], accu == 0);
}
default: { /* illegal mode */
return 0;
}
}
}
fn hack_fun(instr: i32, accu: i32, value: i32) -> i32 {
switch ((instr>>24) & 0x0F) {
case 0: { /* mov */
return value;
}
case 1: { /* neg */
return -value;
}
case 2: { /* add */
return accu + value;
}
case 3: { /* sub */
return accu - value;
}
case 4: { /* mul */
return accu * value;
}
case 5: { /* zdiv */
return accu / value;
}
case 6: { /* rem */
return accu % value;
}
case 7: { /* min */
return min(accu, value);
}
case 8: { /* max */
return max(accu, value);
}
case 9: { /* eq */
return select(0, 1, accu == value);
}
case 10: { /* nq */
return select(0, 1, accu != value);
}
case 11: { /* ls */
return select(0, 1, accu < value);
}
case 12: { /* gr */
return select(0, 1, accu > value);
}
case 13: { /* lq */
return select(0, 1, accu <= value);
}
case 14: { /* gq */
return select(0, 1, accu >= value);
}
default: { /* illegal fun */
return 0;
}
}
}
fn run_id(id : i32) {
if (!(id < i32(arrayLength(&offset)))) {
return;
}
let base : i32 = offset[id];
state[base-3] = id;
var pc : i32 = state[base-2];
var accu : i32 = state[base-1];
while (pc < i32(arrayLength(&code))) {
let instr : i32 = code[pc];
let value : i32 = hack_get(instr, pc, base);
pc = hack_set(instr, accu, pc, base);
accu = hack_fun(instr, accu, value);
}
state[base-2] = pc;
state[base-1] = accu;
}
`
let shader = `
@group(0) @binding(0) var<storage, read> code: array<i32>;
@group(0) @binding(1) var<storage, read> offset: array<i32>;
@group(0) @binding(2) var<storage, read_write> comm: array<Flit>;
@group(0) @binding(3) var<storage, read_write> state: array<i32>;
`
shader += util;
shader += bootstrap;
shader += `
@compute @workgroup_size(${GROUP_SIZE})
fn main(@builtin(global_invocation_id) global_id : vec3<u32>) {
var len = i32(arrayLength(&comm));
run_id(i32(global_id.x));
}
`
let module = device.createShaderModule({
code: shader,
});
let pipeline = device.createComputePipeline({
layout: "auto",
compute: {
module: module,
entryPoint: 'main',
},
});
return pipeline;
}