ios push上移64
BASIC (Beginners’ All-purpose Symbolic Instruction Code or Beginners All-purpose Symbolic Instruction Code) is a family of general-purpose, high-level programming languages whose design philosophy emphasizes ease of use. — Wikipedia: BASIC
BASIC(初学者的通用符号说明代码或初学者的通用符号说明代码)是一系列通用的高级编程语言,其设计理念强调易用性。 — 维基百科:BASIC
After running C64 BASIC on a microcontroller, in a Docker image and also on the console, I thought, why not on an iPhone?
我想在微控制器,Docker镜像以及控制台上运行C64 BASIC之后,为什么不在iPhone上运行?
My idea is to create an iOS app for the output and an input field for the commands. I send the commands to a C64 BASIC interpreter and read the output to display it in the app. I adjust the color and font and then I will have a C64 on my iPhone.
我的想法是为命令的输出和输入字段创建一个iOS应用。 我将命令发送到C64 BASIC解释器,并读取输出以将其显示在应用程序中。 我调整颜色和字体,然后在iPhone上安装C64。

C64 BASIC库 (C64 BASIC Libraries)
At the end I wrote two apps because I had two libraries with a C64 BASIC interpreter, and each library has an advantage over the other one:
最后,我编写了两个应用程序,因为我有两个带有C64 BASIC解释器的库,并且每个库都比另一个库具有优势:
cbmbasic (cbmbasic)
The project started 11 years ago, and the last activity was 6 years ago. As you can read in the README file, any code is completely native. But and this is a very big disadvantage, the code was generated (with LLVM), and therefore everything is in one 1.8 MByte file — Xcode doesn’t like that. The whole interpreter runs in the main function, in a single loop, and the file has 3222 goto commands. The code is not readable.
该项目开始于11年前,最近的活动是6年前。 如您在README文件中所读, 任何代码都是完全本地的 。 但这是一个很大的缺点 ,代码是使用LLVM生成的,因此所有内容都保存在一个1.8 MB的文件中-Xcode不喜欢这样。 整个解释器在单个循环中运行在main函数中,并且该文件具有3222 goto命令。 该代码不可读。
The good thing about this project is, that some code is separated into own functions. This allows me to hook into the input and output handling.
这个项目的好处是,一些代码被分成了自己的函数。 这使我可以参与输入和输出处理。
The advantage of this project is the extensibility. You can add your own BASIC commands and activate them with the SYS 1 command. As an example, Bill Gates’ Personal Easter Eggs in 8 Bit BASIC was implemented. If I think further, this could be an opportunity to communicate with iOS and call from BASIC some Swift code.
该项目的优势是可扩展性。 您可以添加自己的BASIC命令并使用SYS 1命令激活它们。 例如,实施了比尔·盖茨的8位BASIC个人复活节彩蛋 。 如果我进一步考虑,这可能是与iOS通信并从BASIC调用一些Swift代码的机会。
c-simple-emu6502-cbm (c-simple-emu6502-cbm)
I don’t know if you need to know 6502 assembler code, but it’s fun. The c-simple-emu6502-cbm project is a month old and it emulates a 6502 processor. Every 6502-assembler command is implemented in C. The LDA (Load Accumulator) command with the opcode 0xA9 is implemented like this in the emu6502.c file:
我不知道您是否需要了解6502汇编程序代码,但这很有趣。 c-simple-emu6502-cbm项目开展了一个月,它模仿6502处理器。 每个6502汇编器命令都在C中实现。 操作码为 0xA9的LDA (负载累加器)命令在emu6502.c文件中按以下方式实现:
case 0xA9: SetA(GetIM(PC, &bytes)); break;
In case of 0xA9, the SetA
method is called with the return value of the getIM
method. The 'IM' stands for immediate addressing and returns the value after the PC (program counter).
在0xA9的情况下,将使用SetA
方法的返回值调用getIM
方法。 “ IM”代表立即寻址,并在PC(程序计数器)之后返回该值。
The p_bytes
value is added to the PC at the end. It is set to 2, because the operator requires two bytes. One for the LDA command and the second for the value that is passed.
最后将p_bytes
值添加到PC。 设置为2,因为运算符需要两个字节。 一个用于LDA命令,第二个用于传递的值。
static byte GetIM(ushort addr, byte *p_bytes)
{
*p_bytes = 2;
return GetMemory((ushort)(addr + 1));
}
The SetA
method sets the value
to the register A (accumulator).
SetA
方法将value
设置到寄存器A(累加器)。
static void SetA(int value)
{
SetReg(&A, value);
}
This command influences also the flags Z (zero) and N (negativ). Therefore, the SetReg
method checks the value
. If it is null, Z is true and if it is negativ (bit 7 is high) the N flag is true.
该命令还影响标志Z(零)和N(负)。 因此, SetReg
方法检查value
。 如果为空,则Z为true;如果为负(第7位为高),则N标志为true。
static void SetReg(byte *p_reg, int value)
{
*p_reg = (byte)value;
Z = (*p_reg == 0);
N = ((*p_reg & 0x80) != 0);
}
The 6502 Microprocessor Instant Reference Card is an old but also beautiful document. Isn’t it nice?
6502微处理器即时参考卡既旧又漂亮。 好不好
iOS框架 (iOS Framework)
To integrate one of the C64 BASIC interpreters I have created an iOS framework. The article Xcode: Frameworks and Wrapping a C Library in a Swift Framework describes the necessary steps to create an iOS framework project. See also What are Frameworks?
为了集成一个C64 BASIC解释器,我创建了一个iOS框架。 文章Xcode:框架和在Swift框架中包装C库描述了创建iOS框架项目的必要步骤。 另请参阅什么是框架?
架构和工作流程 (Architecture and workflow)
The iOS app is responsible for input and output. It uses a UITextView with C64 colors and a C64 font for the output and a UITextField for the input. The C64 BASIC interpreter is part of an iOS framework, which provides the interface for sending and receiving data of the iOS application. The interpreter receives the input data and returns the output. An Objective-C wrapper is used for this.
iOS应用负责输入和输出。 它使用具有C64颜色和C64字体的UITextView作为输出,并使用UITextField作为输入。 C64 BASIC解释器是iOS框架的一部分,该框架提供了用于发送和接收iOS应用程序数据的接口。 解释器接收输入数据并返回输出。 为此使用了Objective-C包装器。

