TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Implement an emulator for a fantasy CPU in JavaScript

86 点作者 codeguppy将近 5 年前

8 条评论

_nalply将近 5 年前
Many years ago when I was bored I «designed» a fantasy CPU with 6 (six) bits and only one usable register, the accumulator. I thought why not. The result:<p>18 bit address bus<p>12 bit stack pointer<p>6 flags zero, negative, carry, overflow, float and user<p>7 addressing modes accumulator, immediate, absolute, relative, stack, stack without pop, stack special<p>64 opcodes like NOP, OR, JMP, ROL, STO, ...<p>I discovered that it is well possible to have a 6 bit CPU but of course this is just a game and probably not useful at all. I wonder whether I would be able to create the circuit for the CPU in a simulator. Probably not even if I omit the floating point handling or only if I dedicated about 10 years of free time. I would need to design a microcode or PLA (like MOS 6502, see <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=5353198" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=5353198</a>) system.<p>I find it amusing that this is the one CPU where octal base is really the primary way to display numbers.
评论 #24258244 未加载
mikestaszel将近 5 年前
If you’re looking for a good next step I recommend CHIP-8.
评论 #24255291 未加载
charlesdaniels将近 5 年前
Nice article! I would definitely recommend for everyone to write a CPU emulator at least once. It will give you a lot of perspective on how computers really work. As someone else pointed out CHIP-8[0] is a good one that&#x27;s not too complicated.<p>If you want something more &quot;real&quot;, MIPS and RISC-V are also not too hard, if you just do a subset of the instruction set and not the whole thing. You can skip most of the instructions that deal in bytes and half-words. It&#x27;s also easier to implement main memory as being word-addressable only (but this can introduce incompatibilities with real assembler programs).<p>I wrote a cycle-accurate, pipeline simulator for MIPS for a course, which totaled only about 1200 SLOC. If you didn&#x27;t need to simulate all the pipeline registers (you probably don&#x27;t) and use a higher level language, this is probably a &lt;750SLOC project.<p>0 - <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;CHIP-8" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;CHIP-8</a>
评论 #24258289 未加载
评论 #24256987 未加载
vorticalbox将近 5 年前
Low level javascript [1] has a great set of videos on this and other topics<p>[1] <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;playlist?list=PLP29wDx6QmW5DdwpdwHCRJsEubS5NrQ9b" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;playlist?list=PLP29wDx6QmW5DdwpdwHCR...</a>
Hnrobert42将近 5 年前
Reading through that was a great refresher. Thanks.<p>One question: would a real CPU have a JL instruction or would that be implemented some other way, like a series of smaller instructions?
评论 #24256275 未加载
评论 #24256453 未加载
评论 #24256380 未加载
re将近 5 年前
A related topic (that the title initially made me think of) is fantasy consoles like the PICO-8, although those appear to commonly use high-level programming languages like Lua.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;paladin-t&#x2F;fantasy" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;paladin-t&#x2F;fantasy</a>
pnonplussed将近 5 年前
okok
评论 #24293037 未加载
评论 #24260293 未加载
MuffinFlavored将近 5 年前
I&#x27;ve written a SuperH and TriCore emulator from scratch (qemu wouldn&#x27;t cut it). I couldn&#x27;t do it in JavaScript because it falls apart when it comes to bit-shifting and integer arithmetic. A lot of processors rely on the C-like functionality of int32_t, uint32_t, etc.<p>&gt;&gt;&gt; 0 in JavaScript just doesn&#x27;t cut it.
评论 #24256534 未加载
评论 #24262764 未加载