VOGONS


808x and prefetch

Topic actions

First post, by vladstamate

User metadata
Rank Oldbie
Rank
Oldbie

So I finally got around to implementing (what I think) is more correct CPU bus and prefetch behavior in my emulator. I wanted to share some interesting finds. But first this is what is emulated:

- instruction timings (not 100% accurate, but quite close)
- EA calculation timing (5 clocks for this)
- take vs not take a jump (different timings in each case)
- jumps (including calls, int, etc) clear prefetch queue
- correct bus behavior: a transfer every 4th clock, with prefetch filling up in only idle bus cycles.
- all data transfered in and out of CPU obeys the rule above

So I've tried this with both 8086 and 8088 and I found out that the prefetch queue is empty about 9% of requests for 8086 and 15% of requests for 8088. This means even for an 8088 5 out of 6 times the CPU is reading an instruction byte it finds it in the prefetch queue and only 1 out of 6 has to wait 4 more cycles. Is this in the realm of expected?

Here is a question though:

In the case of Jxx where we do not take the branch the instruction only takes 4 cycles. All good, however if we do take the branch, I clear the prefetch queue and proceed to wait 16 cycles. The penalty to the following instructions is not that high though because by the time the 16 cycles finished, the prefetch had a chance to fill 2 bytes or so now it has data again. However if I would clear the prefetch queue at the end of the 16 cycles that would hurt more since then I would have to wait 4 more cycles (for at least 1 bus transfer) before the next instruction is available.

Currently I am implementing the first way, and I am not sure which one is the more correct.

YouTube channel: https://www.youtube.com/channel/UC7HbC_nq8t1S9l7qGYL0mTA
Collection: http://www.digiloguemuseum.com/index.html
Emulator: https://sites.google.com/site/capex86/
Raytracer: https://sites.google.com/site/opaqueraytracer/

Reply 1 of 24, by Jepael

User metadata
Rank Oldbie
Rank
Oldbie

Bunch of short instructions (less than 2 clocks per byte) can drain the queue so it can become empty. So how often it is empty is up to what you execute.

As for your question, timing for control transfer instructions include the time to clear prefetch queue and fetch new instruction.

Note that EA calculation takes 5-12 cycles depending on what registers are needed and if there is a displacement or not.

Reply 2 of 24, by vladstamate

User metadata
Rank Oldbie
Rank
Oldbie

So now that what I've implemented works quite well, the next thing I am adding is memory wait states and video memory details. For example on a 386 with a 16bit ISA VGA a 32bit transfer will take 2 bus cycles, and on a 8086 with a 8bit ISA CGA it will also take 2 bus cycles. But I have a few questions:

- there are 8 bit ISA VGA cards, I assume there are also 16bit ISA CGA cards? Or are all CGA only 8 bit? I also assume Hercules and MGAs are only 8 bit right? My emulator has specific emulation for MDA, CGA, Hercules and VGA
- is there a way to find out wait states for more common graphics cards? If not, what is a typical number for a CGA or a VGA lets say? 1 or 2?
- third, general memory wait states. I have not seen a setting for this until 486 BIOSes, should I just let the user specify 0, 1 or 2 (or more)?

