编程与软件工程linoscope.github.io
用 OCaml 写一个 Game Boy 模拟器
Writing a Game Boy Emulator in OCaml
2022 — 2025,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
这篇博客记录了作者用 OCaml 编写 Game Boy 模拟器的过程,分享实现思路与经验。从评论看,文中讨论了 CPU/GPU/声音的同步(采用“追赶”方式),但显示/帧缓冲部分讲得不多。
为什么经久不衰:用函数式语言写底层模拟器,既展示 OCaml 的表达力与性能,也给想动手写模拟器的人一份可读的实战记录。
HN 评论精选
模拟器是许多工程师想完成的项目,评论者打算把该项目移植到 Clojure 练手。
写一个模拟器仍然在我的工程师愿望清单上。我觉得这是我还未跨过的一道坎,但我相当确定自己能做出来。这周末有点空闲……我可能会把这个仓库移植到 Clojure 上练练手。
tombert查看英文原文
An emulator is still on my "bucket list as an engineer." I feel like it's a hurdle I still haven't crossed and I'm reasonably certain I could make one. I have some free time this weekend...I might take this repo and port it over to clojure to get the hang of things.
OCaml 兼具编译性能、静态类型和尾调用优化,适合实现 Forth 式线程码 VM。
OCaml 最近引起我的兴趣,因为它是少数同时满足这些条件的语言:编译成二进制、速度快、是函数式又能容纳命令式风格、有静态类型。尤其让我感兴趣的是如何用它实现 Forth 或基于线程码的 VM。通常用 switch 语句或标签指针来实现这类机器,但这会导致数百甚至数千行无法模块化的代码;OCaml 等函数式语言提供的解决方案是尾调用优化,可以让代码保持模块化并快速运行。我用 Lua 草拟过这个概念,很容易实现一个基本 Forth 且代码可读;进一步推进的话,OCaml 感觉是好选择。
shadowofneptune查看英文原文
OCaml has recently interested me for being one of the few languages that: - Compiles to a binary - Achieves good speed - Is functional, while accommodating imperative styles. - Has a static type In particular, it interests me because of how Forth or a threaded code-based VM could be implemented in it. You could implement the operations of such a machine with a bunch of individual function calls, but that's slow and rather unnecessary. A jump to the next VM instruction's code is what you want instead. Switch statements or pointers to labels are what are often used, but that means you end up with hundreds or even thousands of lines of code which cannot be split into modules. The solution OCaml and other functional languages provide involves tail-call optimization: let rec first x = print_endline "foo" ; second(x) and second x = print_endline "bar" ; first(x);; first 1;; In a usual compiled language, these two functions would only be able to print 'foobar' a few hundred or thousand times before causing a stack overflow. In a language like OCaml this can run forever, and quickly too. This allows for what this web page calls continuation-passing threaded code: https://www.complang.tuwien.ac.at/forth/threaded-code.html I have mocked up the concept in lua, an imperative language with tail call optimization, and it was quite easy to implement a basic Forth while keeping the code legible. To take the idea further OCaml feels like a good choice. If anyone has experience with OCaml, or any warnings, it would be helpful.
赞赏文章,但希望补充帧缓冲输出等显示细节,并指出 CPU/GPU/声音同步常被资料忽略。
很棒的写作!如果能多讲讲你如何处理显示就更好了。在我的模拟器之旅中,两个最大的“我完全不知道如何解决”的问题是:1)同步 CPU/GPU/声音(我最终采用了和你一样的方法,“追赶”),2)把帧缓冲写到屏幕上。我用过的资料似乎总是跳过这一点,所以我假设它很明显,但对我来说并非如此。
laputan_machine查看英文原文
Really nice write up! It would have been nice to see more on how you handled the display, on my emulator journey the two big "i have no idea how to solve this" problems I had were 1) synchronising cpu/gpu/sound (and I settled on the exact method you use, 'catching up'), and 2) writing the framebuffer to the screen. The resources I've used always seem to skip over it, so I assume it's obvious, but it isn't to me.
历次讨论
同类还值得读
开源应用架构
The Architecture of Open Source Applications
《开源软件架构》(AOSA)是一本在线免费书,邀请知名开源项目的作者亲自剖析其内部设计,涵盖 SQLAlchemy、LLVM、Audacity、Apache、Bash 等系统。
编程与软件工程上首页 9 次 · 2011–2024aosabook.org编辑精选编程语言简史:简短、不完整且多半错误
A Brief, Incomplete, and Mostly Wrong History of Programming Languages
一篇以戏谑、故意不准确的方式回顾编程语言史的博客文章,用夸张轶事和讽刺定义串起 Lisp、C、C++、Java、JavaScript、PHP 等语言。
编程与软件工程上首页 8 次 · 2009–2021james-iry.blogspot.comSQLite 是如何测试的
How SQLite is tested
SQLite 官方文档页,系统介绍 SQLite 的测试体系:多种测试方法(TCL 测试、模糊测试、回归测试、静态分析等)和海量测试用例,展示了这个小型开源项目如何靠极致测试获得…
编程与软件工程上首页 8 次 · 2009–2025sqlite.org编辑精选高级编程语言(2009)
Advanced programming languages (2009)
Matt Might 的经典文章,按用途推荐“高级编程语言”:如果追求就业就选主流语言,如果追求表达力、学术研究或创业,则推荐 Haskell、OCaml、Scala 等函数式语…
编程与软件工程上首页 8 次 · 2009–2022matt.might.net我的 Forth 与栈机器经历
Yosefk - My history with Forth & stack machines
作者以亲身经历回顾了使用 Forth 语言和栈式机器的历程:既赞叹其简洁优雅、代码量小、可自举的优点,也坦率指出栈操作在实际编程中的痛苦,以及 Forth 社区文化中的问题。
编程与软件工程上首页 8 次 · 2010–2024yosefk.com编辑精选