www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - Z80 Emulation Engine

reply "ketmar" <ketmar ketmar.no-ip.org> writes:
quick-and-dirty port of my Zymosis Z80 emulation engine to D. 
code was built from scratch and not using huge tables to generate 
huge sources (it's just one module with source size ~64KB).

it properly emulates all known Z80 quirks (including MEMPTR 
register) and passes all 1335 tests from FUSE.

sorry, it uses GDC  attribute("forceinline") feature, so you need 
latest GDC to build it. it's not strictly necessary though (speed 
optimizations? who needs that speed optimizations with current 
CPUs?!) and can be hacked away with this piece of code:

version(GNU) {
   import gcc.attribute;
} else {
   private struct Attribute(A...) { A args; }
   auto attribute(A...) (A args) if (A.length > 0 && is(A[0] == 
string)) { return Attribute!A(args); }
}

i'm pretty sure that this is the first Z80 emulator written in D. 
%-)

ah, nearly forgot to give repo URL: 
http://repo.or.cz/w/zymosis.d.git
Apr 20 2014
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
ketmar:

 sorry, it uses GDC  attribute("forceinline") feature, so you 
 need latest GDC to build it. it's not strictly necessary though 
 (speed optimizations?
Have you performed a benchmark with and without that attribute?
 http://repo.or.cz/w/zymosis.d.git
In this kind of code computed gotos could help performance.
 struct { ubyte c, b; };
Struct definitions in D don't end with the semicolon.
  property final void iff1 (int v) nothrow { riff1 = (v != 0); } 
 /** set interrupt flip-flop 1 */
I suggest to omit the space between the function name and its arguments. And if you want you can also add an "in": ... iff1(in int v) nothrow ...
 { rregR = ((rregR&0x7f)+1)|(rregR&0x80); }
Better to add spaces around those operators. And perhaps it's better to use enum values instead of magic constants.
   final void exec () {
     ubyte opcode;
     bool gotDD, trueCC;
     int disp;
     ubyte tmpB, tmpC, rsrc, rdst;
     ushort tmpW = 0; /* shut up the compiler; it's wrong but 
 stubborn */
     /* main loop */
     while (rtstates < rnext_event_tstate) {
       if (rCallPager) pager();
       if (rCheckBPs && checkBP()) return;
       rprevpc = rorgpc;
       rorgpc = rpc;
       /* read opcode -- OCR(4) */
       opcode = fetchOpcode();
       rprev_was_EIDDR = 0;
       disp = gotDD = false;
       rdd = &rhl;
       if (rhalted) { --rpc; continue; }
       /* check for I[XY] prefix */
       if (opcode == 0xdd || opcode == 0xfd) {
I suggest to add an empty line before the line of comment. And I suggest to use 4 spaces as indent unit.
 static __gshared ubyte parity_tbl[256];
Better to use the D syntax: static __gshared ubyte[256] parity_tbl; Bye, bearophile
Apr 20 2014
parent "ketmar" <ketmar ketmar.no-ip.org> writes:
On Sunday, 20 April 2014 at 13:08:02 UTC, bearophile wrote:
 sorry, it uses GDC  attribute("forceinline") feature, so you
Have you performed a benchmark with and without that attribute?
not on this code yet. will check it someday,
 http://repo.or.cz/w/zymosis.d.git
In this kind of code computed gotos could help performance.
i don't think so: it's not a 'table-driven executor', it does proper instruction decoding by various opcode bitfields like Z80 do. this is "by design". so i can't just make a huge table for computed goto.
 struct { ubyte c, b; };
Struct definitions in D don't end with the semicolon.
ah. tnx, this is a C code leftovers. maybe compiler should emit warnings on superfluous semicolons?
 I suggest to omit the space between the function name and its 
 arguments.
sorry, but i will not change my coding style. i may use original author's style when sending patches, but i'm used to my own for my projects.
 And if you want you can also add an "in":
tnx, will do.
 And perhaps it's better to use enum values instead of magic 
 constants.
this is hardware magic by itself. %-) i don't think that constants like 'BIT7MASK' and 'BITS0TO6MASK' will look prettier here.
 static __gshared ubyte parity_tbl[256];
Better to use the D syntax: static __gshared ubyte[256] parity_tbl;
ah, C leftovers again, changed, tnx. thanks for all your comments and suggestions. i'm still D newbie, so they are valuable input.
Apr 20 2014
prev sibling next sibling parent Manu via Digitalmars-d-announce <digitalmars-d-announce puremagic.com> writes:
On 20 April 2014 21:16, ketmar via Digitalmars-d-announce <
digitalmars-d-announce puremagic.com> wrote:

 i'm pretty sure that this is the first Z80 emulator written in D. %-)
I suspect mine might have been the first Z80 emulator written in D, but even then, probably not :) https://github.com/TurkeyMan/superemu
Apr 20 2014
prev sibling parent reply Manu via Digitalmars-d-announce <digitalmars-d-announce puremagic.com> writes:
On 21 April 2014 00:49, Manu <turkeyman gmail.com> wrote:

 On 20 April 2014 21:16, ketmar via Digitalmars-d-announce <
 digitalmars-d-announce puremagic.com> wrote:

 i'm pretty sure that this is the first Z80 emulator written in D. %-)
I suspect mine might have been the first Z80 emulator written in D, but even then, probably not :) https://github.com/TurkeyMan/superemu
Funny you make a point of forceinline, I have forceinline placeholders all over my emulators too ;) It'll come... one day.
Apr 20 2014
parent reply "ketmar" <ketmar ketmar.no-ip.org> writes:
On Sunday, 20 April 2014 at 15:17:56 UTC, Manu via 
Digitalmars-d-announce wrote:
 https://github.com/TurkeyMan/superemu
