SirCmpwn

Untitled

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