23

我对虚拟机编程很感兴趣,没有什么像 virtualbox 或 vmware 那样花哨,而是可以模拟简单架构的东西,无论是 cisc 还是 risc,比如 Zilog、SPARC、MIPS 或 80686 架构模型。

我想通过这样做,制作同类型的模拟器会相对简单,我只是有兴趣将它用于体验而不是其他任何东西(作为我的第一个 C 项目,我宁愿在 C 中而不是在还要别的吗)。

4

7 回答 7

14

看看其他人在这方面做了什么!

获取有关特定类型应用程序的信息的好方法(在您的情况下也是获取 c 习惯用法的好方法)是查看相同类型的开源项目的结构和细节。一个人可能会决定只是偷看、简要回顾然后“忘记”,以便从头开始自己的项目,但在所有情况下,这种类型的访问都是有益的。

既然你提到了“简单架构”和 Zilog,我认为Z80 处理器可能是一个很好的匹配。由于各种原因,Z80 模拟器类型中有许多当前和过去的项目。顺便说一句,原因之一是 Z80 上运行了许多旧的插槽式视频控制台,这促使怀旧的游戏玩家编写模拟器来运行他们最喜欢的旧游戏 ;-)

此类项目的一个示例是YAZE-AG,它包括一个完整的 Z80 仿真器以及C/PM。整个东西都是用 C 写的。它也比较成熟(2.x 版)和活跃的。我猜这是一个非常小的团队的工作(可能是一个;-))。

祝你好运!

于 2009-11-05T06:06:21.013 回答
11

如果您正在设计 CPU 并对其进行仿真,

准备好核心。意思是,为寄存器编写类。写一个作为标志。写一个内存控制器。

想想操作码的类型。另外,单词的长度是多少?它是 16 位 CPU 吗?8位?

你想使用什么类型的内存访问?DMA?高清晰DMA?

你想支持什么类型的中断?CPU会成为一个学习平台吗?它只是一个 CPU 和一些内存,还是实际上连接了设备?(声音、视频等)。

这是我正在处理的模拟器中的一些代码(公共领域)。这几天一直在努力。到目前为止大约 3200 行代码(大部分是 microcode.cs,由于它的大小为 2600 行,因此未在此处发布)。

using System;

namespace SYSTEM.cpu
{
    // NOTE: Only level-trigger interrupts are planned right now

    // To implement:
    // - microcode
    // - execution unit
    // - etc

    // This is the "core"; think of the CPU core like a building. You have several departments; flags, memory and registers
    // Microcode is external

    class core
    {
        public cpu_flags flags;
        public cpu_registers registers;

        public cpu_memory memory;

        public core(byte[] ROM, byte[] PRG)
        {
            flags = new cpu_flags();
            registers = new cpu_registers();

            memory = new cpu_memory(ROM, PRG);

            return;
        }
    }
}

using System;

namespace SYSTEM.cpu
{
    class cpu_flags
    {
        // SYSTEM is not a 6502 emulator. The flags here, however, are exactly named as in 6502's SR
        // They do NOT, however, WORK the same as in 6502. They are intended to similar uses, but the only identity is the naming.

        // I just like the 6502's naming and whatnot.

        // This would otherwise be a register in SYSTEM.cpu_core.cpu_registers. SR, with the bits used correctly.
        // This would be less readable, code-wise, so I've opted to dedicate an entire CLASS to the status register

        // Though, I should implement here a function for putting the flags in a byte, so "SR" can be pushed when servicing interrupts

        public bool negative, // set if the high bit of the result of the last operation was 1

            // bit 7, then so on
                overflow, // says whether the last arithmetic operation resulted in overflow (NOTE: No subtraction opcodes available in SYSTEM)

                // NO FLAG

                brk, // break flag, set when a BREAK instruction is executed

                // NO FLAG (would be decimal flag, but I don't see why anyone would want BCD. If you want it, go implement it in my emulator; in software)
                    // i.e. don't implement it in SYSTEM; write it in SYSTEM ASM and run it in SYSTEM's DEBUGGER

