LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-20-2009, 12:43 PM   #1
Anikom15
LQ Newbie
 
Registered: Dec 2009
Posts: 8

Rep: Reputation: 0
I'm writing a CHIP-8 emulator, and have a lot of questions.


I'm writing it in C. Here's the current code: (it's a work in progress)
Code:
#include <stdio.h>

main()
{
	unsigned int memory[0xfff];
	unsigned int stack[0xf];
	unsigned int V[0xf];
	unsigned int I;
	unsigned int delay = 0;
	unsigned int sound = 0;
	unsigned int PC;
	unsigned int SP;
	unsigned int opcode;

	unsigned int xres = 64;
	unsigned int yres = 32;

	for(;;) {
		opcode = memory[PC++];

		switch (opcode) {
			case 0x0:
				if ((opcode& 0x00ff) == 0xe0) {
					/* Clear display */
					break;
				}
				if ((opcode& 0x00ff) == 0xee) {
					/* Return from a subroutine */
					break;
				}
				break;
			case 0x1:
				/* Jump to 0x0fff */
				PC = opcode& 0x0fff;
				break;
			case 0x2:
				/* Call CHIP-8 sub-routine at 0x0fff */
				break;
		}
		PC += 2;
	}

	printf("Bye.\n");
	return 0;
}
1. What's the best method for doing the graphics and sound?

2. What's the "sub-routine" and how do I implement it?

3. How do I load a ROM file from the commandline like this:
Code:
$ <executable> <romfilename>
Thx.
 
Old 12-20-2009, 01:11 PM   #2
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
What's the best method for doing the graphics and sound?
SDL or switch to MS-DOS.
Quote:
What's the "sub-routine" and how do I implement it
you put the value of PC+sizeof(procedure-opcode) on the stack then incrment the stack pointer.
when you return, you deincrment the stack pointer and jump to the address at STACK + SP.
Quote:
How do I load a ROM file from the commandline like this:
main (int argc, char **argv){}
argv is a list of strings passed on the command line.
 
Old 12-20-2009, 01:53 PM   #3
Anikom15
LQ Newbie
 
Registered: Dec 2009
Posts: 8

Original Poster
Rep: Reputation: 0
Ok. I can get it from the commandline, but how do I put the ROM information into the emulator's "memory"?
 
Old 12-20-2009, 04:30 PM   #4
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Code:
FILE *f = fopen(argv[1], "rb");
fread(&memory, 1, 4096, f);
fclose(f);
That will load the file into the emulators memory.
 
Old 12-21-2009, 12:30 PM   #5
Anikom15
LQ Newbie
 
Registered: Dec 2009
Posts: 8

Original Poster
Rep: Reputation: 0
Ok, what should the value of "procedure" be?
 
Old 12-21-2009, 05:46 PM   #6
Anikom15
LQ Newbie
 
Registered: Dec 2009
Posts: 8

Original Poster
Rep: Reputation: 0
Sorry, double post.

Last edited by Anikom15; 12-21-2009 at 05:47 PM.
 
Old 12-21-2009, 05:47 PM   #7
Anikom15
LQ Newbie
 
Registered: Dec 2009
Posts: 8

Original Poster
Rep: Reputation: 0
Ok, here's a snippet of code:

Code:
case 0xd:
/* Some sprite drawing! */
	for (n = (opcode& 0x000f); n > 0; n--) {
		len = 0;
		PC = memory[I];
		while (len <= 8) {
			putpixel(screen, V[opcode& 0x0f00] + len, V[opcode& 0x00f0] + n, color1);
			len++;
		}
	}
break;
How can I test if a color1 (white) pixel has replaced another white pixel? How can I wrap a sprite around to the other side of the screen if it's too long?
 
Old 12-21-2009, 06:11 PM   #8
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by Anikom15 View Post
Ok, here's a snippet of code:

Code:
case 0xd:
/* Some sprite drawing! */
	for (n = (opcode& 0x000f); n > 0; n--) {
		len = 0;
		PC = memory[I];
		while (len <= 8) {
			putpixel(screen, V[opcode& 0x0f00] + len, V[opcode& 0x00f0] + n, color1);
			len++;
		}
	}
break;
How can I test if a color1 (white) pixel has replaced another white pixel?
you cannot, unless you 'get' a pixel
Quote:
How can I wrap a sprite around to the other side of the screen if it's too long?
Code:
case 0xd:
/* Some sprite drawing! */

	for (n = (opcode& 0x000f); n > 0; n--) {
int wrapx=0,wrapy=0;
while(V[opcode& 0x0f00]+ len - wrapx >  [screen-width]/*replace this*/){wrapx += [screen-width]; /*replace this*/}
while(V[opcode& 0x00f0]+ n - wrapy > [screen-height]/*replace this*/){wrapy += [screen-height]; /*replace this*/}

		len = 0;
		PC = memory[I];
		while (len <= 8) {
			putpixel(screen, V[opcode& 0x0f00] + len - wrapx, V[opcode& 0x00f0] + n - wrapy, color1);
			len++;
		}
	}
break;
 
Old 12-22-2009, 01:16 PM   #9
Anikom15
LQ Newbie
 
Registered: Dec 2009
Posts: 8

Original Poster
Rep: Reputation: 0
Ok how do I implement this opcode:
Quote:
Fx33 - LD B, Vx
Store BCD representation of Vx in memory locations I, I+1, and I+2.

The interpreter takes the decimal value of Vx, and places the hundreds digit in memory at location in I, the tens digit at location I+1, and the ones digit at location I+2.
And I need to scale the entire surface (screen) by 10.

Last edited by Anikom15; 12-22-2009 at 06:10 PM.
 
Old 12-23-2009, 01:23 PM   #10
SS-00
LQ Newbie
 
Registered: Dec 2009
Posts: 2

Rep: Reputation: 0
Question

HeLLo
SoRRy for my post
I want to programming CHIP8 but I want someone explain to me how do that with c++ and sdl i tried before but i can't
please I want someone start and Explain it Line by line
I know CHIP8 instructions
I wait for your answer smeezekitty and Anikom15
 
Old 12-23-2009, 01:47 PM   #11
Anikom15
LQ Newbie
 
Registered: Dec 2009
Posts: 8

Original Poster
Rep: Reputation: 0
You want to program Chip-8 games? You'll need a chip-8 assembler, I don't know if any exist for linux.
 
Old 12-23-2009, 05:28 PM   #12
SS-00
LQ Newbie
 
Registered: Dec 2009
Posts: 2

Rep: Reputation: 0
thank you for your reply
what i want is how I write a CHIP8 Emulator like what are you doing now I want you tou explain to me each line for your program.i know how chip8 work and its component
I hope that does not hinder smeezekitty to help you

Last edited by SS-00; 12-23-2009 at 05:31 PM.
 
Old 12-23-2009, 05:37 PM   #13
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Ok how do I implement this opcode:
Quote:
Fx33 - LD B, Vx
Store BCD representation of Vx in memory locations I, I+1, and I+2.
The interpreter takes the decimal value of Vx, and places the hundreds digit in memory at location in I, the tens digit at location I+1, and the ones digit at location I+2.
Just a guess:
Code:
unsigned char digits[10];
itoa(V[PC+1], digits, 10);
V[I+2] = digits[strlen(digits)] - '0';
V[I+1] = digits[strlen(digits)-1] - '0';
V[I] = digits[strlen(digits]-2] - '0';
Of cource i did not include the switch case, etc.
 
Old 12-23-2009, 11:47 PM   #14
Anikom15
LQ Newbie
 
Registered: Dec 2009
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by SS-00 View Post
thank you for your reply
what i want is how I write a CHIP8 Emulator like what are you doing now I want you tou explain to me each line for your program.i know how chip8 work and its component
I hope that does not hinder smeezekitty to help you
Well, I say read this first.

Then this.

You can take any code of mine if you wish, though it doesn't work yet.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with reading/writing GPIO on video card with bttv compatible chip boky Linux - Hardware 0 10-13-2005 05:08 AM
Need a lot of Help- Several Questions Sleepy_Sentry Linux - Wireless Networking 6 05-16-2005 08:18 AM
mysql lot of questions alaios Linux - Software 1 12-12-2003 05:35 PM
I have a lot of questions Ajai Linux - Newbie 4 07-23-2003 02:38 AM
Lot of Questions Half_Elf Linux - Software 4 09-15-2001 03:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration