News for hackers. Stuff that matters.
Re:C-based Virtual Machine for Notch's DCPU-16 by revelation on Wednesday April 04, 2012 @12:46PM (#3798408)
An enum with the opcodes would have been a lot nicer. I guess theres no time to lose in a competition with the rest of the world to come up with stuff on a one page of paper CPU.
Re:C-based Virtual Machine for Notch's DCPU-16 by a1k0n on Wednesday April 04, 2012 @01:38PM (#3798750)
And I'm pretty sure the O register isn't supposed to be set in all cases. And what was lit[] supposed to be for?
Re:C-based Virtual Machine for Notch's DCPU-16 by swetland on Thursday April 05, 2012 @12:36AM (#3801325)
Yeah, think you're right. I'm out of spec there. That's what you get for trying to be cute. Fixed now.
Re:C-based Virtual Machine for Notch's DCPU-16 by a1k0n on Thursday April 05, 2012 @12:07PM (#3803187)
I think your lit table also implies you can write to literals. So I can 'SET 2, 0x1f; SET A, 2' and now A is 0x1f.
Re:C-based Virtual Machine for Notch's DCPU-16 by swetland on Thursday April 05, 2012 @01:48PM (#3803685)
There's a check in the writeback phase that prevents that.
Re:C-based Virtual Machine for Notch's DCPU-16 by a1k0n on Thursday April 05, 2012 @06:44PM (#3805034)
Ah-ha, yes there is. Sorry.
Re:C-based Virtual Machine for Notch's DCPU-16 by jpd on Wednesday April 04, 2012 @05:39PM (#3799987)
He needs 'lit' so dcpu_opr() can uniformly return a pointer for all results, including literal values.
Re:C-based Virtual Machine for Notch's DCPU-16 by cpeterso on Wednesday April 04, 2012 @01:39PM (#3798754)
I'm sure pull requests would be graciously considered. :)
Re:C-based Virtual Machine for Notch's DCPU-16 by revelation on Wednesday April 04, 2012 @02:37PM (#3799116)
Sorry, this came off a bit rude. I just saw the Go version having the same magic numbers all over instead of using a straightforward enum for no cost, so I felt like this was worth mentioning.

On the other hand, I feel that building all kinds of stuff around notchs MMO-CPU is just an outlet for all of the excitement that has built up. I guess its upon Mojang to deliver and stop people from diverting off course.

Re:C-based Virtual Machine for Notch's DCPU-16 by eridius on Wednesday April 04, 2012 @04:18PM (#3799636)
I think you're a bit prejudiced against "magic numbers". If a number is used only once in the entire program, why bother giving it an enum? Just write a comment explaining what it is. That's what I did with the Go implementation.
Re:C-based Virtual Machine for Notch's DCPU-16 by revelation on Wednesday April 04, 2012 @06:30PM (#3800189)
I am not prejudiced, I just know the problem. Enums provide type safety, they integrate with the tools we use, the debugger knows about them, many languages allow you to get a string representation of them for pretty debug output. And best of all, they are completely free.
Re:C-based Virtual Machine for Notch's DCPU-16 by wvenable on Wednesday April 04, 2012 @05:10PM (#3799869)
Looking at this C code; it could be made more self documenting with a few constants (in enum if you want) for the values rather than just putting them in. Comments are fine but it seems like most of the values are used twice.
Re:C-based Virtual Machine for Notch's DCPU-16 by cpeterso on Wednesday April 04, 2012 @07:11PM (#3800381)
An enum is a type of comment that can be enforced by the compiler and is likely less typing than adding /* regular comments */.
Re:C-based Virtual Machine for Notch's DCPU-16 by eridius on Wednesday April 04, 2012 @08:34PM (#3800655)
It can only be enforced if it's used more than once.
Re:C-based Virtual Machine for Notch's DCPU-16 by doyoulikeworms on Wednesday April 04, 2012 @07:33PM (#3800461)
> If a number is used only once in the entire program, why bother giving it an enum?

Software changes, and is often changed by someone other than the original author.

Re:C-based Virtual Machine for Notch's DCPU-16 by dizidoro on Wednesday April 04, 2012 @06:37PM (#3800221)
I hate magic numbers, i believe comments should be made as last resort. The code should be self-documenting. It's about reading and understanding the code easier. One thing that i learned wit CS is that it's all about making higher abstractions, making the logic closer to the domain you are modeling. If there is a word that resumes CS, i would say abstraction. Thats why we have high level languages, OOP, Operating Sytems, folders and files and etc.
Re:C-based Virtual Machine for Notch's DCPU-16 by chj on Wednesday April 04, 2012 @11:17PM (#3801132)
Normally enum is better.

Here enum is not a good idea if you have a switch case like:

switch (val) { /* val is uint8_t / case OP_XXX: break; case OP_YYY: break; case OP_ZZZ: break; }

Looks nice, but in debugging, you have no idea where we are going when you set a breakpoint at the switch. because the val is not an enum type, so debugger can't make the job easier for you.

Now it is much better:

switch (val) { / val is uint8_t / case 0: / XXX / break; case 1: / YYY / break; case 2: / ZZZ */ break; }

Re:C-based Virtual Machine for Notch's DCPU-16 by Ralith on Thursday April 05, 2012 @02:06AM (#3801512)
> Looks nice, but in debugging, you have no idea where we are going when you set a breakpoint at the switch. because the val is not an enum type, so debugger can't make the job easier for you.

So .. move forward a step?

Re:C-based Virtual Machine for Notch's DCPU-16 by chj on Thursday April 05, 2012 @10:12AM (#3802596)
Things could be more complicated than that.

The val may be a corrupted value which are not covered by any of the listed case. However, you will be able to identify what's going wrong right away by comparing the value with listed cases.

Yes, it is rare. but sometimes I do wish I haven't used enum.

Re:C-based Virtual Machine for Notch's DCPU-16 by DanBC on Thursday April 05, 2012 @06:32AM (#3802013)

    putting three spaces in front of a line formats it as code and allows use of *
not putting those spaces means that text in asterisks become italics
Re:C-based Virtual Machine for Notch's DCPU-16 by swetland on Thursday April 05, 2012 @12:39AM (#3801331)
Well, I think of it more as "fan art", if you will, than a serious project. I was trying to aim for a "one page" implementation for a one page spec (given how generally clean and tidy the design is) because it was fun.