From 418c714c453a181f9e7e99b195f320b84351fd48 Mon Sep 17 00:00:00 2001 From: Will Scullin Date: Sat, 5 Aug 2023 18:27:57 -0700 Subject: [PATCH] Convert RAM to Typescript --- js/ram.js | 57 ------------------------------------------------- js/ram.ts | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ js/types.ts | 16 ++++++++++++++ 3 files changed, 77 insertions(+), 57 deletions(-) delete mode 100644 js/ram.js create mode 100644 js/ram.ts create mode 100644 js/types.ts diff --git a/js/ram.js b/js/ram.js deleted file mode 100644 index a64c5d3..0000000 --- a/js/ram.js +++ /dev/null @@ -1,57 +0,0 @@ -/* -*- mode: JavaScript; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* Copyright 2010-2019Will Scullin - * - * Permission to use, copy, modify, distribute, and sell this software and its - * documentation for any purpose is hereby granted without fee, provided that - * the above copyright notice appear in all copies and that both that - * copyright notice and this permission notice appear in supporting - * documentation. No representations are made about the suitability of this - * software for any purpose. It is provided "as is" without express or - * implied warranty. - */ - -import { base64_decode, base64_encode } from './base64'; -import { allocMemPages } from './util'; - -export default function RAM(sp, ep) { - var mem; - var start_page = sp; - var end_page = ep; - - mem = allocMemPages(ep - sp + 1); - - for (var page = 0; page <= ep; page++) { - for (var off = 0; off < 0x100; off++) { - mem[page * 0x100 + off] = 0; // Math.floor(Math.random()*256); - } - } - - return { - start: function() { - return start_page; - }, - end: function() { - return end_page; - }, - read: function(page, off) { - return mem[(page - start_page) * 0x100 + off]; - }, - write: function(page, off, val) { - mem[(page - start_page) * 0x100 + off] = val; - }, - - getState: function() { - return { - start: start_page, - end: end_page, - mem: base64_encode(mem) - }; - }, - - setState: function(state) { - start_page = state.start; - end_page = state.end; - mem = base64_decode(state.mem); - } - }; -} diff --git a/js/ram.ts b/js/ram.ts new file mode 100644 index 0000000..0de775d --- /dev/null +++ b/js/ram.ts @@ -0,0 +1,61 @@ +/* Copyright 2010-2023 Will Scullin + * + * Permission to use, copy, modify, distribute, and sell this software and its + * documentation for any purpose is hereby granted without fee, provided that + * the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation. No representations are made about the suitability of this + * software for any purpose. It is provided "as is" without express or + * implied warranty. + */ + +import { base64_decode, base64_encode } from "./base64"; +import { allocMemPages } from "./util"; +import type { byte } from "./types"; + +export interface RAMState { + start: byte; + end: byte; + mem: string; +} + +export default class RAM { + mem: Uint8Array | byte[]; + + constructor(private start_page: byte, private end_page: byte) { + this.mem = allocMemPages(end_page - start_page + 1); + + for (var page = 0; page <= end_page; page++) { + for (var off = 0; off < 0x100; off++) { + this.mem[page * 0x100 + off] = 0; // Math.floor(Math.random()*256); + } + } + } + + start() { + return this.start_page; + } + end() { + return this.end_page; + } + read(page: byte, off: byte) { + return this.mem[(page - this.start_page) * 0x100 + off]; + } + write(page: byte, off: byte, val: byte) { + this.mem[(page - this.start_page) * 0x100 + off] = val; + } + + getState(): RAMState { + return { + start: this.start_page, + end: this.end_page, + mem: base64_encode(this.mem), + }; + } + + setState(state: RAMState) { + this.start_page = state.start; + this.end_page = state.end; + this.mem = base64_decode(state.mem); + } +} diff --git a/js/types.ts b/js/types.ts new file mode 100644 index 0000000..91b2795 --- /dev/null +++ b/js/types.ts @@ -0,0 +1,16 @@ +/* Copyright 2023 Will Scullin + * + * Permission to use, copy, modify, distribute, and sell this software and its + * documentation for any purpose is hereby granted without fee, provided that + * the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation. No representations are made about the suitability of this + * software for any purpose. It is provided "as is" without express or + * implied warranty. + */ + +export type byte = number; + +export type word = number; + +export type address = word;