                irq,    // whether or not an interrupt should begin at the next interrupt period (if false, no interrupt)

                zero, // says whether the last arithmetic operation resulted in zero

                carry; // set when alpha rolls from 0xFFFF to 0x0000, or when a 1 is rotated/shifted during arithmetic

        public cpu_flags()
        {
            negative = true; // all arithmetic registers are FFFF by default, so of course they are negative

            overflow = false; // obviously, because no arithmetic operation has been performed yet

            brk = false;

            irq = true; // interrupts are enabled by default of course

            zero = false; // obviously, since all arith regs are not zero by default

            carry = false;  // obviously, since no carry operation was performed

            return;
        }

        // Explain:

        // These flags are public. No point putting much management on them here, since they are boolean

        // The opcodes that SYSTEM supports, will act on these flags. This is just here for code clarity/organisation

    }
}

using System;



// This implements the memory controller

// NOTE: NO BANK SWITCHING IMPLEMENTED, AND NOT PLANNED AT THE MOMENT, SO MAKE DO WITH TEH 64

// SYSTEM has a 16-bit address bus (and the maximum memory supported; 64K)
// SYSTEM also has a 16-bit data bus; 8-bit operations are also performed here, they just use the low bits

// 0x0000-0x00FF is stack
// 0xF000-0xFFFF is mapped to BIOS ROM, and read-only; this is where BIOS is loaded on startup.
// (meaning PROGRAM ROM can be up to 4096B, or 4K. Normally this will be used for loading a BIOS)
// Mapping other PROGRAM ROM should start from 0x0100, but execution should start from 0xF000, where ROM/BIOS is mapped

// NOTE: PROGRAM ROM IS 32K, and mapped from 0x0100 to 0x80FF

// ;-)

namespace SYSTEM.cpu
{
    class cpu_memory
    {
        // to implement:
        // device interaction (certain addresses in ROM should be writeable by external device, connected to the controller)
        // anything else that comes to mind.

        // Oh, and bank switching, if feasible

        private byte[] RAM; // As in the bull? ...

        public cpu_memory(byte[] ROM, byte[] PRG)
        {
            // Some code here can be condensed, but for the interest of readability, it is optimized for readability. Not space.

            // Checking whether environment is sane... SYSTEM is grinning and holding a spatula. Guess not.
            if(ROM.Length > 4096) throw new Exception("****SYSINIT PANIC****: BIOS ROM size INCORRECT. MUST be  within 4096 BYTES. STOP");

            if (PRG.Length > 32768) throw new Exception("****SYSINIT PANIC**** PROGRAM ROM size INCORRECT. MUST be within 61184 BYTES. STOP");

            if(ROM.Length != 4096) // Pads ROM to be 4096 bytes, if size is not exact
            {                       // This would not be done on a physical implementation of SYSTEM, but I feel like being kind to the lazy
                this.RAM = ROM;
                ROM = new byte[4096];
                for(int i = 0x000; i < RAM.Length; i++) ROM[i] = this.RAM[i];
            }

            if(PRG.Length != 32768) // Pads PRG to be 61184 bytes, if size is not exact
            {                   // again, being nice to lazy people..
                this.RAM = PRG;
                PRG = new byte[32768];
                for(int i = 0x000; i < RAM.Length; i++) PRG[i] = RAM[i];
            }

            this.RAM = new byte[0x10000]; // 64K of memory, the max supported

            // Initialize all bytes in the stack, to 0xFF
            for (int i = 0; i < 0x100; i++) this.RAM[i] = 0xFF; // This is redundant, but desired, for my own undisclosed reasons.

        // LOAD PROGRAM ROM AND BIOS ROM INTO MEMORY

            for (int i = 0xf000; i < 0x10000; i++)  // LOAD BIOS ROM INTO MEMORY
            {
                this.RAM[i] = ROM[i - 0xf000]; // yeah, pretty easy actually
            }

            // Remember, 0x0100-0x80FF is for PROGRAM ROM

            for (int i = 0x0100; i < 0x8100; i++) // LOAD PROGRAM ROM INTO MEMORY
            {
                this.RAM[i] = PRG[i - 0x100]; // not that you knew it would be much different
            }

            // The rest, 0x8100-0xEFFF, is reserved for now (the programmer can use it freely, as well as where PRG is loaded).
            // still read/writeable though

            return;
        }

// READ/WRITE:

