[Original] the first wave of NES: How Universal 6502 macro assembler, made with NES / FC games.

  In 163 blog off it. Here re-opened.

  If users have problems after what you want the long answer, I am here to answer.

  As the first wave of original articles, I intend to do a white demonstration. That is step by step to show the use of an assembler.

 

  First, science

  Many people think that the program has nothing to do with the CPU, it is subject to the influence of the C language. In fact, different CPU, its instruction set is different.

  The compilation is for the instruction set, assembly instructions can be considered is the CPU instruction. Of course, addressing different. (Of course, the real CPU instructions are machine code, because the assembler is most close to the CPU, I'll do for granted. Interested friends can learn the relationship between a compiled machine code compilation and study of the future when a large HACK God.)

  Another point to mention is that even if the same is 6502CPU different macro assembler, the code types are slight differences. Naturally, this is because there is a 6502 assembly language standard caused, as there are two standard C language, some of it okay. We used to use naturally understand. The macro aspects, macro assembler that each are quite different, can be said that the Eight Immortals - recount. Macro is dotted prefix instructions.

 

  About NES file structure, we must understand the two. NES files is a cassette Dump come. This means that the tape from the card read byte ROM, document preservation, during which also needs to be decrypted; also identifying the circuit configuration of the cassette (cassette is not a kind of circuit, on the contrary, each manufacturer has its own unique design really streaky one hundred.), some of the information is defined as iNes cassette header.

  NES file structure, we call iNES. It consists of: header + PRG_ROM + CHR_ROM, are connected in order.

  This time we introduce the Mapper = NES production methods 0. This is the most basic, so after all the changes here from the beginning.

  Mapper is a new term, Mapper is an 8-bit ID number which refers to "the circuit configuration of the cassette." That is, when the person Dump simple circuit configuration different set a serial number, as distinguished. The number 0 is the first Nintendo cassette structure, is the simplest, most primitive, no change. It may also be referred to as a prototype.

 

  Header thing, it will not elaborate.

  Program (we call PRG_ROM) by 16K units, when Mapper = 0, Bin program may be 16K or 32K. That long sequence program can only be a maximum of 32K, this limitation. You do not have to worry about when writing program that generates irregular length. There are way behind. But if you want to learn long cut sheet, it is Mapper> 0, then the post.

  Graphics (we call CHR_ROM) by 8K units, when Mapper = 0, graphic Bin can be 0 or 8K. 0 What do you mean, no, do not be mean. So no graphics, it is otherwise blank screen. It is not capable of displaying the picture and graphic pattern. So do the CHR_ROM, then bring the card is not blank, but added something 8K of CHR_RAM. We should understand ROM and RAM. CHR is a graphical means. This sentence can be understood as not to ROM, and replaced RAM, a graphics library is not fixed, and can be transferred from a different graphics program to CHR_RAM go inside. Then the display even more flexible. But spend RAM can not cut the graphics page. After these words will understand.

 

  Second, download

  Science ends. This time I want to explain the universal macro assembler for 6502 is " 6502 Macroassembler & Simulator ." Chinese means " 6502 Macro Assembler and Simulator ", the name is a bit long, we later referred to as "6502Sim". I can download the " network disk ." In particular " NES Tool Kit " folder.

  The following article mentioned tools, as well as follow-up blog post NES used a tool drum brain into this folder until filled, only to put the same name as the No. 2 folder.

 

  Third, introduce the protagonist

  Description: 6502Sim, as a simulator, allows stepping through code to run on it. 6502 compilation and to learn to see changes in all registers of the CPU, as well as changes in the stack. From an intuitive point of view to learn assembly. Debugging naturally produce machine code, machine code that can be saved. This is the features we need. All in all, a very good tool. I offer this is the Chinese version. Inevitably some of the most simple English retained.

  We take a look at the photograph 6502Sim. see picture 1.

Figure 1 6502Sim

 

  It can be seen as long as the cursor points to a command, it will display the corresponding command analysis. As long as the menu "Help", click "dynamic instruction Help '. Very thoughtful feature. I like. Why help is in English? Haha not finished it, anyway, I'm not finished, so we have to learn some proper English. Or with the machine turned themselves to solve it.

  Unfortunately, there is no line number column, if the compiler error, it depends on the coordinates of the lower right corner to find the information. Ln is the line, Col is the column.

  Note: 6502Sim the Directive must be indented one space or a Tab. I do not know why, this may be the author's format, without any prompting on help. I often make this mistake novice. Error did not say clearly, people know to ask. pit!

 

  Fourth, example

  Tuwan grievances. We also need to look at some of what ghost.

  This time I provide a complete sample code, and used to fill a graphic file, in fact, in this example for simplicity's sake, we do not intend to display graphics. However, in order to learn later, or packaged together. Anyway, batch commands used inside.

 

  This is an example of the early works of the maple leaf "Happy Birthday" music.

  Code is relatively simple, easy to examples. (Maple leaf did not give us the original code, to ourselves to disassemble. Tears ran ah.) This code consists of " not a girl offer." I changed a bit small. Type I changed the header section, but also to facilitate learning. I packed the entire file, we go to my network disk download can (have previously linked). Also in "NES Tool Kit".

  Tools and documentation, I packaged together. Because I have a network disk file quantity. But also a pit. Under the list of documents is a must and tools.

 

 Table 1 list

No. Types of name
1 tool 6502Sim
2 tool nesınfo
3 file Happy Birthday .65s
4 file ascii_2.chr
5 文件 make.bat

 

  其它的文件就是自己造出来的了。其实make.bat也能自己写,不过相对有点复杂。我还是留着,还有一个文件头记录我也保留了。等下会说到。

 

  五、步骤

  这是大家最关心的事情了。

  5.1 代码的编辑工具。

  编辑工具用6502Sim就行,也可以用npp,记事本等等。有一点记好了,编码格式要求是"ANSI"。这一点非常重要。小白就用6502Sim,默认就是ansi格式。若用其它工具编辑,那么保存后缀名改为" .65s ",再用6502Sim打开。

  为什么要是65s后缀名?这只是为了区别汇编代码的格式。上面说过了,不同宏汇编器,对应的格式有区别,为了不混淆。若是像txt后缀名,用6502Sim也能打开,只要文件类型选“ *.* ”就行了。

  5.2 代码编译和生成。

  打开文件后,点菜单“仿真”-“编译”。记好这个图标(三张纸一个下箭头),在快捷栏直接点击也行。没错的话,只会闪一下,也没有什么别的提示。唯有快捷栏上编译按扭旁边的Debuger按扭(一个甲虫的样子)亮了。

  以后调试就用这个甲虫按扭,这次不提。

  接下来要生成bin文件,即65b文件。点菜单"文件"-“保存代码”。这次的例子,代码是从$C000开始的,那么直到$FFFF。(这就是16K了,那么这些代码自然不是刚好16K,完全是少于16K的,其中的空白部分编译时自动补全了。)

  填好文件名,文件名不要有空格,为了后面的方便,否则在make.bat里面给文件名加引号。我不建议给自己添麻烦。本例用的文件名是 BinaryCode 配合后面的make.bat。

  选好类型是“*.65b”,这一个不能错。

  还有还有,还要点“选项”。非常重要。不能漏。选项中,填“开始地址” 0xC000,结束地址“0xFFFF”,代码长度会自己算好。

  最后,点确定,再点保存。OK,这样程序Bin就生成了。(若是切页的话,众多的程序Bin,按次序连接就成了PRG_ROM。现在只有一个Bin,那它就是PRG_ROM。)

  5.3 图形Bin文件

  即作为CHR_ROM。这已经有现成的了(ascii_2.chr,有4K),再说这次示例不作图形显示,所以这个图形Bin也就是充数的。为了简单起见,下次重点再说说图形显示的事情。图形Bin的后缀名是chr,因为我是用一个叫YYCHR的软件生成的。

  这文件只有4K,为了补足8K,后面连接2次。下面详说。

  5.4 文件头的制作

  我们用的软件是我自己开发的。我们来看看样子。打开NesInfo程序,见图2。

图2 NesInfo

  

  填上这次,我们要的数值:

  这次我们只谈Mapper=0,那么填0就可以。

  这次程序从C000到FFFF,共16K,那么PRG ROM填1就可以,1x16K的意思呀。

  这次图形定为8K,当然,没有也行,不过为了完整演示,还是加一个补足。CHR ROM填1就好了。

  镜像,S-RAM,4屏幕,TRAINER用不上,默认好了。

  这里简单解释一下,大家暂时不用追求深入了解。

 

表2  名词

  释义
镜像 背景图像的左右或垂直镜像,滚屏用的。
S-RAM 带电池的SRAM,记录游戏数据
4屏幕 4屏联动,废掉镜像的作用。
TRAINER 教练,就是增加512字节的ROM,外加一些游戏示例等。实质是多了一点空间。电路设计麻烦了一点儿。

 

  填好之后,为了以后方便,先保存一下。点击“高级”。见图3。

图3 NesInfo 高级

  按下“另存信息”,就保存一个ini文件。

  (本示例包中,保留了head.ini。可以直接按这个“打开”,载入之前定义好的文件头。)

  最后要生成bin文件才行。我没有设计生成汇编.db形式的,否则生成数值,直观一些。我这个程序功能有点多,好像能够用行指令,那么就能写进make.bat,不细说。

  生成Bin,就是按“保存Bin",就行,他会以打开的文件名,按同名保存成bin,生成后没有太多的反应,就给你一上提勾图标就算了。

  那么,你打开我的head.ini,就会生成一个head.bin。否则没有名字,它会要求你写一个的。本例中名字是head.bin,这要配合后面的make.bat。

 

  5.5 Make批文件

  用批文件的原因是,因为要用到COPY指令。打开win附件记事本。填写如下

copy /b head.bin+BinaryCode.65b+ascii_2.chr+ascii_2.chr Day.nes
pause

  head.bin是我们生成文件头,

  BinaryCode.65b是我们生成的程序,

  ascii_2.chr是图形,本示例用于充数,

  Day.nes是生成的nes文件名。

  记得要写+号,我不晓得文件名出现空格会什么情况,也不知加引号顶不顶用。反正我的文件名不加空格,那就对了。

  后面一个暂停指令,让你看看过程出没出问题。

  保存成文件make.bat。文件类型选*.*,编码选ANSI。选错了出问题,不过ANSI是默认的。不用操心。只是你有可能文件类型忙记选了,保存的还是txt。变成make.bat.txt。

  那么保存后再重命名了,哈哈。

  那么让我们来双击make.bat

  

  正常的如下。见图4。

图4 正常的结果

  按任意键,就结束。nes就生成好了。可以用模拟器打开。就能听到生日快乐音乐。

 

Guess you like

Origin www.cnblogs.com/fogota/p/11298360.html