Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Designing a simple CPU  (Read 3354 times)
0 Members and 1 Guest are viewing this topic.
Offline sci4me

Junior Devvie





« Posted 2013-07-05 11:53:21 »

Hey guys. Now I know the title makes this seem very non-game related, but this is actually for a game I am working on. So, basically, I want to design a simple CPU. I have done this in the past, but I have always simplified things. I haven't touched interrupts. All I have done is a basic cpu with registers, an instruction set, etc. but never any more. Now, for this game, I want to have a CPU of my own design that is 32 bit (64 maybe? probably not needed... but maybe) That is .. well.. maybe a bit more powerful than a Z80? As far as all of its features go. The main thing I need help with is interrupts. I think I pretty much get them, but I have little/no idea how they actually work and how to program them into an emulated CPU. I am going to start out by writing a detailed specification for the CPU to go by. That way, i'll know what im coding. But I would like to get the interrupts thing figured out first. If anyone is able to help me out with this, it would be greatly appreciated! Thanks!

EDIT:
Maybe a more proper way to word it would be a simulated CPU not an emulated one...
Offline sci4me

Junior Devvie





« Reply #1 - Posted 2013-07-06 04:26:53 »

Also, something else I need help with is labels. Or if you want to refer to them as functions.. that works too.. but.. the things where you can type something like this:
1  
2  
3  
4  
test:
    code... blah.. blah...

b test

Or something of that nature... I haven't seem anything about that in any spec for any cpu's that i've looked at... is this something that is handled by the compiler?
Offline HeroesGraveDev

JGO Kernel


Medals: 309
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #2 - Posted 2013-07-06 05:31:36 »

Emulating hardware with software doesn't go well.

Even if you managed to get everything working, I doubt it would be very powerful.
Remember, you are sharing the (real) CPU with all other applications running on the system, including the system itself.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sci4me

Junior Devvie





« Reply #3 - Posted 2013-07-06 05:35:11 »

I didnt mean emulating the actual hardware... I just meant emulating its functionality. It worked great for another CPU I designed. Thing ran fast as my computer. And look up 0x10c.
Offline HeroesGraveDev

JGO Kernel


Medals: 309
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #4 - Posted 2013-07-06 05:49:25 »

In that case, it would be easy (relatively).

For labels:
When parsing the code, split it up into each of the labels. Then you just use the code in each label as it becomes neccessary.

For interrupts:
Read what an interrupt does, and do exactly that (sort of).

Offline sci4me

Junior Devvie





« Reply #5 - Posted 2013-07-06 05:52:18 »

Easier said than done...

When parsing code... split it up by the labels and use them when needed... uh... how would that work in compiled code? i mean .. how would the cpu process that...
Offline HeroesGraveDev

JGO Kernel


Medals: 309
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #6 - Posted 2013-07-06 05:58:14 »

Easier said than done...

When parsing code... split it up by the labels and use them when needed... uh... how would that work in compiled code? i mean .. how would the cpu process that...

Depends how you've coded it so far.

If you don't already, you should have different functions for each instruction so you can easily rearrange the code without breaking everything.

Offline sci4me

Junior Devvie





« Reply #7 - Posted 2013-07-06 06:01:56 »

I havent started coding it.. I want to be able to understand how it will work before I try to code it... Thats why I plan to write a spec. But yes, I would have a method for each instruction. I suppose I should explain a little bit of how I envision it functioning:

Each cycle, a method called executeInsn (or something like that) would be called. This function would get the next instruction, increment the program counter and then execute the instruction.

It would of course be more than that (operands) but thats the BASIC idea of how it would work. Thats why I dont really get the labels... because if the program counter just goes up and up, something like this:
1  
2  
3  
4  
5  
6  
7  
8  
test1:
    code...code...blah..

test2:
    code2...code2...blah2..

b test2
b test1

Would be.. well.. bad.
Offline HeroesGraveDev

JGO Kernel


Medals: 309
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #8 - Posted 2013-07-06 06:19:32 »

Do what I said before, and split up the code based on the labels.

Load the entire program, then take all the separate pieces (labels and main (non-label) code) and store them in a HashMap or whatever.
Then have a function like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
// Really simple function.
// Pretty easy to understand.
public void execSection(String labelName)
{
    Label label = labels.get(labelName); // if using a HashMap.

    for(String instruction : label.getInstructions())
    {
        execInstruction(instruction); // parses the string into the instruction
    }
}