        // NOTE: SYSTEM's cpu is LITTLE ENDIAN
        // WHEN DOUBLE-READING, THE BYTE-ORDER IS CONVERTED TO BIG ENDIAN
        // WHEN DOUBLE-WRITING, THE BYTE TO WRITE IS BIG ENDIAN, AND CONVERTED TO LITTLE ENDIAN

        // CPU HAS MAR/MBR, but the MEMORY CONTROLLER has ITS OWN REGISTERS for this?

    // SINGLE OPERATIONS

        public byte read_single(ref cpu_registers registers, ushort address) // READ A SINGLE BYTE
        {                               // reading from any memory location is allowed, so this is simple
            registers.memoryAddress = address;
            return registers.memoryBuffer8 = this.RAM[registers.memoryAddress];

        }

        public ushort read_double(ref cpu_registers registers, ushort address) // READ TWO BYTES (converted to BIG ENDIAN byte order)
        {
            ushort ret = this.RAM[++address];
            ret <<= 8;
            ret |= this.RAM[--address];

            registers.memoryAddress = address;
            registers.memoryBuffer16 = ret;

            return registers.memoryBuffer16;
        }

        public void write_single(ref cpu_registers registers, ushort address, byte mbr_single) // WRITE A SINGLE BYTE
        {
            if (address < 0x0100) return; // block write to the stack (0x0000-0x00FF)
            if (address > 0xEFFF) return; // block writes to ROM area (0xF000-0xFFFF)

            registers.memoryAddress = address;
            registers.memoryBuffer8 = mbr_single;

            this.RAM[registers.memoryAddress] = registers.memoryBuffer8;

            return;
        }

        public void write_double(ref cpu_registers registers, ushort address, ushort mbr_double) // WRITE TWO BYTES (converted to LITTLE ENDIAN ORDER)
        {
            // writes to stack are blocked (0x0000-0x00FF)
            // writes to ROM are blocked   (0xF000-0xFFFF)

            write_single(ref registers, ++address, (byte)(mbr_double >> 8));
            write_single(ref registers, --address, (byte)(mbr_double & 0xff));

            registers.memoryBuffer16 = mbr_double;
            return;
        }

        public byte pop_single(ref cpu_registers registers) // POP ONE BYTE OFF STACK
        {
            return read_single(ref registers, registers.stackPointer++);
        }

        public ushort pop_double(ref cpu_registers registers) // POP TWO BYTES OFF STACK
        {
            ushort tmp = registers.stackPointer++;          ++registers.stackPointer;
            return read_double(ref registers, tmp);
        }

    // PUSH isn't as easy, since we can't use write_single() or write_double()
    // because those are for external writes and they block writes to the stack
    // external writes to the stack are possible of course, but
        // these are done here through push_single() and push_double()

        public void push_single(ref cpu_registers registers, byte VALUE) // PUSH ONE BYTE
        {
            registers.memoryAddress = --registers.stackPointer;
            registers.memoryBuffer8 = VALUE;

            this.RAM[registers.memoryAddress] = registers.memoryBuffer8;
            return;
        }

        public void push_double(ref cpu_registers registers, ushort VALUE) // PUSH TWO BYTES
        {
            this.RAM[--registers.stackPointer] = (byte)(VALUE >> 8);
            this.RAM[--registers.stackPointer] = (byte)(VALUE & 0xff);

            registers.memoryAddress = registers.stackPointer;
            registers.memoryBuffer16 = VALUE;

            return;
        }
    }
}

using System;

namespace SYSTEM.cpu
{
    // Contains the class for handling registers. Quite simple really.

    class cpu_registers
    {
        private byte sp, cop; // stack pointer, current opcode
        //

