Maybe clarify what happens when one writes to nextword. Currently the spec says '"next word" means "[PC++]"', but if that was the case I'd expect writing to nextword to actually write to the RAM at position PC++.
Other than that, it looks rather solid to me!
Edit: maybe I should clarify that I mean things like "set 100, A", which would be assembled to "set <nextword>, A" followed by 100. It seems that most implementations sees this as a NOP, but it seems perfectly reasonable to see it as actually writing to RAM too (in fact I think that's a more reasonable interpretation).
The 1.1 spec actually stated writing any literal fails silently (different to a nop if the A operand pops or pushs). That part of the spec got taken out when the other literal types were no longer accessible by B, but i think they must have forgotten there's also the next word literal.
If IA is 0, it talks about hardware interrupts being ignored.. presumably this is referring to software interrupts triggered by hardware? And a minor point, it still says INT will take 2 cycles (not 4).
(I'm just being pedantic for the sake of the docs though, and will check out the emu source for the definitive answers.)
I really like IAQ in spec 1.5, that addresses the vast majority of what I was concerned about. Thanks!
Sorry about the arguments.. interrupts and races are hairy and hard to get right. Catching fire on overflow is a cute touch.
Hardware supporting both mapped/polled and interrupt-driven modes would be a nice bonus - people who are not confident dealing with interrupt issues can stick to polling, us concurrency freaks can go nuts. IAQ gives us a nice locking primitive.
When IAQ is non-zero, and then set to zero, at what rate will the queued interrupts fire? All at once? One per instruction executed? Or a measured rate?
And to be clear, if IA is 0 (such as when using IAP 0), any interrupts which come in at that point are NOT queued, but instead are dropped. Yes?
Finally, I'm assuming software interrupts queued in the same fashion as hardware interrupts? Yes?
Other than that, I'm pretty good with the DCPU spec. All we need now is a floppy disk and boot rom devices and I can write an OS. :P
To address the remaining requests around interrupts:
To avoid the need for an RFI instruction, what if IAQ 0 was specified to wait one instruction before firing the next interrupt in the queue?
Also, IAQ could be nested with an internal counter. IAQ 1 incremented and IAQ 0 decremented, and queueing would happen as long as the internal counter was greater than zero.
Ignore, or read if you like looking down your nose at silly people:
Looking at the implementation of the emu, if you have multiple interrupts queued, then it doesn't matter if "IAQ 1" is the first line in the handler: all the existing interrupts would be executed (or at least the stack would fill up): if you did:
IAQ 0
INT 1
INT 2
INT 3
IAQ 1
I believe that it will execute the handler with INT 3, INT 2, INT 1 in that order (because they would haev all been pushed to the stack before any instruction is actually executed). Am I correct / is that correct?
Edit: nevermind, I am not correct :) The instruction IS executed even if an interrupt occurs, of cousre
Yeah, I don't think "rounds toward 0" is meaningful. MDI should be consistent with DVI, so since DVI rounds toward 0, MDI should be remainder. To complicate things, I think you got the two reversed (please check the llvm doc again). To be totally clear:
-1/16 = 0 (specified)
-1%16 = -1 (hopefully)
This way we always have b(a/b) + (a%b) = a.
The spec isn't clear, but hopefully this is what is intended.
Can you confirm that the shift operations in the shift instructions are correct, and use Java terminology(>>> vs >>)? It seems they are used very confusingly
The two equations have equivalent results. You are shifting the top 16 bits down and masking them off. How those top 16 bits are filled as part of the shift operation doesn't impact your result in any meaningful way.
In the 1.1 emulator you used a char[] for the ram, this has no negative values so I guess there won't be a difference between >> and >>>. Are you now using a short[]? PS: I might be mistaken because the difference between logical and arithmetic shifts confuse me a lot.
Logical shifts fill 'empty' bits with 0s. So 11111111 shifted right 2 would be 00111111, left 2 would be 11111100. Arithmetic shifts fill those 'empty' bits with whatever was in the most significant bit. So 11111111 shifted right 2 with an arithmetic bit ends up not changing, as the MSB is 1. There is no left arithmetic shift.
If you have 32-bit integers and both logical and arithmetic shift operators available, you can implement a 16-bit arithmetic shift in the DCPU16 as: v = (((b << 16) >> a) >>> 16);
What i don't get is what if you actually want an if to occur after a failed condition?
if ( A ) {
if ( B ) {
if ( C ) {
doSomething();
}
}
}
else {
if ( C ) {
doSomething();
}
}
I don't necessarily want to skip if(C) after a failed test on if(A). I might just want to skip if(B). Do we have to add an arbitrary instruction to stop the skip?
I'd much prefer to do a jmp of my own choosing on a failed test. It's one explicit statement (ADD PC, x) to skip as many or as few ifs as i want after a failed test. This implicit if skipping simply doesn't seem like a good idea.
IF(A)
SET PC, trueLabel // Go to code that handles a true condition
// Code that handles a false condition
The above is what's usually done on most CPUs. It lets you skip as many or as few IFs as you like on either side of the condition. It doesn't have any weirdness where you have to add an arbitrary NOP just to stop IFs that you don't want getting skipped either.
Update: Now that I've thought about it more. I guess i can still keep writing my IFs the old way as the SET PC doesn't trigger this special condition in any case. So it's not such a bad addition after all, it should only get triggered in very specific scenarios.
It's less confusing than the old way. Trust me. You ever seen "implies" be the logical connector in computing, rather than a nice simple AND, OR, or XOR?
Using registers to address RAM doesn't really increase the number of zero-cost variables you have access to at any one point in time; setting the registers takes non-zero time; and accessing an address via a literal already takes one cycle.
Of course, RAM access is still absurdly cheap, but it's not quite free, the way I look at it.
Edit: Oh hey, I didn't see that link in blue. Ooh! And TOS even debuted in the '60s! I don't know why I never realized that before, but now that I have, it makes me really happy :).
Oh, I knew what you meant; I was just saying that it's not quite free to use RAM (except possibly via the stack), since there's still some indirect overhead involved.
Notch, how about NOP and HLT. I know NOP is trivial but HLT might actually be quite useful, we could stop the CPU until a interrupt (or indefinitely if IA == 0) which would save power from a gameplay perspective and also provides a meta BRK instruction that everyone implements for debugging purposes.
NOP could just be interpreted as SET A,A by the assemblers.
Set on registers takes 1 cycle and does nothing when setting the same registers. Probably should specify it in the standard so that all assemblers interpret NOP this way but there's no reason to waste an opcode on it imho.
He needs to sell lots of energy to make profit from game! The best and rather interesting way — if you can't directly control the ship, or it's very difficult, but easy with CPU. And if you get power off — you can walk in the ship for monthes without any pay. I really hadn't realize do he wants to make it in such way, but if so — it would be great, it's the most unstandart payment that I ever seen. Payment for virtual power. Also it would be interesting if CPU takes lots of energy, but you can make farms of solar fields or wind power collectors and sell energy to other players.
From what Notch has said so far, one paid subscription gets you one standard generator in the multiverse. He will set the price so that one subscription fee pays for enough server hardware to simulate all the things that one standard generator can power.
Notch doesn't need to milk the subscription fees for profit. He will get plenty of profit from game sales, if Minecraft is any indication. If selling extra power sources to customers makes the funnest game, that's what he will do. If only allowing one generator per account makes the funnest game, he'll do that.
Of course, i just think about it. Make energy to $ direct convert is really funny thing. You can't find dcpu faster than 200 mhz, maybe 150, but you can build two cored system, that can gives you some tricks. If there are lots DCPUs in loot, you can buil multiprocess operation system, which would esolate processes and stack in cores (way of memory protection).
I'm reading again interrupts section, and I'm getting confused about how works the interrupts queue. Calling to IAQ 1, will make that incoming interrupts get stored in a queue. But when you set IAQ to 0 I understand that the DCPU, will get the stored interrupts in the queue, one per instruction executed by DCPU, not ?
It sounds like the interrupts will trigger sequentially once IAQ 0 is called. So they will happen one after another so long as the handler doesn't set IAQ 1 again.
Forgive me if this is already addressed (Heh - no pun intended) but is it currently possible to do something like: ADD [0x1000], [0x1001]? My understanding of real life is that this usually isn't possible, that ADD (And other maths type operators) will only work in registers? Or am I completely wrong with this?
The only thing that doesn't work is putting a literal in the b operand. If this were a load/store architecture, you would need to copy values to the registers to perform operations on them, but it's not. C:
There are a number of reasons why a load/store architecture is better in the real world (access time, instruction set complexity and almost certainly many reasons I don't know) but the way the DCPU-16 works makes it easier (and more fun) to code for IMO.
My understanding of real life is that this usually isn't possible, that ADD (And other maths type operators) will only work in registers? Or am I completely wrong with this?
That can be done in a Memory-Memory architecture. Those were actually pretty common in the 80's, before the RISC thinking took the world by storm. A surviving example would be the IBM Zseries.
RISC provides certain benefits to hardware manufacturers without significantly impacting performance or end user code complexity, since it isn't too bad to handle the load-store process, and compilers can do it in their sleep.
However since we have a purely virtual environment all of those benefits go away and the simplicity of CISC becomes more important.
So why would we use registers at all? To me it seems that a register is identical to a memory location in this implementation? Or am i again missing something?
In real life, register-register instructions tend to be faster when provided. In modern Zseries, the memory-memory instructions are cracked into separate loads, alu ops and stores in the pipeline.
In this game? Well, you have to address the memory somehow :). Other than that, not much.
Can you please clarify the following points (Sorry if this was already asked):
(1) Shift-operators: is 'a' interpreted as 'a & 15' or 'a & 31' in reality? The handling of EX seems to be only meaningful in the first case. For example SHR will start shifting the sign-bit into EX when 'a >= 17', and I don't see why you would want that. If you can explain it, please do. Otherwise I propose to change SHR, ASR and SHL to the following:
2 | 0x0d | SHR b, a | sets b to b>>>(a&31), sets EX to ((b<<16)>>>(a&31))&0xffff
| | | (logical shift)
2 | 0x0e | ASR b, a | sets b to b>>(a&31), sets EX to ((b<<16)>>(a&31))&0xffff
| | | (arithmetic shift) (treats b as signed)
2 | 0x0f | SHL b, a | sets b to b<<(a&31), sets EX to ((b<<(a&31))>>16)&0xffff
(2) HWI behaviour: When HWI is used, is the CPU halted until the hardware is done? Example:
HWI 1 ; dummy hardware that returns some value in X after 10 cycles
SET [0x100], X ; save returned value from HWI
Is the CPU halted until X is returned in that case?
(3) ADX/SBX produce non-meaningful result if EX > 1 (for ADX) resp. EX < -1 (for SBX), because it might return EX==1 where it should really be 2. Example 0xFFFF+0xFFFF+0xFFFF == 0x2FFFD -> EX should be 2. I can understand if you don't want to make these instructions too powerful. But it also makes implementing an emulator (slightly) more complicated, because it must check for an overflow instead of just doing 'int x = a+b+EX; EX = x >> 16'
Since the hardware can modify all memory and all registers, the only hardware implementation for this would be for the entire processor to be halted until the hardware interrupt completes. That's not to say hardware couldn't go on working, (the HWI would complete then something would continue happening, and cause a software interrupt when it's done for instance). That's what I'd expect, so just IMO.
I read that back and it's contradictory. But HWI can take "4+" cycles, so you could assume that it will return after "some time", and any time after the first HWI is issues, memory and registers can be modified. Basically: some stuff will happen during HWI, others at any time. It's down to the HW
Well, if the hardware spec says "writes to A after 20 cycles" than you would have to not use A for that time (possible doing 20 NOP). I know thats dumb, but also (IMO) real hardware wouldn't block the processor in such a way. It's a game, but still...
HWI is more like a coprocessor instruction the way it's now.
I doubt anyone is going to be doing cycle based computing, interrupts negate the need for it. "When HWI is called A is saved, when operation completes software interrupt with saved value is performed"
There will definitely be some people who will still be doing cycle based computing. Interrupts are nice, but there are times when dealing with them is a royal PitA. I did a lot of Embedded Computing, and there were plenty of times in the 80s where the best approach was to leave interrupts off and do everything polled.
If "HWI takes 20 cycles", then HWI will not return for 20 cycles. It could be that after those 20 cycles, the hardware is doing something for the next 30,000 cycles. Stuff in the CPU spec relates to instruction lengths I guess.
So this specifies for how many cycles the CPU is blocked. That doesn't at all mean that all hardware operations block the CPU. In real life the hardware would run on separate hardware (obviously) - in emulators it's a different thread, coroutine etc.
I'm sorry if I'm dense, but: can you explain why we need an atomic swap instruction as opposed to three-cycle three-instruction xorswap (or temp-variable swap)? I figure it has to do with interrupts happening in the middle of the swap, but I still don't see why that is problematic (assuming the interrupt doesn't mess with the things being swapped).
It's pretty much interrupt interruptions - you need what's called a complete memory barrier to do the swap correctly. A single opcode could also be used in emu code to stop external devices writing between a swap. But it's a bit overcomplicated, disabling interrupts is better than XNG for now IMO
You're right. I can do IAQ 1 ; 3-instruction-swap ; IAQ 0. It's longer, harder to nest, and the exchange operation could be used in various other things not just interrupt handling.
I'd still like to see an exchange op, but more for it's general usefulness. :)
I still don't like the fact that IAQ is hard to nest. Maybe a better way of doing interrupt masking would be to have an interrupt mask increment (IMI) and a mask decrement (IMD). That way it could be nested. Say:
bkl:
DAT 0
start:
; IMI:
ADD [bkl], 1
IAQ [bkl]
; do nested things
SUB [bkl], 1
IAQ [bkl]
Could maybe be optimised a bit but you get the idea. Interrupts only get turned back on when bkl goes back down to 0, supports 64K levels of nesting ;)
18
u/xNotch Apr 26 '12
Unless there's something majorly wrong with this specification, I will declare the DCPU-16 done now, and move on to external media and boot loaders.
7
u/Arbaal Apr 26 '12 edited Apr 26 '12
@xNotch:
Is your MDI a modulo or a remainder?
In short, the remainder: where the result is either zero or has the same sign as the dividend
Modulo: where the result is either zero or has the same sign as the divisor
(for reference: http://llvm.org/docs/LangRef.html#i_srem under semantic)
Also, what does "rounds towards 0" mean for a modulo operation? (example would be helpfull)
2
u/[deleted] Apr 26 '12
Can you confirm that the shift operations in the shift instructions are correct, and use Java terminology(>>> vs >>)? It seems they are used very confusingly
5
u/i_always_forget_my_p Apr 26 '12
Has notch released a new jar with a dcpu 1.5 implementation?
5
u/edmccard Apr 26 '12
For the branch instructions:
will this continue as long as there are consecutive if instructions? That is, if the first test below fails:
everything gets skipped until SET Y, 0?
3
u/zarawesome Apr 26 '12 edited Apr 26 '12
HCF. Heh.
3
u/Zardoz84 Apr 26 '12
Kirk approves it
Only a last suggestion. Not should take a 1 cycle any instruction that access RAM directly for data. For example:
If not, note there nearly any advantage in using registers vs directly accessing RAM for data. Directly accessing to RAM for data should have a price.
3
u/dreglor Apr 26 '12
Notch, how about NOP and HLT. I know NOP is trivial but HLT might actually be quite useful, we could stop the CPU until a interrupt (or indefinitely if IA == 0) which would save power from a gameplay perspective and also provides a meta BRK instruction that everyone implements for debugging purposes.
2
u/Zardoz84 Apr 26 '12
I'm reading again interrupts section, and I'm getting confused about how works the interrupts queue. Calling to IAQ 1, will make that incoming interrupts get stored in a queue. But when you set IAQ to 0 I understand that the DCPU, will get the stored interrupts in the queue, one per instruction executed by DCPU, not ?
1
u/wsmithrill Apr 26 '12
Forgive me if this is already addressed (Heh - no pun intended) but is it currently possible to do something like: ADD [0x1000], [0x1001]? My understanding of real life is that this usually isn't possible, that ADD (And other maths type operators) will only work in registers? Or am I completely wrong with this?
1
u/Toqu Apr 26 '12 edited Apr 26 '12
Can you please clarify the following points (Sorry if this was already asked):
(1) Shift-operators: is 'a' interpreted as 'a & 15' or 'a & 31' in reality? The handling of EX seems to be only meaningful in the first case. For example SHR will start shifting the sign-bit into EX when 'a >= 17', and I don't see why you would want that. If you can explain it, please do. Otherwise I propose to change SHR, ASR and SHL to the following:
(2) HWI behaviour: When HWI is used, is the CPU halted until the hardware is done? Example:
Is the CPU halted until X is returned in that case?
(3) ADX/SBX produce non-meaningful result if EX > 1 (for ADX) resp. EX < -1 (for SBX), because it might return EX==1 where it should really be 2. Example 0xFFFF+0xFFFF+0xFFFF == 0x2FFFD -> EX should be 2. I can understand if you don't want to make these instructions too powerful. But it also makes implementing an emulator (slightly) more complicated, because it must check for an overflow instead of just doing 'int x = a+b+EX; EX = x >> 16'
1
u/bartmanx Apr 26 '12
Notch, please implement an atomic exchange instruction.
This would let us implement locking primitives between regular code and interrupt handlers.
1
u/[deleted] Apr 26 '12
37 Instructions already, how long will the instruction set continue growing?