(The main code should also be treated like a label, but don't store it in the HashMap)

And then when you hit a jump instruction, simply call
execSection(labelName)

Offline sci4me

Junior Devvie





« Reply #9 - Posted 2013-07-06 07:00:07 »

Okay ... uhm.. yeah. But how do real cpu's do it? I mean... I want it to be as realistic of a simulator as possible... But, if it will run like the real thing would then thats enough I suppose.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline HeroesGraveDev

JGO Kernel


Medals: 309
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #10 - Posted 2013-07-06 07:57:32 »

Okay ... uhm.. yeah. But how do real cpu's do it?

Honestly, I don't know that much about CPUs.
I'm just saying what I know from learning a bit of Assembly.

Offline RobinB

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #11 - Posted 2013-07-06 12:13:28 »

a real CPU has regiser values and just runs them one by one, a label is a refrence to one of these values.
I have simplified the commands a little, but it should give some understandign how it works.
Look into assemby languane for some more info.

Output:
1  
2  
3  
4  
5  
00000000 10
00000001 if(&00000000 == 0) JMP 00000004
00000002 &00000000 += 1
00000003 GOTO &00000001
00000004 //Go on with rest of the code


Labels are converted to these register values at compile time.
so adress &00000000  could have been labeled like "value"

Input:
1  
2  
3  
4  
5  
00000000 10
00000001 if(value == 0) JMP 00000004
00000002 value  += 1
00000003 GOTO &00000001
00000004 //Go on with rest of the code


You can even go deeper, by programming a ALU but that really sucs.
Offline sci4me

Junior Devvie





« Reply #12 - Posted 2013-07-06 12:19:43 »

runs register values one by one? uh... runs register values?
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 288
Exp: 5 years



« Reply #13 - Posted 2013-07-06 13:01:09 »

I can recommend this youtube playlist: Benny's Redstone

Yeah it's minecraft, but it's all about making a CPU from the ground up, and if you know how redstone works, then it's even better. He has other tutorials as well of varying simplicity.
You should also check out Wikipedia to start. You might quickly realize that making a CPU is not as simple as you thought.  Wink
Offline sci4me

Junior Devvie





« Reply #14 - Posted 2013-07-06 13:02:41 »

ooh ive seen that series. i watch thebennybox's tutorials. as far as designing a cpu, ive done it.. but i havent included more advanced things like interrupts. thats why im trying to learn how those things work. i want the cpu im designing now to be more advanced than my previous ones.
Offline RobinB

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #15 - Posted 2013-07-06 13:35:58 »

runs register values one by one? uh... runs register values?

The program gets stored in the program register (memory).
each adress hold an instruction or value.
then the cpu starts at adress 0 and runs untill the end of this page.
Offline jonjava
« Reply #16 - Posted 2013-07-06 13:39:05 »

So, basically, I want to design a simple CPU. I have done this in the past, but I have always simplified things. I haven't touched interrupts. All I have done is a basic cpu with registers, an instruction set, etc. but never any more.

Okay ... uhm.. yeah. But how do real cpu's do it?

runs register values one by one? uh... runs register values?

Seems like you don't know how a CPU really works. Seems like the only reason you want to do an in-game CPU is to learn how a CPU fundamentally works. I suggest you read a good and informative book on the subject. I've personally read "Computer Organization and Design, 4th Ed" by D. A. Patterson and J.L. Hennessy. I recommend it highly.

However, there are other books such as: Behrooz Parhami, Computer Arithmetic: Algorithms and
Hardware Designs


There's a java mips simulator you can use to see and test assembly code: http://courses.missouristate.edu/kenvollmar/mars/

We use some of these in my university.

Offline sci4me

Junior Devvie





« Reply #17 - Posted 2013-07-06 13:43:52 »

Well I dont know that I would say "I dont know how a cpu works" ... but I dont know everything... Here is the link to the first project to do with cpu's I did:
https://github.com/sci4me/S80-CPU
That repo is slightly out of date but .. its close. If you look through the S80 class (the cpu emu) .. well.. is that not a fairly realistic simulator? It is my own design so you may want to look at the spec. It is quite simple... but after looking at this, do you still feel I dont know how a cpu works? If so then I'll .. well.. learn more I guess. Cheesy
Offline Troncoso

JGO Coder


Medals: 20



« Reply #18 - Posted 2013-07-06 17:56:07 »

Well, let's see if we can get some stuff straight here. First of all, there really isn't such thing as a "simple CPU" if you want something that actually functions. I recommend the book "Computer Systems - A Programmer's Perspective 2nd Edition" It goes through everything you need to do to simulate the x86 architecture (albeit with not quite as many instructions).

Things to consider when designing the cpu:

It's not as simple as reading a file and calling a function that represents each line.

You'll want to separate the different stages, such as getting the instructions (fetching), determining what they do (decoding), executing them, making changes to memory as needed, and then setting up the registers for the next cycle.

You'll want to look into pipe-lining (basically what I mentioned above). There are different methods of pipe-lining though, so it's good to know your options.

You'll need registers to store temporary data.

You'll need some memory for longer term storage.

You need to know what instructions you want to support and assign them all op-codes and operands. op code being the unique ID of an instruction and the operands being the information needed by the instruction to carry out whatever it does.

Labels are accomplished with a symbol table. You need map for each label and the address it corresponds to. This means you need to read through your assembly code twice. Once to build the symbol table and a second time to execute the instructions. (After being loaded into your memory) Symbols are used with jump instructions. So, when you call a jump, it's as simple as getting the address from the symbol table and setting the program counter to that.

Interrupts are exactly what they sound like. Though, what you don't understand is that is something that can be implemented on the code level. In your assembly code, you just create a subfunction that acts as the interrupt code to run, then whenever it needs to be called, you jump to it, and at the end of the subfunction, you return back to where you were. Case in point, that's not something you need to implement in your simulator.

This is really an extensive topic. I spent an entire semester learning nothing but how the CPU works and building my own simulator. It's not something you are going to be able to tackle in a few days time.
Offline sproingie

JGO Kernel


Medals: 202



« Reply #19 - Posted 2013-07-06 21:22:55 »

Start with a virtual machine, which is much easier than trying to emulate a real CPU.  You don't need to deal with interrupts, pipelines, or any of that nonsense.  Don't bother writing an assembler first, just write programs in raw opcodes.  A stack-based VM is really easy to write and good to start with, though register-based is ultimately where it's at as interesting VMs go (the JVM is stack-based but it's more of an exception than the rule).  Writing register allocation algorithms will teach you some really interesting computer science principles too.
Offline sci4me