        private ushort pp, ip, // program pointer, interrupt pointer
            mar, mbr_hybrid; // memory address and memory buffer registers,
                        // store address being operated on, store data being read/written
                        // mbr is essentially the data bus; as said, it supports both 16 and 8 bit operation.

                        // There are properties in this class for handling mbr in 16-bit or 8-bit capacity, accordingly
                        // NOTE: Paged memory can be used, but this is handled by opcodes, otherwise the memory addressing
                        //       is absolute

                        // NOTE: sp is also an address bus, but used on the stack (0x0000-0x00ff) only
                        // when pushing to the stack, or pulling, mbr gets updated in 8-bit capacity



                        // For pulling 16-bit word from stack, shifting register 8 left is needed, otherwise the next 
                        // POP operation will override the result of the last

        // Alpha is accumulator, the rest are general purpose
        public ushort alphaX, bravoX, charlieX, deltaX;

        public cpu_registers()
        {
            sp = 0xFF;  // stack; push left, pop right
            // stack is from 0x0000-0x00ff in memory
            pp = 0xf000; // execution starts from 0xf000; ROM is loaded
            // from 0xf000-0xffff, so 4KB of ROM. 
            // 0xf000-0xffff cannot be written to in software; though this disable
            // self-modifying code, effectively.

            ip = pp; // interrupt pointer starts from the same place as pp

            alphaX = bravoX = charlieX = deltaX = 0xffff;

            cop = 0x00; // whatever opcode 0x00 is, cop is that on init

            mar = mbr_hybrid = 0x0000;

            return;
        }

        // Registers:

        public ushort memoryAddress // no restrictions on read/write, but obviously it needs to be handled with care for this register
        {                       // This should ONLY be handled by the execution unit, when actually loading instructions from memory
            set { mar = value; }
            get { return mar; }
        }

    // NOTE: 8-bit and 16-bit address bus are shared, but address bus must have all bits written.
    // when writing 8-bit value, byte-signal gets split. Like how an audio/video splitter works.

        public byte memoryBuffer8 // treats address bus as 8-bit, load one byte
        {
            set {   // byte is loaded into both low and high byte in mbr (i.e. it is split to create duplicates, for a 16-bit signal)
                mbr_hybrid &= 0x0000;   
                mbr_hybrid |= (ushort)value;
                mbr_hybrid <<= 0x08;
                mbr_hybrid |= (ushort)value;
            } get {
                return (byte)mbr_hybrid;
            }
        }

        public ushort memoryBuffer16 // treats address bus as 16-bit, load two bytes
        {
            set {
                mbr_hybrid &= 0x0000;
                mbr_hybrid |= value;
            } get {
                return mbr_hybrid;
            }
        }

        public byte stackPointer // sp is writable, but only push/pull opcodes
        {                        // should be able to write to it. There SHOULD
            set { sp = value; }  // be opcodes for reading from it
            get { return sp; }
        }

        public byte currentOpcode
        {
            set { cop = value; }
            get { return cop; }
        }

        public ushort programPointer // says where an instruction is being executed from
        {
            set { pp = value; }
            get { return pp; }
        }

        public ushort interruptPointer // says where the next requested interrupt should begin 
        {                   // (copied into PP, after pushing relevant registers)
            set { ip = value; }
            get { return ip; }
        }

        public byte status(cpu_flags flags) // status word, containing all flags
        {
            byte ret = 0;
            if (flags.negative) ret |= 0x80;
            if (flags.overflow) ret |= 0x40;
            if (flags.brk) ret |= 0x10;
            if (flags.irq) ret |= 0x04;
            if (flags.zero) ret |= 0x02;
            if (flags.carry) ret |= 0x01;

            return ret;
        }

    }
}

using System;

using System.Collections.Generic;

namespace SYSTEM.cpu
{
    class cpu_execution
    {
        public core processor; // the "core", detailing the CPU status, including memory, memory controller, etc
        public cpu_microcode microcode; // the microcode unit (note, microcode is plug and play, you could use something else here)

