1980年代微型计算机游戏《巫师的城堡》源代码首行的神秘注释"10 REM"_(C2SLFF4"近日被逆向工程解析,揭示其背后隐藏的技术巧妙性 [1]。
这行看似乱码的REM语句实际上是嵌入的Z80机器码例程,用于模拟RANDOM函数 [1]。通过分析发现,该机器码序列为ED 5F 28 FC 32 FF F7 C9 [1],其设计巧妙地读取屏幕右下角内存地址(0xF7FF,即地址-2049对应位置)的数据作为伪随机数生成器的种子 [1]。机器码在BASIC内存中的起始位置位于地址474 [1]。
这一实现方案使得游戏能够生成多达127个可能的随机关卡,其原理为利用Z80处理器的R寄存器低7位,同时排除0值 [1]。这套系统最初发表于1980年7月的《Recreational Computing》杂志 [1]。
技术分析者Josh和Chris通过MAME模拟器、Java版本模拟器和磁带镜像版本的验证,确认了这一机制在Exidy Sorcerer平台上的实现 [1]。
The opening line of the 1980s microcomputer game "Wizard's Castle"—10 REM"_(C2SLFF4—has been revealed to contain cleverly hidden Z80 machine code rather than random characters. [1] Through reverse engineering, researchers Josh and Chris discovered that this seemingly cryptic REM statement functioned as a machine language routine to simulate a RANDOM function. [1]
The embedded code reads a memory address at screen position 0xF7FF, the lower right corner of the display, to seed the random number generator for procedural dungeon generation. [1] The Z80 machine code sequence ED 5F 28 FC 32 FF F7 C9 maps to memory address 474, corresponding to the location where the REM text begins in BASIC memory. [1] This technique enabled the game to generate up to 127 possible dungeon layouts, utilizing the lower 7 bits of the R register while excluding the zero value. [1]
The game was published in July 1980 in Recreational Computing magazine on the Exidy Sorcerer platform. [1] The hidden machine code implementation has since been verified across multiple emulation environments, including MAME, Java-based emulators, and tape image versions. [1]