wow, my google-fu is bad than. %-) doing 'git clone' right now. btw, what is the license for your code?
 Funny you make a point of forceinline, I have forceinline 
 placeholders all
 over my emulators too ;)
i think that almost any emulator writer just can't resist the urge to 'help' compiler this way. %-)
Apr 20 2014
parent reply Manu via Digitalmars-d-announce <digitalmars-d-announce puremagic.com> writes:
On 21 April 2014 04:03, ketmar via Digitalmars-d-announce <
digitalmars-d-announce puremagic.com> wrote:

 On Sunday, 20 April 2014 at 15:17:56 UTC, Manu via Digitalmars-d-announce
 wrote:

 https://github.com/TurkeyMan/superemu

 wow, my google-fu is bad than. %-) doing 'git clone' right now.
btw, what is the license for your code?
I don't really care. Refer to it as much as you like. I have larger plans for this project, but I've been distracted with other apparently more important things. I've been meaning to get back to it. It's a bit out of date now. I need to update it. It might be fun to collaborate if you're interested...? Funny you make a point of forceinline, I have forceinline placeholders all
 over my emulators too ;)
i think that almost any emulator writer just can't resist the urge to 'help' compiler this way. %-)
Indeed.
Apr 21 2014
parent reply "ketmar" <ketmar ketmar.no-ip.org> writes:
 btw, what is the license for your code?
I don't really care. Refer to it as much as you like.
so maybe you will add license to sources? WTFPL, for example, which basically means "public domain". the thing is that sources without license are proprietary, and nobody except the author can do anything with that.
 I have larger plans for this project, but I've been distracted 
 with other
 apparently more important things.
same thing here. %-)
 It might be fun to collaborate if you're interested...?
i'm actually know only one vintage computer: ZX Spectrum. and sadly i have not much time now. but this surely will be fun, so i hope that i will be able at least help a little here and there. %-) p.s. zymosis was written a long time ago in C, when i realized that there is NO Z80 emulator which is: 1. accurate. 2. does not need alot of tables to generate 400kb of shitty source code. 3. written by me. %-) zymosis.d was just a quick port from C which i did in two or three evenings. one of the goals wal to see how easy it will be to port such a messy code, 'cause i'm planning to use D in my future projects and eventually port all my homebrew C libraries to D. and when zymosis.d compiled first time it immediately passes almost all tests. very impressive. then i decided to announce the thing here — just in case if anybody will need such thing. there is no much sense to let it rotting on my hdd.
Apr 21 2014
next sibling parent reply Manu via Digitalmars-d-announce <digitalmars-d-announce puremagic.com> writes:
On 22 April 2014 00:45, ketmar via Digitalmars-d-announce
<digitalmars-d-announce puremagic.com> wrote:
 btw, what is the license for your code?
