// Copyright 2024 Google LLC // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // https://www.apache.org/licenses/LICENSE-2.0 // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #include "common.h" #include "math.h" #include "../third_party/z80_trace.h" #ifdef WASM FILE * const stderr=NULL; int fprintf(FILE *stream, const char *format, ...) {return 0;} #define PAGE_SIZE 0x10000 extern unsigned char __heap_base; static int _heap_end = (int)&__heap_base; WASM_EXPORT("alloc") void * alloc(int size) { int ptr = (_heap_end+0xf) & ~0xf; // round up to 16 byte alignment _heap_end = ptr + size; int pages_needed = (_heap_end+PAGE_SIZE-1) / PAGE_SIZE; int pages_n = __builtin_wasm_memory_size(0); if (pages_n> 30)) * 0xbf58476d1ce4e5b9; z = (z ^ (z >> 27)) * 0x94d049bb133111eb; return z ^ (z >> 31); } WASM_EXPORT("init") void init(int seed) { rng_state[0] = seed; for (int i=0; i>= 8; soup[rnd % soup_len] = v; } } WASM_EXPORT("prepare_batch") int prepare_batch() { const int h=soup_dims[0], w=soup_dims[1], tape_n=h*w; int pair_n = 0, collision_count=0, pos=0; for (int i=0; i>=1; int horizontal = rnd&1; rnd>>=1; int i = rnd % tape_n, j=i; if (idx_in_batch[i]>=0) { ++collision_count; continue;} if (horizontal) { if (i % w == 0) { dir = 1; } if ((i+1) % w == 0) { dir = -1; } j += dir; } else { if (i < w) { dir = 1; } if (tape_n-i-1 < w) { dir = -1; } j += dir*w; } if (idx_in_batch[j]>=0) { ++collision_count; continue;} idx_in_batch[i] = idx_in_batch[j] = pair_n; batch_idx[2*pair_n] = i; batch_idx[2*pair_n+1] = j; for (int k=0; kport_in = inPort; cpu->port_out = outPort; cpu->read_byte = memoryRead_inplace; cpu->write_byte = memoryWrite_inplace; cpu->userdata = (void*)i; batch_pc[i] = 0; batch_read_mask[i] = 0; batch_write_mask[i] = 0; } } WASM_EXPORT("z80_step") void _z80_step() { for (int i=0; ipc; } } WASM_EXPORT("z80_mark") void z80_mark() { z80 * cpu = (z80*)batch_z80; z80_init(cpu); cpu->pc = 0x1111; cpu->sp = 0x2222; cpu->a = 0x01; cpu->b = 0x02; cpu->c = 0x03; cpu->d = 0x04; cpu->e = 0x05; cpu->h = 0x06; cpu->l = 0x07; }