Giter Club home page Giter Club logo

Comments (6)

waltje avatar waltje commented on August 24, 2024 1

from 6502.

redcode avatar redcode commented on August 24, 2024

Yes. It's very easy. The m6502_power() sets the registers to the values they would be after a real CPU is powered ON (or OFF).
Passing state as TRUE initializes the registers with the values after power ON. Passing FALSE clears the registers (that's good to show them as 0 in a debugger, for example).

The following example emulates a fictional machine. A linear video framebuffer is assumed to be located at address F000h, with indexed colors, palette of 16 colors, 1 byte per index. The machine produces an IRQ during the VBLANK interval:

#define MACHINE_SCREEN_WIDTH        352
#define MACHINE_SCREEN_HEIGHT       296
#define MACHINE_VISIBLE_SCANLINES   MACHINE_SCREEN_HEIGHT
#define MACHINE_VBLANK_SCANLINES    16
#define MACHINE_VRAM_ADDRESS	    0xF000

#define MACHINE_CYCLES_PER_FRAME    69888
#define MACHINE_CYCLES_PER_IRQ	    32

#define MACHINE_CYCLES_PER_SCANLINE \
	(MACHINE_CYCLES_PER_FRAME / (MACHINE_VISIBLE_SCANLINES + MACHINE_VBLANK_SCANLINES))

#define MACHINE_CYCLES_PER_VBLANK \
	(MACHINE_CYCLES_PER_SCANLINE * MACHINE_VBLANK_SCANLINES)

#define MACHINE_CYCLES_AT_IRQ               24
#define MACHINE_CYCLES_AT_IRQ_END           (MACHINE_CYCLES_AT_IRQ + MACHINE_CYCLES_PER_IRQ)
#define MACHINE_CYCLES_AT_VISIBLE_SCANLINES MACHINE_CYCLES_PER_VBLANK


#define RGBA(r, g, b) 0x##FF##b##g##r

zuint32 const color_palette[16] = {
	RGBA(00, 00, 00), RGBA(00, 00, D7), RGBA(D7, 00, 00), RGBA(D7, 00, D7),
	RGBA(00, D7, 00), RGBA(00, D7, D7), RGBA(D7, D7, 00), RGBA(D7, D7, D7),
	RGBA(00, 00, 00), RGBA(00, 00, FF), RGBA(FF, 00, 00), RGBA(FF, 00, FF),
	RGBA(00, FF, 00), RGBA(00, FF, FF), RGBA(FF, FF, 00), RGBA(FF, FF, FF)
};


typedef struct {
	M6502 cpu;
	zuint8 memory[65535];
	zusize frame_cycles;
	zuint32 *video_output_buffer;
} Machine;


zuint8 cpu_read(Machine *self, zuint16 address)
	{return self->memory[address];}


void cpu_write(Machine *self, zuint16 address, zuint8 value)
	{self->memory[address] = value;}


void machine_initialize(Machine *self, zuint32 *video_output_buffer)
	{
	self->cpu.context = self;
	self->cpu.read = cpu_read;
	self->cpu.write = cpu_write;
	m6502_power(&self->cpu, TRUE);
	memset(self->memory, 0, 65535);
	self->video_output_buffer = video_output_buffer;
	self->cycles = 0;
	}


void machine_run_frame(Machine *self)
	{
	zuint    y, x;
	zuint8*  vram_input = &self->memory[MACHINE_VRAM_ADDRESS];
	zuint32* video_output = self->video_output_buffer;

	/* VBLANK before IRQ */
	self->cycles += m6502_run(&self->cpu, MACHINE_CYCLES_AT_IRQ - self->cycles);

	/* IRQ */
	m6502_irq(&self->cpu, TRUE);
	self->cycles += m6502_irq(&self->cpu, MACHINE_CYCLES_AT_IRQ_END - self->cycles);
	m6502_irq(&self->cpu, FALSE);

	/* VBLANK after IRQ */
	self->cycles += m6502_run(&self->cpu, MACHINE_CYCLES_AT_VISIBLE_SCANLINES - self->cycles);

	/* Visible scanlines */

	for (y = 0; y < MACHINE_VISIBLE_SCANLINES; y++)
		{
		self->cycles = m6502_run(
			&self->cpu,
			MACHINE_CYCLES_AT_VISIBLE_SCANLINES     +
			((y + 1) * MACHINE_CYCLES_PER_SCANLINE) -
			self->cycles);

		/* Draw scanline pixels in the output buffer */
		for (x = 0; x < MACHINE_SCREEN_WIDTH; x++)
			*video_output++ = color_palette[*vram_input++];
		}

	self->cycles -= MACHINE_CYCLES_PER_FRAME;
	}


int main(int argc, char *argv)
	{
	zuint32 video_buffer[MACHINE_SCREEN_WIDTH * MACHINE_SCREEN_HEIGHT];
	Machine machine;

	machine_initialize(&machine, video_buffer);

	while (1)
		{
		machine_run_frame(&machine);

		/* Draw frame */
		...
		}

	return 0;
	}

from 6502.

redcode avatar redcode commented on August 24, 2024

If you have more doubts, feel free to join my Discord server:
https://discord.gg/NeTytxBh
The link will be valid for 7 days.

from 6502.

leap0x7b avatar leap0x7b commented on August 24, 2024
main.c:35:15: error: invalid operands to binary expression ('size_t' (aka 'unsigned long') and 'void')
     self->cycles += m6502_irq(&self->cpu, MACHINE_CYCLES_AT_IRQ_END - self->cycles);
     ~~~~~~~~~~~~ ^  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

from 6502.

waltje avatar waltje commented on August 24, 2024

This code could indeed use a "simple emulator" framework that can be used with the CPU backend. Possibly this could also be used for the Z80 backend, as they function similarly.

from 6502.

redcode avatar redcode commented on August 24, 2024

I have another example for the Z80 documentation: https://zxe.io/software/Z80/documentation/latest/Usage.html

The documentation is not finished yet, but it will be soon.

from 6502.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.