SirCmpwn

Untitled

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