gpt4 book ai didi

在 C 中为 Gameboy 创建第二个计数器

转载 作者:太空宇宙 更新时间:2023-11-04 00:37:54 25 4
gpt4 key购买 nike

所以我是C新手,被分配了制作游戏的任务。我将使用 Gameboy 模拟器,并且不鼓励导入任何超出基础知识的库。

我想想出一种方法来运行秒计数器(将显示在屏幕上,)但无法使用 time.h 库,我觉得有点卡住了。

无论如何我可以着手做这件事吗?有人告诉我 Gameboy 运行得相当慢,如果我能让它陷入“忙循环”,我就可以“大约一秒”并以此方式计算。

我考虑过使用 sleep 函数来执行此操作,但那是在 unistd.h 库中。我也考虑过也许设置一个循环并计数到 10,000(或任何可能需要一秒钟计算的数字),但所有这些都将同时发生在手头的游戏中,我担心这样的事情会延迟游戏玩法和其他事情的发生。

有什么建议吗?

编辑:我认为 stdlib.hstdio.h 之外的任何内容都是不允许的。

最佳答案

gameboy有一个硬件定时器。

Sometimes it's useful to have a timer that interrupts at regular intervals for routines that require periodic or percise updates. The timer in the GameBoy has a selectable frequency of 4096, 16384, 65536, or 262144 Hertz. This frequency increments the Timer Counter (TIMA). When it overflows, it generates an interrupt. It is then loaded with the contents of Timer Modulo (TMA). The following are examples:

;This interval timer interrupts 4096 times per second
ld a,-1
ld ($FF06),a ;Set TMA to divide clock by 1
ld a,4
ld ($FF07),a ;Set clock to 4096 Hertz

因此,编写中断处理程序以跟踪中断次数并每 4096 (=0x1000) 次中断更新显示的时钟。

关于在 C 中为 Gameboy 创建第二个计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22873168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com