The figure shows the files for the example c-simple-emu6502-cbm, they are similar for the cbmbasic project. The Swift application communicates with the Swift interface of the iOS framework. This one with the Objective-C wrapper and this one with the C64 BASIC interpreter.
该图显示了示例c-simple-emu6502-cbm的文件 ,这些文件与cbmbasic项目相似。 Swift应用程序与iOS框架的Swift接口进行通信。 这是使用Objective-C包装器的,而这是使用C64 BASIC解释器的。
The iOS app starts the emulator with the run
command. The user input is sent with the cmd
method. The read
method reads the C64 output.
iOS应用程序使用run
命令启动模拟器。 用户输入使用cmd
方法发送。 read
方法读取C64输出。
The Swift interface implements the run
, cmd
and read
methods. The run
method calls the C64_Init
method with the ROM file names. Additionally, it calls the MyResetRun
method, which is a simple wrapper method - otherwise I would have had to deal with function pointers of the original method. The cmd
method writes the user input into a char array, and the read
method fetches the C64 output data.
Swift接口实现run
, cmd
和read
方法。 run
方法使用ROM文件名调用C64_Init
方法。 另外,它调用MyResetRun
方法,这是一个简单的包装方法-否则我将不得不处理原始方法的函数指针。 cmd
方法将用户输入写入char数组,而read
方法则获取C64输出数据。
CBM_Console_ReadChar
and writeChar
implement the input and output. Both interpreters use stdin and stdout to read and write. But it is not a console application and therefore I have redirected the input and output.
CBM_Console_ReadChar
和writeChar
实现输入和输出。 两个解释器都使用stdin和stdout进行读写。 但这不是控制台应用程序,因此我已经重定向了输入和输出。
输入输出 (Input and Output)
The CBM_Console_ReadChar
function loops and looks if there is something new in a command buffer and then sends the character back as "keyboard input". This allows me to write from the app to the framework/lib via Swift, Objective-C and C into the buffer. To prevent the battery from burning 🔥 out I have a sleep in the while loop. Otherwise the CPU would be at 100%. The writeChar
function writes the C64 output into a char array, which is implemented as a ring buffer. I fetch the output characters in the app with the read
method and display them. The app runs a timer which reads the output every 100 ms. Since there is no trigger within C64 BASIC that signals an end, I read the output buffer continuously.
CBM_Console_ReadChar
函数循环并查看命令缓冲区中是否有新内容,然后将字符作为“键盘输入”发送回去。 这使我可以通过Swift,Objective-C和C将应用程序从应用程序写入框架/ lib到缓冲区。 为了防止电池烧坏,我在while循环中入睡。 否则,CPU将处于100%的状态。 writeChar
函数将C64输出写入char数组,该数组实现为环形缓冲区。 我使用read
方法在应用程序中获取输出字符并显示它们。 该应用程序运行一个计时器,该计时器每100毫秒读取一次输出。 由于C64 BASIC中没有触发结束的触发器,因此我连续读取输出缓冲区。
C64外观 (C64 look and feel)
To make the application look like a C64 I have modified the colors 🎨 and font. But the feeling of a C64 is not possible with a touchscreen. Did the C64 keyboard feel like a mechanical keyboard, only clunkier?
为了使应用程序看起来像C64,我修改了颜色🎨和字体。 但是触摸屏无法实现C64的感觉。 C64键盘是否感觉像机械键盘,只是笨拙?
字形 (Font)
The document Adding a Custom Font to Your App describes how you can change the font of your app. I found two fonts and choose the first one:
向您的应用程序添加自定义字体的文档介绍了如何更改应用程序的字体。 我找到了两种字体,然后选择第一种:
色彩 (Colors)
I adapted the foreground and background color based on the Wikipedia: List of 8-bit computer hardware graphics — C64 image. I’ve selected the colors with a pipette.
我根据Wikipedia修改了前景色和背景色:8位计算机硬件图形列表-C64图像。 我已经用移液器选择了颜色。
加载ROM (Load ROMs)
The iPhone app runs within a sandbox; therefore it is not possible to simply access e.g. “basic” as a file. This file is located somewhere in the file system and it must be part of the build target to access it. Bundle.main.path
returns the path of the file and with this the CBM library can open the file. This can also be used to start the emulator with a C64 program.
iPhone应用程序在沙箱中运行; 因此,不可能简单地将例如“基本”作为文件访问。 该文件位于文件系统中的某个位置,并且必须是构建目标的一部分才能访问它。 Bundle.main.path
返回文件的路径,CBM库可以使用此文件打开文件。 这也可以用于通过C64程序启动仿真器。
摘要 (Summary)
Since the 6502 is emulated, I can now run everything on it and so I thought: How about COBOL on a 6502? And then I found this:
由于6502是仿真的,所以我现在可以在其上运行所有内容,因此我想到:在6502上使用COBOL怎么样? 然后我发现了这一点:
“The T-800’s visual systems (at least) appear to be programmed in COBOL and 6502 assembly […]” — T-800 — Terminator Fandom Wiki
“ T-800的视觉系统(至少)似乎是在COBOL和6502组件中编程的[…]” — T-800 —终结者狂热百科
Both interpreters were designed for the console, they just print the output. However, they do not handle the memory of the monitor, which is in the 0x0400 range (see Screen RAM). The graphical output is also not possible. VICE is an emulator which supports it but isn’t available for iOS.
这两个解释器都是为控制台设计的,它们只是打印输出。 但是,它们不处理监视器的内存,内存在0x0400范围内(请参阅Screen RAM )。 图形输出也是不可能的。 VICE是支持它的仿真器,但不适用于iOS。
Another interesting project is the flooh / chips project on which the C64 Docker image is based, which I mentioned in the Dockerfile FROM AS — C64 Emulator blog post. I will take a closer look at this project, because different emulators (e.g. Z80, CPC) run in the browser as WebAssembly.
另一个有趣的项目是C64 Docker映像所基于的flooh / chips项目,我在Dockerfile FROM AS — C64 Emulator博客文章中提到了该项目 。 我将仔细研究这个项目,因为不同的仿真器(例如Z80,CPC)在浏览器中作为WebAssembly运行。
But it’s enough for now. I have two iOS apps with different C64 BASIC interpreters:
但这已经足够了。 我有两个具有不同C64 BASIC解释器的iOS应用程序:
In an upcoming blog post I will take a look at the RND function.
在即将发布的博客文章中,我将介绍RND功能。
99 END
99结束
Originally published at https://www.larsgregori.de on May 22, 2020.
最初于 2020年5月22日 在 https://www.larsgregori.de 上 发布 。
ios push上移64