It is currently Fri Aug 23, 2019 2:09 am

All times are UTC - 7 hours





Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Thu Jul 25, 2019 7:08 am 
Offline

Joined: Tue Jul 23, 2019 10:41 am
Posts: 31
Hi,

Dunno if this is appropriate for this subforum, but I came across this video where a guy programmed a NES emulator in a 1000 lines of code.
Because it was a youtube video he had to make sure to not make it too long, so he made a lookup table for CPU "microinstructions". What I mean is he made a table/matrix that contained stuff like temp = X+=1; and X = temp.
So if there is an instruction that adds 1 to to the X register, the program would look inside the lookup table to see what is needed to do for this instruction, which is temp = X+=1 and then X = temp.
This is what such lookup table looks like:
Code:
if ("0100100"[opcode] == '1') {temp = X+=1;};
if ("0101000"[opcode] == '1') {X = temp;};
if ("0010001"[opcode] == '1') {Y = temp;};

There would of course be way more if's and 0's and 1's, but I hope you get the idea.
So let's say we have a fictional CPU with that is emulated with above table and the currect opcode would be 1, which would be to increment X by one. What it will do is look at the second number in the string, then if it's one it will execute the thing that "microinstruction" is meant to do.
So an instruction that increments by one would execute the first and second microinstruction, but not the last one since it's not needed for the opcode we are handling.
Now I believe the intention that the person in the video did it this way was because it uses less code, but I think it could implementing instructions way easier since you just need to select the right microinstructions for the instruction/opcode being executed.

So what I'm wondering is(and this is why I created this post), are there any other emulators that do this? It seems like a smart way to implement instructions. But it might impact performance(on slower PCs).

Thanks!


Top
 Profile  
 
PostPosted: Thu Jul 25, 2019 9:58 am 
Offline
User avatar

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 7565
Location: Canada
I really love that video, but honestly, this approach is just about optimizing for "simple" code, and probably not widely applicable in any other way than that. In emulators it's common to have shared code between instructions, but not executed by a matrix like that. At least, not to that extent.

In most cases its more practical and more efficient to write cases for individual instructions. Bisqwit pulled it off here, partly because of how simple the 6502 is specifically, partly by deliberately oversimplifying it to make the code more symmetrical.

Understanding the symmetries of the 6502 opcode behaviour is a 10,000 pound research question. To pull this off you really need to understand the entire CPU inside and out before you even begin writing. The more "traditional" instruction-oriented approach lets you learn piece by piece, instead. The 6502 has only 256 possible instructions and is one of the simplest general purpose CPUs in existence. If you want to try and accomplish the same thing with anything else... not impossible, but good luck, I guess?

So... I thought it was a very interesting demonstration, and an amazing illustration of symmetries in the 6502's instruction set, but I didn't ever see it as a practical approach to writing an emulator. Though, maybe it's not too far off from what you'd need to do to recreate the CPU in an FPGA or something like that. YMMV.


Aside from the strange string matrix approach, the rest of it is actually pretty "normal" and a very clear and followable example for the other parts of the emulator. I think the video really hits its mark in terms of showing someone how to write an emulator, but I don't think much that it's showing you about the CPU is something you can learn and transfer to trying to write for another CPU. It's too idiosyncratic, and it's too much tied to having extensive, deep, well researched knowledge of the 6502 and its peculiar small architecture. Even just for learning the 6502 itself, I'm not sure how helpful it is as an approach; this looks more to me like the 6502 emulator you write for fun when you've already written ten 6502 emulators before.


Top
 Profile  
 
PostPosted: Thu Jul 25, 2019 11:00 am 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 21560
Location: NE Indiana, USA (NTSC)
For beautiful symmetry, look at anything based on Intel 8080, such as Zilog Z80 or Sharp SM83. However, in practice on 8080 family, you often end up with "this CPU is missing the one instruction I need" syndrome because it was such an early design among 8-bit single-chip processors. I don't know enough about Motorola 6800 (what the 6502 team worked on before leaving Moto and making the 6502) to know whether what the 6502 got right was a lucky accident or a clever achievement.

_________________
Pin Eight | Twitter | GitHub | Patreon


Top
 Profile  
 
PostPosted: Fri Jul 26, 2019 6:30 am 
Offline

Joined: Tue Jul 23, 2019 10:41 am
Posts: 31
rainwarrior wrote:
I really love that video, but honestly, this approach is just about optimizing for "simple" code, and probably not widely applicable in any other way than that. In emulators it's common to have shared code between instructions, but not executed by a matrix like that. At least, not to that extent.

In most cases its more practical and more efficient to write cases for individual instructions. Bisqwit pulled it off here, partly because of how simple the 6502 is specifically, partly by deliberately oversimplifying it to make the code more symmetrical.

Understanding the symmetries of the 6502 opcode behaviour is a 10,000 pound research question. To pull this off you really need to understand the entire CPU inside and out before you even begin writing. The more "traditional" instruction-oriented approach lets you learn piece by piece, instead. The 6502 has only 256 possible instructions and is one of the simplest general purpose CPUs in existence. If you want to try and accomplish the same thing with anything else... not impossible, but good luck, I guess?

So... I thought it was a very interesting demonstration, and an amazing illustration of symmetries in the 6502's instruction set, but I didn't ever see it as a practical approach to writing an emulator. Though, maybe it's not too far off from what you'd need to do to recreate the CPU in an FPGA or something like that. YMMV.


Aside from the strange string matrix approach, the rest of it is actually pretty "normal" and a very clear and followable example for the other parts of the emulator. I think the video really hits its mark in terms of showing someone how to write an emulator, but I don't think much that it's showing you about the CPU is something you can learn and transfer to trying to write for another CPU. It's too idiosyncratic, and it's too much tied to having extensive, deep, well researched knowledge of the 6502 and its peculiar small architecture. Even just for learning the 6502 itself, I'm not sure how helpful it is as an approach; this looks more to me like the 6502 emulator you write for fun when you've already written ten 6502 emulators before.

Okay thanks for your reply.
The matrix looks weird/cool. But the rest looks like reasonable code indeed, a bit compressed though.
Good to know this isn't the best approach for a cpu.

tepples wrote:
For beautiful symmetry, look at anything based on Intel 8080, such as Zilog Z80 or Sharp SM83. However, in practice on 8080 family, you often end up with "this CPU is missing the one instruction I need" syndrome because it was such an early design among 8-bit single-chip processors. I don't know enough about Motorola 6800 (what the 6502 team worked on before leaving Moto and making the 6502) to know whether what the 6502 got right was a lucky accident or a clever achievement.

What do you mean?


Top
 Profile  
 
PostPosted: Fri Jul 26, 2019 6:42 am 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 21560
Location: NE Indiana, USA (NTSC)
Look at how symmetrical the move and ALU operations in an 8080 (table) or the related Game Boy CPU (table) are.

(The other part was just a rant that 8080 lacks useful indexed or offset addressing modes, and the 6502 team may have happened on something useful by mistake.)

_________________
Pin Eight | Twitter | GitHub | Patreon


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group