        public cpu_execution(byte[] ROM, byte[] PRG) // initialize execution unit and everything under it
        {
            processor = new core(ROM, PRG);
            microcode = new cpu_microcode();

            return;
        }

        public void fetch() // fetch current instruction
        {
            processor.registers.currentOpcode = processor.memory.read_single(ref processor.registers, processor.registers.programPointer);
            return;
        }

        public void execute() // execute current instruction
        {
            processor = microcode.use(processor);
            return;
        }



    }
}

模拟操作码的 microcode.cs 不包含在此处,因为它有 2600 行代码。

所有这些都是 C#。

于 2010-10-09T18:19:30.250 回答
7

我建议查看Elements of Computing Systems一书。在阅读本书的过程中,您将从基本的逻辑门开始构建一个虚拟计算机。当你读完这本书时,你将拥有一个基本的操作系统、编译器等
。可在线获得的源代码也在 Java 之上实现了计算机的体系结构。

于 2011-07-13T12:28:31.297 回答
6

一个常见的练习是构建一个简单的计算器。它只有有限数量的操作(通常为 4, ),一种数据类型(数字),您可能对它应该* / + -如何工作有很好的理解。这使调试变得容易得多。

尽管很简单,但您已经必须处理一些基本的 VM 问题。您需要解析命令序列、存储您正在处理的多个对象并处理输出。

巧合的是,计算器 IC 是 CPU 的先驱,因此从历史的角度来看,这种方法也很有意义。

于 2009-11-05T09:34:42.107 回答
4

Zilog 时代的东西会很好,因为您可能会找到一些在真正的 Z-80 机器上运行的软件并将其用作最终测试。

我编写的第一个真正的程序(除了一页课堂作业)是我在高中时使用的 HP2100A 小型机的模拟器。我是用 C 的前身 B 写的,我不认为这对于第一个 C 程序来说太难了。如果有的话,它可能太简单了。当然,像 80686 这样的东西比 Z-80 更具挑战性,但 QEMU、VirtualBox 和其他公司已经做到了。

其中最困难的部分将是连接机器与外部世界的整个中断系统。

您可能想阅读有关 LLVM 的内容并决定您是否真的要编写 VM 或仿真器。

于 2009-11-05T09:54:46.983 回答
4

一些想法:

  • 较旧的指令集会更简单,因此它们可能是一个很好的起点。
  • 选择 risc 架构:解码指令流会容易得多。
  • 忽略中断、NMI 等。
  • 准确地模拟启动总是有很多繁琐的细节。相反,选择一些非常简单的东西,比如从地址零开始执行,所有寄存器都设置为零。
  • 真正的程序也需要像真正的硬件仿真这样的东西,所以不要那样做。
  • 您可能希望使用一些特殊的 i/o 指令来扩展指令集以读取数字、写入字符(甚至是字符串)等,这样您就可以编写实际上执行非常简单 i/o 的简单测试程序。
  • 解析像 elf 这样的目标文件格式本身就需要做很多工作。使用 objdump 之类的工具,您可能可以仅将文本部分(即指令)提取为二进制(至少 ascii hex)。
  • 首先为您想要模拟的任何指令集编写反汇编程序,或者至少为您将要拍摄的初始子集编写反汇编程序。无论如何,您都需要它进行调试。
  • 了解如何让您选择的指令集工作的 gas(gnu 汇编器),这样您就可以生成已知的良好目标文件和测试程序。

除非您了解其他编程语言,和/或对汇编程序有合理的理解,否则这是一个非常具有挑战性的第一个 C 项目。永远不会少,祝你好运!

于 2009-11-05T06:07:31.073 回答
2

这不是产品代言,而是一种观察……

我会拿起一本 Deitel 和 Deitel 的书开始。(如果你想用 C 语言做的话,可能是这个)他们似乎总是有一章关于制作虚拟机,以及一些为你的虚拟机编写汇编代码的说明,不管他们教的是什么语言。

编辑 - 添加

(虽然我会在购买之前在图书馆检查一下,以防我误解了你想写的内容)

于 2009-11-05T05:37:59.810 回答