I don't really care. Refer to it as much as you like.
so maybe you will add license to sources? WTFPL, for example, which basically means "public domain". the thing is that sources without license are proprietary, and nobody except the author can do anything with that.
Yeah I know, I just never expected anyone else to take interest. I'm often torn between gpl and bsd/zlib. If something's open source with no commercial intent, is there good reason not to use gpl? How hard is it to change later?
 It might be fun to collaborate if you're interested...?
i'm actually know only one vintage computer: ZX Spectrum. and sadly i have not much time now. but this surely will be fun, so i hope that i will be able at least help a little here and there. %-)
I've written a spectrum emulator. Good fun. It could play games from cassette tape attached to the audio-in port ;)
 p.s. zymosis was written a long time ago in C, when i realized that there is
 NO Z80 emulator which is:
 1. accurate.
 2. does not need alot of tables to generate 400kb of shitty source code.
 3. written by me. %-)
Yeah, I understand point 3. It's a good exercise to write an emulator, so a lot of people reinvent that wheel for the experience alone :)
Apr 21 2014
next sibling parent "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
On Tuesday, 22 April 2014 at 01:17:46 UTC, Manu via 
Digitalmars-d-announce wrote:
 If something's open source with no commercial intent, is there 
 good
 reason not to use gpl?
Nothing in GPL prevents commercial use, and it doesn't limit your ability to issue other licenses later. It does not limit the author, only the user. But you have to make sure that all patches you receive are followed by a written statements where the ownership is transferred to you. The patches makes it a derived work, and then you need all the authors of that derived work to agree on an additional license.
 How hard is it to change later?
You cannot revoke GPL for released code, but you can stop releasing new versions under GPL.
Apr 22 2014
prev sibling parent reply "ketmar" <ketmar ketmar.no-ip.org> writes:
 If something's open source with no commercial intent, is there 
 good
 reason not to use gpl? How hard is it to change later?
i don't see a reason not to use GPL even on commercial code. %-)
Apr 22 2014
next sibling parent Manu via Digitalmars-d-announce <digitalmars-d-announce puremagic.com> writes:
On 23 April 2014 00:33, ketmar via Digitalmars-d-announce
<digitalmars-d-announce puremagic.com> wrote:
 If something's open source with no commercial intent, is there good
 reason not to use gpl? How hard is it to change later?
i don't see a reason not to use GPL even on commercial code. %-)
I was convinced that GPL doesn't serve the purpose intended. I went with BSD, there's a license.txt now.
Apr 22 2014
prev sibling parent Rory McGuire via Digitalmars-d-announce writes:
Its because its viral that a lot of people avoid it.


On Tue, Apr 22, 2014 at 4:33 PM, ketmar via Digitalmars-d-announce <
digitalmars-d-announce puremagic.com> wrote:

 If something's open source with no commercial intent, is there good
 reason not to use gpl? How hard is it to change later?
i don't see a reason not to use GPL even on commercial code. %-)
Apr 23 2014
prev sibling next sibling parent Ben Boeckel via Digitalmars-d-announce writes:
On Tue, Apr 22, 2014 at 11:17:32 +1000, Manu via Digitalmars-d-announce wrote:
 Yeah I know, I just never expected anyone else to take interest.
 I'm often torn between gpl and bsd/zlib.
FYI, if you're using the free services on GitHub, it *must* be FOSS. I think the GitHub terms of service permit forking for public repositories regardless of the license[1].
 If something's open source with no commercial intent, is there good
 reason not to use gpl?
http://choosealicense.com/
 How hard is it to change later?
If you're the only author, you can change at will. If you accept contributions, you'll need everyone to agree (or rip their code out). You could go with a contributor agreement, but I'd advise against it. Personally, I refuse to contribute to projects which require handing copyright of my pathces over. --Ben [1]https://help.github.com/articles/github-terms-of-service §F.1
Apr 21 2014
prev sibling parent reply Manu via Digitalmars-d-announce <digitalmars-d-announce puremagic.com> writes:
On 22 April 2014 12:24, Ben Boeckel via Digitalmars-d-announce
<digitalmars-d-announce puremagic.com> wrote:
 On Tue, Apr 22, 2014 at 11:17:32 +1000, Manu via Digitalmars-d-announce wrote:
 Yeah I know, I just never expected anyone else to take interest.
 I'm often torn between gpl and bsd/zlib.
FYI, if you're using the free services on GitHub, it *must* be FOSS. I think the GitHub terms of service permit forking for public repositories regardless of the license[1].
 If something's open source with no commercial intent, is there good
 reason not to use gpl?