Junior Devvie





« Reply #20 - Posted 2013-07-06 22:36:44 »

Well, let's see if we can get some stuff straight here. First of all, there really isn't such thing as a "simple CPU" if you want something that actually functions. I recommend the book "Computer Systems - A Programmer's Perspective 2nd Edition" It goes through everything you need to do to simulate the x86 architecture (albeit with not quite as many instructions).

Things to consider when designing the cpu:

It's not as simple as reading a file and calling a function that represents each line.

You'll want to separate the different stages, such as getting the instructions (fetching), determining what they do (decoding), executing them, making changes to memory as needed, and then setting up the registers for the next cycle.

You'll want to look into pipe-lining (basically what I mentioned above). There are different methods of pipe-lining though, so it's good to know your options.

You'll need registers to store temporary data.

You'll need some memory for longer term storage.

You need to know what instructions you want to support and assign them all op-codes and operands. op code being the unique ID of an instruction and the operands being the information needed by the instruction to carry out whatever it does.

Labels are accomplished with a symbol table. You need map for each label and the address it corresponds to. This means you need to read through your assembly code twice. Once to build the symbol table and a second time to execute the instructions. (After being loaded into your memory) Symbols are used with jump instructions. So, when you call a jump, it's as simple as getting the address from the symbol table and setting the program counter to that.

Interrupts are exactly what they sound like. Though, what you don't understand is that is something that can be implemented on the code level. In your assembly code, you just create a subfunction that acts as the interrupt code to run, then whenever it needs to be called, you jump to it, and at the end of the subfunction, you return back to where you were. Case in point, that's not something you need to implement in your simulator.

This is really an extensive topic. I spent an entire semester learning nothing but how the CPU works and building my own simulator. It's not something you are going to be able to tackle in a few days time.

Did you look at that github? The S80 class? If that doesnt qualify as a simulation of a custom designed cpu then I dont know what does. But yes, you're still right. I don't know all of what I need to know. I've only started to scratch the surface as far as learning how cpu's actually function... but still...
Online Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #21 - Posted 2013-07-07 13:06:23 »

Labels are accomplished with a symbol table. You need map for each label and the address it corresponds to. This means you need to read through your assembly code twice. Once to build the symbol table and a second time to execute the instructions. (After being loaded into your memory) Symbols are used with jump instructions. So, when you call a jump, it's as simple as getting the address from the symbol table and setting the program counter to that.

I think you confused compiler functionality and the execution phase. The compiler creates the symbol table, and uses it to calculate the absolute or relative jumps, storing the results directly as operands.

So an unconditional jump wouldn't be:
1  
program_counter = symbol_table[decoded_label_index]

as that level of indirection is already resolved by the compiler, so we'd simply do:
1  
program_counter = decoded_target_address

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Online Roquen
« Reply #22 - Posted 2013-07-09 10:00:23 »

I'm not clear on the goal, but along the same lines as sproingie suggestion, instead of a CPU...implement forth-a-like instead is an interesting exercise.
Offline lcass
« Reply #23 - Posted 2013-08-22 20:37:02 »

1: for this I would say dont go complicated do something like the redpower2 computers, they are very effective and do pretty much what you are asking for.
2: probably dont need to worry about bits.
3: To make your life a heck of a lot easier write an interpreter for the incoming code.
4: if you want make an xml that holds your commands.
5: yes as some people have said do something similar to a fouth language, people know it  it's a lot easier to code and its commands are simple.

Anyway hope this goes well. Just dont try to simulate full computer functions that will not only destroy your tick and frame rate but also turn into inception and when you dont have a crazy guy who jumps into dreams then gets everyone killed with his dead wife and a train well, its not gonna work.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (75 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (46 views)
2014-11-29 21:32:03

toopeicgaming1999 (113 views)
2014-11-26 15:22:04

toopeicgaming1999 (100 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!