SirCmpwn

Untitled

Apr 25th, 2012
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.18 KB | None | 0 0
  1. ***This is a 3rd-party extension of the DCPU specification. The changes are as follows:
  2. * Addition of three instructions: SMP, RMP, HWP, and HWU.
  3. * Slightly modified how interrupts are handled for better hardware support.
  4. * Addition of hot-swapping critieria.
  5. * Added protection to changing IA.
  6. ***Justification for changes:
  7. These changes make it more difficult for unintentional crashes to occur. For instance, a
  8. kernel in the current specification could be accidently corrupted by a user program, causing
  9. the machine state to be extremely unstable. Or, a small corruption could introduce a bug
  10. in a very isolated area of the kernel, perhaps only affecting a program or two and being very
  11. difficult to detect. Additionally, a malicious program could introduce an infinite loop and
  12. disable interrupts to crash the device.
  13.  
  14. With these changes, kernels and more could be made temporarily (or permenately) read-only,
  15. preventing corruption. Additionally, on multitasking systems, hardware usage could be
  16. managed by the kernel, preventing conflict by locking down hardware use. Hot-swapping is
  17. included here merely for convenience. With the protection of IA, it also adds added stability
  18. by allowing a kernel or system interrupt to have a key-combination based safety mechanism
  19. that could, under all circumstances, recover from a crashed program.
  20.  
  21.  
  22. DCPU-16 Specification [3rd-party extension]
  23. Copyright 1985 Mojang
  24. Version 1.4
  25.  
  26.  
  27.  
  28. === SUMMARY ====================================================================
  29.  
  30. * 16 bit words
  31. * 0x10000 words of ram
  32. * 8 registers (A, B, C, X, Y, Z, I, J)
  33. * program counter (PC)
  34. * stack pointer (SP)
  35. * extra/excess (EX)
  36. * interrupt address (IA)
  37.  
  38. In this document, anything within [brackets] is shorthand for "the value of the
  39. RAM at the location of the value inside the brackets". For example, SP means
  40. stack pointer, but [SP] means the value of the RAM at the location the stack
  41. pointer is pointing at.
  42.  
  43. Whenever the CPU needs to read a word, it reads [PC], then increases PC by one.
  44. Shorthand for this is [PC++]. In some cases, the CPU will modify a value before
  45. reading it, in this case the shorthand is [++PC].
  46.  
  47. For stability and to reduce bugs, it's strongly suggested all multi-word
  48. operations use little endian in all DCPU-16 programs, wherever possible.
  49.  
  50.  
  51.  
  52. === INSTRUCTIONS ===============================================================
  53.  
  54. Instructions are 1-3 words long and are fully defined by the first word.
  55. In a basic instruction, the lower five bits of the first word of the instruction
  56. are the opcode, and the remaining eleven bits are split into a five bit value b
  57. and a six bit value a.
  58. b is always handled by the processor after a, and is the lower five bits.
  59. In bits (in LSB-0 format), a basic instruction has the format: aaaaaabbbbbooooo
  60.  
  61. In the tables below, C is the time required in cycles to look up the value, or
  62. perform the opcode, VALUE is the numerical value, NAME is the mnemonic, and
  63. DESCRIPTION is a short text that describes the opcode or value.
  64.  
  65.  
  66.  
  67. --- Values: (5/6 bits) ---------------------------------------------------------
  68. C | VALUE | DESCRIPTION
  69. ---+-----------+----------------------------------------------------------------
  70. 0 | 0x00-0x07 | register (A, B, C, X, Y, Z, I or J, in that order)
  71. 0 | 0x08-0x0f | [register]
  72. 1 | 0x10-0x17 | [register + next word]
  73. 0 | 0x18 | (PUSH / [--SP]) if in b, or (POP / [SP++]) if in a
  74. 0 | 0x19 | [SP] / PEEK
  75. 1 | 0x1a | [SP + next word] / PICK n
  76. 0 | 0x1b | SP
  77. 0 | 0x1c | PC
  78. 0 | 0x1d | EX
  79. 1 | 0x1e | [next word]
  80. 1 | 0x1f | next word (literal)
  81. 0 | 0x20-0x3f | literal value 0xffff-0x1e (-1..30) (literal) (only for a)
  82. --+-----------+----------------------------------------------------------------
  83.  
  84. * "next word" means "[PC++]". Increases the word length of the instruction by 1.
  85. * By using 0x18, 0x19, 0x1a as PEEK, POP/PUSH, and PICK there's a reverse stack
  86. starting at memory location 0xffff. Example: "SET PUSH, 10", "SET X, POP"
  87.  
  88.  
  89.  
  90. --- Basic opcodes (5 bits) ----------------------------------------------------
  91. C | VAL | NAME | DESCRIPTION
  92. ---+------+----------+--------------------------------------------------------
  93. - | 0x00 | n/a | special instruction - see below
  94. 1 | 0x01 | SET b, a | sets b to a
  95. 2 | 0x02 | ADD b, a | sets b to b+a, sets EX to 0x0001 if there's an overflow,
  96. | | | 0x0 otherwise
  97. 2 | 0x03 | SUB b, a | sets b to b-a, sets EX to 0xffff if there's an underflow,
  98. | | | 0x0 otherwise
  99. 2 | 0x04 | MUL b, a | sets b to b*a, sets EX to ((b*a)>>16)&0xffff (treats b,
  100. | | | a as unsigned)
  101. 2 | 0x05 | MLI b, a | like MUL, but treat b, a as signed
  102. 3 | 0x06 | DIV b, a | sets b to b/a, sets EX to ((b<<16)/a)&0xffff. if a==0,
  103. | | | sets b and EX to 0 instead. (treats b, a as unsigned)
  104. 3 | 0x07 | DVI b, a | like DIV, but treat b, a as signed
  105. 3 | 0x08 | MOD b, a | sets b to b%a. if a==0, sets b to 0 instead.
  106. 1 | 0x09 | AND b, a | sets b to b&a
  107. 1 | 0x0a | BOR b, a | sets b to b|a
  108. 1 | 0x0b | XOR b, a | sets b to b^a
  109. 2 | 0x0c | SHR b, a | sets b to b>>>a, sets EX to ((b<<16)>>a)&0xffff
  110. | | | (logical shift)
  111. 2 | 0x0d | ASR b, a | sets b to b>>a, sets EX to ((b<<16)>>>a)&0xffff
  112. | | | (arithmetic shift) (treats b as signed)
  113. 2 | 0x0e | SHL b, a | sets b to b<<a, sets EX to ((b<<a)>>16)&0xffff
  114. 2 | 0x0f | MVI b, a | sets b to a, then increases I and J by 1
  115. 2+| 0x10 | IFB b, a | performs next instruction only if (b&a)!=0
  116. 2+| 0x11 | IFC b, a | performs next instruction only if (b&a)==0
  117. 2+| 0x12 | IFE b, a | performs next instruction only if b==a
  118. 2+| 0x13 | IFN b, a | performs next instruction only if b!=a
  119. 2+| 0x14 | IFG b, a | performs next instruction only if b>a
  120. 2+| 0x15 | IFA b, a | performs next instruction only if b>a (signed)
  121. 2+| 0x16 | IFL b, a | performs next instruction only if b<a
  122. 2+| 0x17 | IFU b, a | performs next instruction only if b<a (signed)
  123. - | 0x18 | - |
  124. - | 0x19 | - |
  125. 3 | 0x1a | ADX b, a | sets b to b+a+EX, sets EX to 0x0001 if there is an over-
  126. | | | flow, 0x0 otherwise
  127. 3 | 0x1b | SUX b, a | sets b to b-a+EX, sets EX to 0xFFFF if there is an under-
  128. | | | flow, 0x0 otherwise
  129. - | 0x1c | - |
  130. - | 0x1d | - |
  131. - | 0x1e | SMP b, a | sets the address range b-a to be read-only. This is a
  132. | | | NOP unless executed from read-only memory or PC < 0x1000.
  133. 2 | 0x1f | RMP b, a | sets the address range b-a to be writeable. This is a
  134. | | | NOP unless executed from read-only memory or PC < 0x1000.
  135. ---+------+----------+----------------------------------------------------------
  136.  
  137. * The branching opcodes take one cycle longer to perform if the test fails
  138. * Signed numbers are represented using two's complement.
  139.  
  140.  
  141.  
  142. Special opcodes always have their lower five bits unset, have one value and a
  143. five bit opcode. In binary, they have the format: aaaaaaooooo00000
  144. The value (a) is in the same six bit format as defined earlier.
  145.  
  146. --- Special opcodes: (6 bits) --------------------------------------------------
  147. C | VAL | NAME | DESCRIPTION
  148. ---+------+-------+-------------------------------------------------------------
  149. - | 0x00 | n/a | reserved for future expansion
  150. 3 | 0x01 | JSR a | pushes the address of the next instruction to the stack,
  151. | | | then sets PC to a
  152. - | 0x02 | - |
  153. - | 0x03 | - |
  154. - | 0x04 | - |
  155. - | 0x05 | - |
  156. - | 0x06 | - |
  157. - | 0x07 | - |
  158. 4 | 0x08 | INT a | triggers a software interrupt with message a
  159. 1 | 0x09 | IAG a | sets a to IA. This is a NOP unless executed from read-only
  160. | | | memory or PC < 0x1000.
  161. 1 | 0x0a | IAS a | sets IA to a. This is a NOP unless executed from read-only
  162. | | | memory or PC < 0x1000.
  163. - | 0x0b | - |
  164. - | 0x0c | - |
  165. - | 0x0d | - |
  166. - | 0x0e | - |
  167. - | 0x0f | - |
  168. 2 | 0x10 | HWN a | sets a to number of connected hardware devices
  169. 4 | 0x11 | HWQ a | sets A, B, C, X, Y registers to information about hardware a
  170. | | | a+b is a 32 bit word identifying the hardware type
  171. | | | c is the hardware revision
  172. | | | x+y is a 32 bit word identifying the manufacturer
  173. 4+| 0x12 | HWI a | sends an interrupt to hardware a
  174. 2 | 0x13 | HWP a | adds hardware protection to hardware a. This is a NOP unless
  175. | | | executed from read-only memory or PC < 0x1000.
  176. 2 | 0x14 | HWU a | removes hardware protection from hardware a. This is a NOP
  177. | | | unless executed from read-only memory or PC < 0x1000.
  178. - | 0x15 | - |
  179. - | 0x16 | - |
  180. - | 0x17 | - |
  181. - | 0x18 | - |
  182. - | 0x19 | - |
  183. - | 0x1a | - |
  184. - | 0x1b | - |
  185. - | 0x1c | - |
  186. - | 0x1d | - |
  187. - | 0x1e | - |
  188. - | 0x1f | - |
  189. ---+------+-------+-------------------------------------------------------------
  190.  
  191.  
  192.  
  193. === INTERRUPTS =================================================================
  194.  
  195. The DCPU-16 will perform at most one interrupt between each instruction.
  196.  
  197. When IA is set to something other than 0, interrupts triggered on the DCPU-16
  198. will push PC to the stack, followed by pushing A and B to the stack (in that order),
  199. then set the PC to IA, A to the interrupt message, and B to the interrupting device
  200. number. A well formed interrupt handler must pop A and B from the stack before
  201. returning (popping PC from the stack)
  202.  
  203. If IA is set to 0, a triggered interrupt does nothing. Software interrupts still
  204. take up two clock cycles, but immediately return, hardware interrupts are
  205. ignored, with the hardware being notified of this. Some hardware may choose to
  206. attempt to trigger the interrupt again at a later point.
  207.  
  208. The DCPU-16 has no way of knowing when an interrupt handler has finished, so if
  209. an interrupt is triggered while an interrupt is being handled, the handler will
  210. get called twice. Calling IAS 0 immediately at the start of the handler will
  211. reliably prevent multiple concurrent interrupts.
  212.  
  213.  
  214.  
  215. === HARDWARE ===================================================================
  216.  
  217. The DCPU-16 supports up to 65534 connected hardware devices. These devices can
  218. be anything from additional storage, sensors, monitors or speakers.
  219. How to control the hardware is specified per hardware device, but the DCPU-16
  220. supports a standard enumeration method for detecting connected hardware via
  221. the HWN, HWQ and HWI instructions.
  222.  
  223. Interrupts sent to hardware can't contain messages, can take additional cycles,
  224. and can read or modify any registers or memory adresses on the DCPU-16. This
  225. behavior changes per hardware device and is described in the hardware's
  226. documentation.
  227.  
  228. Hardware must NOT start modifying registers or ram on the DCPU-16 before at
  229. least one HWI call has been made to the hardware.
  230.  
  231. When hardware is disconnected from the CPU (hot-swapped), an interrupt is sent
  232. with a B set to 0xFFFF. The message (A) is the device number that has been
  233. disconnected. The same proceduce is used for devices when connected, except
  234. that B is set to 0xFFFE.
  235.  
  236. Hardware that is "protected" may only have interrupts triggered from read-only
  237. memory.
Advertisement
Add Comment
Please, Sign In to add comment