http://choosealicense.com/
Yeah, I understand the license options essentially, but it's more than just the license text, there are license cultures that affect the decision, and people are borderline religious about this sort of thing. I mean, the GPL seems fine to me, but there are many people who see GPL and avoid it like the plague as a matter of superstition or something. I'd prefer to not discourage interest or contribution just because I wrote "GPL" near my code. Then people invented LGPL and in my experience, this makes some of them feel okay with it, and others still don't wanna go near it. What practical reasons are there to avoid GPL if your software is fundamentally open-source? Ideally, I'd like something like GPL, with the option that I can grant someone an exception to the license upon request.
Apr 21 2014
parent reply Jacob Carlborg <doob me.com> writes:
On 22/04/14 07:57, Manu via Digitalmars-d-announce wrote:

 Yeah, I understand the license options essentially, but it's more than
 just the license text, there are license cultures that affect the
 decision, and people are borderline religious about this sort of
 thing.
 I mean, the GPL seems fine to me, but there are many people who see
 GPL and avoid it like the plague as a matter of superstition or
 something. I'd prefer to not discourage interest or contribution just
 because I wrote "GPL" near my code.
 Then people invented LGPL and in my experience, this makes some of
 them feel okay with it, and others still don't wanna go near it.

 What practical reasons are there to avoid GPL if your software is
 fundamentally open-source?
 Ideally, I'd like something like GPL, with the option that I can grant
 someone an exception to the license upon request.
If you want to use some library that is not GPL, or incompatible with GPL. Or the opposite. If someone wants to use your code, but not want to use GPL, but still an open source license. BSD, for example, is much more flexible in these cases. -- /Jacob Carlborg
Apr 21 2014
parent reply Manu via Digitalmars-d-announce <digitalmars-d-announce puremagic.com> writes:
On 22 April 2014 16:29, Jacob Carlborg via Digitalmars-d-announce
<digitalmars-d-announce puremagic.com> wrote:
 On 22/04/14 07:57, Manu via Digitalmars-d-announce wrote:

 Yeah, I understand the license options essentially, but it's more than
 just the license text, there are license cultures that affect the
 decision, and people are borderline religious about this sort of
 thing.
 I mean, the GPL seems fine to me, but there are many people who see
 GPL and avoid it like the plague as a matter of superstition or
 something. I'd prefer to not discourage interest or contribution just
 because I wrote "GPL" near my code.
 Then people invented LGPL and in my experience, this makes some of
 them feel okay with it, and others still don't wanna go near it.

 What practical reasons are there to avoid GPL if your software is
 fundamentally open-source?
 Ideally, I'd like something like GPL, with the option that I can grant
 someone an exception to the license upon request.
If you want to use some library that is not GPL, or incompatible with GPL. Or the opposite. If someone wants to use your code, but not want to use GPL, but still an open source license. BSD, for example, is much more flexible in these cases.
But then you lose the incentive to return contribution back to the original community. I've worked in companies where we take OSS libraries, modified for our needs, and never offer the modifications back to the community. I've done it myself, and it's basically wrong. I am not aware of the license that encourages community contribution, but also doesn't infect your code like the plague?
Apr 21 2014
next sibling parent "Joakim" <dlang joakim.airpost.net> writes:
On Tuesday, 22 April 2014 at 06:41:58 UTC, Manu via 
Digitalmars-d-announce wrote:
 On 22 April 2014 16:29, Jacob Carlborg via 
 Digitalmars-d-announce
 <digitalmars-d-announce puremagic.com> wrote:
 On 22/04/14 07:57, Manu via Digitalmars-d-announce wrote:

 Yeah, I understand the license options essentially, but it's 
 more than
 just the license text, there are license cultures that affect 
 the
 decision, and people are borderline religious about this sort 
 of
 thing.
 I mean, the GPL seems fine to me, but there are many people 
 who see
 GPL and avoid it like the plague as a matter of superstition 
 or
 something. I'd prefer to not discourage interest or 
 contribution just
 because I wrote "GPL" near my code.
 Then people invented LGPL and in my experience, this makes 
 some of
 them feel okay with it, and others still don't wanna go near 
 it.

 What practical reasons are there to avoid GPL if your 
 software is
 fundamentally open-source?
 Ideally, I'd like something like GPL, with the option that I 
 can grant
 someone an exception to the license upon request.