Most 286s (reading the Abrash's chapter) seem to have 1 wait state for general memory access, what about IBM PC?

I am also investigating the PCJr memory as I know there the bus cycles are shared with the CGA and it is even slower...

YouTube channel: https://www.youtube.com/channel/UC7HbC_nq8t1S9l7qGYL0mTA
Collection: http://www.digiloguemuseum.com/index.html
Emulator: https://sites.google.com/site/capex86/
Raytracer: https://sites.google.com/site/opaqueraytracer/

Reply 3 of 24, by reenigne

User metadata
Rank Oldbie
Rank
Oldbie

For IBM CGA, the wait state for accessing (read or write) video RAM is computed by doing this:
* wait 8 hdots (one hdot is 1/3 of a CPU cycle)
* wait for the next 16 hdot boundary
* wait for the next CPU cycle

To a first approximation, the resulting wait state is between 3 and 8 CPU cycles (the 3 cycle wait state occurs 1/16th of the time, the others each occur 3/16ths of the time). So that's an average of 93/16 = 5.8125 CPU cycles per access. CGA RAM is quite slow!

I have noticed, however, that accessing the CGA memory at certain times gives a wait state that is actually unpredictable (there's a race condition between two signals which are generated from exactly the same clock edge, and which one gets there first depends on temperature and electrical noise).

PCjr is actually much simpler and faster than CGA (though slower than PC/XT system memory). The scheme there is that the CPU gets the opportunity to do one memory access every 16 hdots.

Reply 4 of 24, by SoftPCMuseum_

User metadata
Rank Newbie
Rank
Newbie

I know that I am replying to this late when I should have replied earlier, but I really recommend that you start work on improving the Intel 80386 CPU emulation since even your own page made it quite clear that it was still heavily incomplete. Over the past several years I have noticed how many emulators claimed to be "planning support for the 80286 and higher" but never progressed beyond the 80186 level; especially given the number of so-called "collectors" who simply don't care about the IBM PC/AT and higher (let alone anything with a "PS/" or "Personal System/" in front of it), so if you want more people to actually use your emulator, then I suggest that you finish the Intel 8086/80186 as soon as possible and then start work on finishing the 80286 and higher CPUs.

Remember that this is not meant as an attack against your project, but simply to explain that the emulator should progress beyond the level of the first CPU once it becomes stable enough to reliably be used as the basis of the next CPU. From the sound of your post, it would seem as if even the Intel 8086 CPU is still very much a work in progress.

Reply 5 of 24, by Scali

User metadata
Rank l33t
Rank
l33t

I think an emulator should emulate 8088 properly first, before trying to tackle 286 and higher 😀
There are too many PC emulators out there already that emulate various CPU/machine types poorly. But no emulator that gets at least one of them right.

http://scalibq.wordpress.com/just-keeping-it- … ro-programming/

Reply 6 of 24, by Jo22

User metadata
Rank l33t++
Rank
l33t++

This is cool, but..
I wonder, why does nobody aim for cycle exact emulation of a NEC V20 ?
I thought a lot of XTs where equipped with that one because the 8088 was somewhat slow it made people cry. 😉
Anyway, I don't know much about this. I'm just curious, did games or applications of the 80s ever fail on a 808x clone ?

"Time, it seems, doesn't flow. For some it's fast, for some it's slow.
In what to one race is no time at all, another race can rise and fall..." - The Minstrel

//My video channel//

Reply 7 of 24, by reenigne

User metadata
Rank Oldbie
Rank
Oldbie
Jo22 wrote:

This is cool, but..
I wonder, why does nobody aim for cycle exact emulation of a NEC V20 ?
I thought a lot of XTs where equipped with that one because the 8088 was somewhat slow it made people cry. 😉

The v20 was a common upgrade, but still only a minority of machines had them. It was about a 20% speed boost (depending on the application), so not really enough to make the tears subside.

Jo22 wrote:

Anyway, I don't know much about this. I'm just curious, did games or applications of the 80s ever fail on a 808x clone ?

Well, there were clones and then there were clones. It was usually the support hardware rather than the CPU itself that caused incompatibilities though. As far as I know there is only one program that requires an Intel 8088 to work correctly - 8088MPH.

Reply 8 of 24, by SoftPCMuseum_

User metadata
Rank Newbie
Rank
Newbie
Scali wrote:

I think an emulator should emulate 8088 properly first, before trying to tackle 286 and higher 😀
There are too many PC emulators out there already that emulate various CPU/machine types poorly. But no emulator that gets at least one of them right.

The thing is, that the Intel 8088 has already been done to death, presumably because it is among the earliest chips and also the lowest for the IBM PC product line, and also because people appear to be interested in the original IBM PC most of all. And while most emulators are still far from perfect, there are a few that actually faithfully emulate the Intel 8088 as it originally existed (PCE, The PCjs Project, and even my own emulator which is based directly off of PCE are examples of that). This includes the prefetch queue and other features as such.

The problem here is that a lot of emulator authors seem not to be interested in emulating anything beyond the original IBM PC models. They always say that they're "planning support for the 80286 and higher" but they all gave up. There are quite a number of emulators at the level of the original IBM PC, but only one emulator which fully emulates an IBM PC/AT as it originally existed (The PCjs Project) and only one emulator which fully emulates any of the IBM PS/2 series, and even then just for the one 286-based IBM PS/1 model (IBMulator). PCem's emulation of the IBM machines leaves much to be desired if you ask me, and in MESS, nearly all of their IBM machines (later than the original IBM PC) are now completely broken, since most of their developers don't even seem to care about them in the slightest.

So while I fully appreciate the work that you've done regarding the earlier and lower-end Intel CPUs, I would really suggest finishing the 80286 and higher CPUs as soon as possible, and also add in support for at least one of the machines in the IBM PS/2 series if you want the emulator to gain significant usage share. Just my suggestions of course. But it's your choice.

Reply 9 of 24, by Scali

User metadata
Rank l33t
Rank
l33t

I think getting all the hardware right is more important than implementing as many CPUs as possible, while having broken/incomplete hardware emulation. A PC(/AT/PS/2/whatever) is more than just a CPU.

http://scalibq.wordpress.com/just-keeping-it- … ro-programming/

Reply 10 of 24, by reenigne

User metadata
Rank Oldbie
Rank
Oldbie
SoftPCMuseum_ wrote:

there are a few that actually faithfully emulate the Intel 8088 as it originally existed (PCE, The PCjs Project, and even my own emulator which is based directly off of PCE are examples of that). This includes the prefetch queue and other features as such.

Are there any that emulate an Intel 8088 with cycle-exact timing yet? I haven't seen any (and, in particular, there is still no emulator that runs 8088MPH correctly). Such an emulator would be really useful for making more 8088+CGA demos.

Accurate emulations of later PCs would be interesting too but not nearly so useful from a point of view of developing software for these machines - DOSBox, PCem et al do a decent enough job in those cases. As these machines have CPU clocks that run independently of the video card's pixel clock (and that of the PIT) you can't do 8088MPH-style cycle-counting tricks on these machines anyway (and such tricks are much less necessary when interrupt overhead is lower). Also, accurately emulating later machines is much more difficult than emulating earlier ones (the chips get more complex and the documentation less thorough) so it's not particularly surprising that emulator authors lose interest.

What could we do with a more accurate 286/386/EGA/VGA emulator that we can't do today?

Reply 11 of 24, by Arbee

User metadata
Rank Newbie
Rank
Newbie
SoftPCMuseum_ wrote:

in MESS, nearly all of their IBM machines (later than the original IBM PC) are now completely broken, since most of their developers don't even seem to care about them in the slightest.

On the contrary, MAME's ("MESS" was merged in over a year ago and that name is no longer used) at486 and at586 happily run DOS, Windows 1/2/3/95/98/ME, NT 3.1/3.5/4.0/2000, OS/2 1, 2, 3, and Warp, Linux, FreeBSD, XENIX, and a lot of other stuff. Plus we support multiple video cards, including accelerated Windows and X11, multiple sound cards, the MPU-401 (by running the real 6801 code!), multiple keyboards (by running the real 8051 or similar code), ethernet, and so on. Carl, our x86 guy, has put in the effort, which is why we've several times been the only emulator to run various obscure x86 OSes. We also fully support a bunch of x86-based "MS-DOS compatibles" ( machines that run a ported version of DOS but aren't PC-compatible) like the PC-98.

But if you have specific grievances or bug reports, we'd love to hear them.

Reply 12 of 24, by Scali

User metadata
Rank l33t
Rank
l33t
reenigne wrote:

Accurate emulations of later PCs would be interesting too but not nearly so useful from a point of view of developing software for these machines - DOSBox, PCem et al do a decent enough job in those cases. As these machines have CPU clocks that run independently of the video card's pixel clock (and that of the PIT) you can't do 8088MPH-style cycle-counting tricks on these machines anyway (and such tricks are much less necessary when interrupt overhead is lower). Also, accurately emulating later machines is much more difficult than emulating earlier ones (the chips get more complex and the documentation less thorough) so it's not particularly surprising that emulator authors lose interest.

Not to mention that there is a lot more variation in the later types. A significant amount of 8088-based PCs were IBMs, and the ones that weren't, were quite faithful copies.
But in the 286 era and beyond, more and more people were using clones, and these started using integrated chipsets with extra features, and EGA/VGA cards with better performance than the original IBM. Aside from the performance varying a lot between systems that had the same specs on paper (eg '286 8 MHz with VGA and 2 MB memory'), you also had the problem that these cloned/enhanced chipsets had their own quirks, and weren't fully compatible down to the register level, let alone on timing.

This creates the rather strange situation where a PC emulator for 286+ doesn't have to be very accurate in order to work with most software. Most software was already written around the problems of inconsistent timing, and poor support of the more obscure functionality of PC chipsets (such as the auto-EOI bugs in the integrated 8259s that I uncovered in the Headland chipset of my 286-20).

http://scalibq.wordpress.com/just-keeping-it- … ro-programming/

Reply 13 of 24, by SoftPCMuseum_

User metadata
Rank Newbie
Rank
Newbie
Arbee wrote:
SoftPCMuseum_ wrote:

in MESS, nearly all of their IBM machines (later than the original IBM PC) are now completely broken, since most of their developers don't even seem to care about them in the slightest.

On the contrary, MAME's ("MESS" was merged in over a year ago and that name is no longer used) at486 and at586 happily run DOS, Windows 1/2/3/95/98/ME, NT 3.1/3.5/4.0/2000, OS/2 1, 2, 3, and Warp, Linux, FreeBSD, XENIX, and a lot of other stuff. Plus we support multiple video cards, including accelerated Windows and X11, multiple sound cards, the MPU-401 (by running the real 6801 code!), multiple keyboards (by running the real 8051 or similar code), ethernet, and so on. Carl, our x86 guy, has put in the effort, which is why we've several times been the only emulator to run various obscure x86 OSes. We also fully support a bunch of x86-based "MS-DOS compatibles" ( machines that run a ported version of DOS but aren't PC-compatible) like the PC-98.

But if you have specific grievances or bug reports, we'd love to hear them.

Well yes, of course it supports generic hardware, but when was the last time that it supported any of the IBM PS/2 series, for example? The last time I checked, it didn't even run any of those machines at all. And this is not some sort of an obscure product or piece of hardware that almost no one used either; these are major machines from companies such as IBM.

I have also seen requests for more hardware on the Bannister website, only for those requesting it to essentially be told to shut up about it if it happens to be for a machine or product that other users don't care less about. That's partly why I'm continuing on with my own emulator, so that I can add in support for all of the machines (including the IBM PS/2 series) that other emulator developers prefer to ignore completely.

I am not saying that YOU ignore those machines, of course, and I think that you can understand where I'm coming from about the IBM machines and so on. However, the truth is that MAME (as it is now called following the merger as you yourself pointed out above) simply does not have adequate emulation for most of the IBM machines. This is already going off-topic as it is since the topic was not about MAME at all but about emulating the prefetch queue of the Intel 8086/8088 CPU; I'm just pointing out that if a project wants to succeed, then it also needs to show some progress.

Reply 14 of 24, by Jo22

User metadata
Rank l33t++
Rank
l33t++

Didn't know later PCs frome the PC/AT or PS/2 line were that problematic regarding compatiblity.
I thought it was the reverse. I read that in early to mid 80s some PC clones were advertised
as "MS-DOS compatible", but not "PC compatible". I know of a few such models, at least on paper..
Olivetti M24, Sanyo MBC 550, Alphatronic P-30 / P-40, CM1910 and most notably the Sirius 1 (Victor 9000).
The Sirius looks like it was years ahead of its competiton. It was said to have 2.4MB floppies and an 800x400 mode (in 82).
I also thought this was the reason those special OEM versions of DOS were made in the first place.
But again, this was before my time. I've never seen these machines in persona, so I can't judge.

@SoftPCMuseum_ ,@Arbee
For years I missed a faithful emulation of the IBM/PC AT, Model 5170.
It wasn't until I've found ScriptPC and PCem that I was finally able to see such a machine running at all.
If we think about the historical relevance, this is just sad, I think.
People are so fixed about the "first PC" (which by the way, wasn't the "first PC" per se. The term Personal Computer was already used in the 70s by other manufacturers such as Sharp with the MZ line and for various no-name computers).
Whenever you see a television show about computer history, they mention the Apple I,II and the IBM PC as the beginning of everything. Then they skip the Compaq/386 and the AT rightaway.
Often they mention no tube computer, no relays computers, no IMSAI 8080, no Zuse Z3 or Z22. Just IBM and Apple.
Man, the AT brought so many stuff we now take for granted. Like a protected-mode environment, a realtime clock, the AT-style keyboard which can now be found on every desktop in the world, a software configurable firmware, AT-Bus hard drives (derived from ISA,16Bit), a standard architecture (ISA).
I'm not angry whatsoever, just a bit sad. If we can't get work in emulation started soon, both the chance and those machines will be gone.
And yes, I do appreciate the Apple II and IBM PC 5150 for their open design and love those emulation projects.
It just makes me sad that other early and interesting computers, like the Sirius 1, will be lost and forgotten and kids in the future have no chance to experience them.
Sure, we can't keep everything, but take into consideration how much they're already interested in stuff of their older brothers, sisters and parents! On youtube for example, you see them at young age enjoying their NES and A2600 games as we did.

"Time, it seems, doesn't flow. For some it's fast, for some it's slow.
In what to one race is no time at all, another race can rise and fall..." - The Minstrel

//My video channel//

Reply 15 of 24, by Alegend45

User metadata
Rank Newbie
Rank
Newbie
Arbee wrote:
SoftPCMuseum_ wrote:

in MESS, nearly all of their IBM machines (later than the original IBM PC) are now completely broken, since most of their developers don't even seem to care about them in the slightest.

On the contrary, MAME's ("MESS" was merged in over a year ago and that name is no longer used) at486 and at586 happily run DOS, Windows 1/2/3/95/98/ME, NT 3.1/3.5/4.0/2000, OS/2 1, 2, 3, and Warp, Linux, FreeBSD, XENIX, and a lot of other stuff. Plus we support multiple video cards, including accelerated Windows and X11, multiple sound cards, the MPU-401 (by running the real 6801 code!), multiple keyboards (by running the real 8051 or similar code), ethernet, and so on. Carl, our x86 guy, has put in the effort, which is why we've several times been the only emulator to run various obscure x86 OSes. We also fully support a bunch of x86-based "MS-DOS compatibles" ( machines that run a ported version of DOS but aren't PC-compatible) like the PC-98.

But if you have specific grievances or bug reports, we'd love to hear them.

I'd love to hear what obscure OSes you claim only MAME can run. I can test them in PCem, and see if you're right!

Reply 16 of 24, by Battler

User metadata
Rank Member
Rank
Member
Arbee wrote:

On the contrary, MAME's ("MESS" was merged in over a year ago and that name is no longer used) at486 and at586 happily run DOS, Windows 1/2/3/95/98/ME, NT 3.1/3.5/4.0/2000, OS/2 1, 2, 3, and Warp, Linux, FreeBSD, XENIX, and a lot of other stuff.

A lot of which also runs on PCem. Of course, there's still some hiccups but what do you expect from a project where the main developer insists on developing the emulator with a "fix/implement something if a relevant enough piece of software is found that uses it" rather than with a "do everything by the specification" approach (which yould IMHO yield much cleaner code and less bugs), and the person who does the second most work, ie. myself, having only been doing any work on it for about 2 years now, and having other interests outside emulation as well hence why I don't do PCem work all the time.

Plus we support multiple video cards, including accelerated Windows and X11, multiple sound cards,

So does PCem.

the MPU-401 (by running the real 6801 code!),

Well, you do one up us on that one. :p

multiple keyboards (by running the real 8051 or similar code),

PCem currently only high-level emulates the 8042 (and whatever chip the PC and XT used), and even that needs improvement, something I am still working on.
Also, I'm not sure what MAME is using for getting keyboard input from the host, but it's probably either some FOSS library such as SDL which ultimately ends in either DirectInput or Windows virtual key codes, or direct use of any of Microsoft's high-level API's, and that's guaranteed to screw up if using a keyboard layout other than US English on the host, as well as not being able to pass obscure keys correctly from the host. I am even using custom key remappings and aside from virtualizers such as VMWare and Virtual PC 2007, the only thing that respects them is PCem and that's because I personally improved its keyboard input obtaining from the host.

ethernet, and so on.

Ethernet is being worked on, both ISA (NE2000 ISA) and PCI (Realtek RTL-8029AS a.k.a. NE2000 PCI), by me personally as well as neozeed.

Carl, our x86 guy, has put in the effort, which is why we've several times been the only emulator to run various obscure x86 OSes.

I am personally contributing fixes to PCem whenever I stumble upon one of those cases. An example is when I added LOADALL386 to make those OS/2 prototypes work on the emulated 386.

We also fully support a bunch of x86-based "MS-DOS compatibles" ( machines that run a ported version of DOS but aren't PC-compatible) like the PC-98.

[/quote][/quote]
I do have in my plains to eventually create a fork of PCem that would emulate PC-98-compatibles instead of IBM-compatibles, but the original NEC documentation has only recently appeared on archive.org, and I am still in the process of learning Japanese. However, one day, I will be able to. :p
In the mean time, my friend SA1988 who has also contributed an amount of code to PCem, is, alongside roytam1 from BetaArchive (where I'm a Moderator), contributing to NP21W which is currently the best PC-98 emulator in existence. I am thinking of starting contributing to that myself as well.

Reply 17 of 24, by Scali

User metadata
Rank l33t
Rank
l33t
Battler wrote:

Plus we support multiple video cards, including accelerated Windows and X11, multiple sound cards,

So does PCem.

I think he means multiple video/sound cards at the same time, which PCem could not, last time I looked.
For some reason, most PC emulators don't do this. While it wasn't too uncommon to have eg both MDA and CGA in a single PC (my 5160 has that as well). IBM specifically designed their hardware for this.
Likewise, I ran my main gaming/demoscene system with both a Sound Blaster Pro and a GUS MAX at the same time for years, and I think many people did.
In both cases, it's basically when you upgrade your system, but don't remove the old hardware, because they can complement eachother.

http://scalibq.wordpress.com/just-keeping-it- … ro-programming/

Reply 18 of 24, by Arbee

User metadata
Rank Newbie
Rank
Newbie

Right, at the same time. We used MDA + (S)VGA + SoftICE in the game industry until Intel finally changed the chipsets to make it impossible (circa 2000, I think). Similarly, MAME's Mac driver supports multiple video cards (or multiple copies of the same one) since MacOS supported multiple heads as early as 1987.

Battler: I'm not here for a dick-waving contest, I actually think PCem is pretty cool. I just was summoned because SoftPCMuseum was talking shit 😀

Jo22: Our ibm5170 driver works fine to my knowledge and is historically accurate in the usual MAME manner (actually running the 8042, etc, etc). If you have problems with it we'd like to know (e.g. MAMETesters.org, or talk to the devs at forums.bannister.org). I'm currently reverse-engineering the "PC Transporter" card which let an Apple II run PC-XT software on a 7.14 MHz V30 (with the 6502 emulating some of the peripherals in the background, like the HDD) so I'm well aware how much of today's PC standard actually came from the AT 😀

Reply 19 of 24, by Scali

User metadata
Rank l33t
Rank
l33t
Arbee wrote:

Right, at the same time. We used MDA + (S)VGA + SoftICE in the game industry until Intel finally changed the chipsets to make it impossible (circa 2000, I think). Similarly, MAME's Mac driver supports multiple video cards (or multiple copies of the same one) since MacOS supported multiple heads as early as 1987.

Yup, same here, I've used my old IBM MDA card for years with an old 12" paperwhite monitor as a SoftICE screen, so I could easily debug my fullscreen graphics, until I no longer had a PC with an ISA slot.
If you have such a setup, run the demo Stars: Wonders Of The World by NoooN: http://www.pouet.net/prod.php?which=301
It has a lovely easter-egg 😀

http://scalibq.wordpress.com/just-keeping-it- … ro-programming/