SirCmpwn

Untitled

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