If you want to use some library that is not GPL, or incompatible with GPL. Or the opposite. If someone wants to use your code, but not want to use GPL, but still an open source license. BSD, for example, is much more flexible in these cases.
But then you lose the incentive to return contribution back to the original community. I've worked in companies where we take OSS libraries, modified for our needs, and never offer the modifications back to the community. I've done it myself, and it's basically wrong. I am not aware of the license that encourages community contribution, but also doesn't infect your code like the plague?
That would be the CDDL, which Sun came up with for OpenSolaris, and other file-based licenses like the MPL, which Mozilla came up with for the open-sourcing of Netscape: https://glassfish.java.net/public/CDDLv1.0.html The CDDL is like the GPL, in that CDD-licensed files have to stay open source when redistributed, but since it applies on a file-by-file basis, doesn't infect the rest of the codebase. Others can compile your CDD-licensed files with their own files that they license differently, as long as they provide the source for your CDDL files, including any modifications they've made to your files. All that said, simple licenses, like the BSD or MIT licenses, are probably best, because they work with almost everything else.
Apr 22 2014
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 22 Apr 2014 02:41:49 -0400, Manu via Digitalmars-d-announce  
<digitalmars-d-announce puremagic.com> wrote:

 On 22 April 2014 16:29, Jacob Carlborg via Digitalmars-d-announce
 <digitalmars-d-announce puremagic.com> wrote:
 On 22/04/14 07:57, Manu via Digitalmars-d-announce wrote:

 Yeah, I understand the license options essentially, but it's more than
 just the license text, there are license cultures that affect the
 decision, and people are borderline religious about this sort of
 thing.
 I mean, the GPL seems fine to me, but there are many people who see
 GPL and avoid it like the plague as a matter of superstition or
 something. I'd prefer to not discourage interest or contribution just
 because I wrote "GPL" near my code.
 Then people invented LGPL and in my experience, this makes some of
 them feel okay with it, and others still don't wanna go near it.

 What practical reasons are there to avoid GPL if your software is
 fundamentally open-source?
 Ideally, I'd like something like GPL, with the option that I can grant
 someone an exception to the license upon request.
If you want to use some library that is not GPL, or incompatible with GPL. Or the opposite. If someone wants to use your code, but not want to use GPL, but still an open source license. BSD, for example, is much more flexible in these cases.
But then you lose the incentive to return contribution back to the original community.
I think you're confusing incentive with enforcement. But enforcement of keeping sources open is not what GPL does, GPL forces you to open YOUR sources. It's the opposite of incentive, it's a disincentive. I don't know any for-pay developer that would prefer GPL over a less restrictive license.
 I've worked in companies where we take OSS libraries, modified for our
 needs, and never offer the modifications back to the community. I've
 done it myself, and it's basically wrong.
I disagree. There are cases where your changes are not relevant to the community. There are cases where the code is hacky, and you don't really want to support it (as some open source projects require), or follow the community guidelines for coding or documentation.
 I am not aware of the license that encourages community contribution,
 but also doesn't infect your code like the plague?
By definition, open source encourages community contribution. ANY open source license encourages this. As ANYONE who has used OSS for their binary-only distribution, had to modify it, and then had to maintain their changes internally as bugs were released on the community version, it does not pay off. There is no good reason to withhold changes to the OSS itself, and almost anyone would MUCH rather prefer to get their changes into the main-line and have them maintained by the community! -Steve
Apr 22 2014
prev sibling parent "Kagamin" <spam here.lot> writes:
On Tuesday, 22 April 2014 at 06:41:58 UTC, Manu via 
Digitalmars-d-announce wrote:
 But then you lose the incentive to return contribution back to 
 the
 original community.
 I've worked in companies where we take OSS libraries, modified 
 for our
 needs, and never offer the modifications back to the community. 
 I've
 done it myself, and it's basically wrong.
 I am not aware of the license that encourages community 
 contribution,
 but also doesn't infect your code like the plague?
You said, it couldn't be commercialized. GPL prevents commercialization, if the latter won't happen anyway, GPL doesn't give you anything. I did that, but sent a patch to the developer, diff -ur doesn't hurt in the least. Though the tool is only used by developers, no redistribution required, so GPL wouldn't prevent from doing private changes and still benefit from them (the tool provided a very important feature absent in a commercial analog, and I added 3 more more cool features).
Apr 22 2014