Advertisement
Guest User

DCPU-32 Documentation

a guest
Sep 10th, 2013
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
DCPU-16 11.54 KB | None | 0 0
  1. --------------------------------------------------------------------------------
  2. DCPU-32 Specification
  3. Copyright 1989 Maraver
  4. Version 1.0
  5.  
  6.  
  7.  
  8. === SUMMARY =====================================================================
  9.  
  10. * 16-bit words, 32-bit dwords
  11. * 0x10000 dwords of ram
  12.  
  13. When working with the DCPU-32, a words is always a 16-bit unsigned number and a
  14. dword is always a signed (two's-complement) 32-bit number.
  15.  
  16. In this table T is the type of regiser, either D for data or A for address. All
  17. data registers are dwords, and all adress registers are words. NAME(S) is the
  18. mnemonic of the register, or registers, avaliable of that type and description.
  19. DESCRIPTION is a short text that describes either the type of data, or use of the
  20. address, depending on the register type.
  21.  
  22. --- Registers (16/32 bits) ------------------------------------------------------
  23. T | NAME(S)      | DESCRIPTION
  24. ---+--------------+--------------------------------------------------------------
  25. D | A, B, C, X,  | integer registers
  26. D |  Y, Z, I, J  |
  27. D | FA, FB, FC   | floating-point registers
  28. A | PC           | program counter
  29. A | SP           | stack pointer
  30. A | IA           | interrupt address
  31. ---+--------------+--------------------------------------------------------------
  32.  
  33. In this document, anything within [brackets] is shorthand for "the value of the
  34. RAM at the location of the value inside the brackets". For example, SP means
  35. stack pointer, but [SP] means the value of the RAM at the location the stack
  36. pointer is pointing at.
  37.  
  38. Whenever the CPU needs to read a word, it reads [PC], then increases PC by one.
  39. Shorthand for this is [PC++]. In some cases, the CPU will modify a value before
  40. reading it, in this case the shorthand is [++PC].
  41.  
  42. The DCPU-32 is intended to be backwards compatiable with the DCPU-16 inorder
  43. to make the migration between the systems as easy as possible. However, for
  44. security and additional protection from accidental harm, it is recommended
  45. that all DCPU-32 applications use big endian wherever possible.
  46.  
  47.  
  48.  
  49. === INSTRUCTIONS ================================================================
  50.  
  51. Instructions are 1-3 words long and are fully defined by the first word.
  52. In a basic instruction, the lower eight bits of the first word of the instruction
  53. are the opcode, the next sixteen bits are shared between the a and b values. The
  54. remaining eight bits are reserved for future expansion. The b value is always
  55. handled by the processor after a, and is the lower eight bits.
  56. In bits (in LSB-0 format), a basic instruction has the format:
  57. RRRRRRRRaaaaaaaabbbbbbbboooooooo
  58.  
  59. In the tables below, C is the time required in cycles to look up the value, or
  60. perform the opcode, VALUE is the numerical value, NAME is the mnemonic, and
  61. DESCRIPTION is a short text that describes the opcode or value.
  62.  
  63.  
  64.  
  65. --- Values: (5/6 bits) ----------------------------------------------------------
  66. C | VALUE     | DESCRIPTION
  67. ---+-----------+-----------------------------------------------------------------
  68. 0 | 0x00-0x07 | register (A, B, C, X, Y, Z, I or J, in that order)
  69. 0 | 0x08-0x0f | [register]
  70. 1 | 0x10-0x17 | [register + next word]
  71. 0 |      0x18 | (PUSH / [--SP]) if in b, or (POP / [SP++]) if in a
  72. 0 |      0x19 | [SP] / PEEK
  73. 1 |      0x1a | [SP + next word] / PICK n
  74. 0 |      0x1b | SP
  75. 0 |      0x1c | PC
  76. 0 |      0x1d | EX
  77. 1 |      0x1e | [next word]
  78. 1 |      0x1f | next word (literal)
  79. 0 | 0x20-0x22 | float register (FA, FB, or FC, in that order)
  80. 0 | 0x23-0xff | literal value -0x6e..0x6e (-110..110) (literal) (only for a)
  81. --+-----------+-----------------------------------------------------------------
  82.  
  83. * Attempting to access a location in ram with a value past 0xffff, which includes
  84.  all negative numbers, may cause unexpected results due to the value being masked
  85.  Example: 80000 is 14464 after being masked (80000 & 0xffff).
  86.  
  87. * "next word" means "[PC++] & 0xffff". This increases the dword length of the
  88.  instruction by 1, but returns a word value.
  89.  
  90. * By using 0x18, 0x19, 0x1a as PEEK, POP/PUSH, and PICK there's a reverse stack
  91.   starting at memory location 0xffff. Example: "SET PUSH, 10", "SET X, POP"
  92.  
  93. * Attempting to write to a literal value fails silently
  94.  
  95.  
  96.  
  97. --- Basic opcodes (5 bits) -----------------------------------------------------
  98.  C | VAL  | NAME     | DESCRIPTION
  99. ---+------+----------+----------------------------------------------------------
  100.  - | 0x00 | n/a      | special instruction - see below
  101.  1 | 0x01 | SET b, a | sets b to a
  102.  2 | 0x02 | ADD b, a | sets b to b+a, sets EX to 0x0001 if there's an overflow,
  103.   |      |          | 0x0 otherwise
  104. 2 | 0x03 | SUB b, a | sets b to b-a, sets EX to 0xffff if there's an underflow,
  105.    |      |          | 0x0 otherwise
  106.  2 | 0x04 | MUL b, a | sets b to b*a, sets EX to ((b*a)>>16)&0xffff
  107.  2 | 0x05 | MLI b, a | compatibility alias for MUL, as all values are signed
  108.  3 | 0x06 | DIV b, a | sets b to b/a, sets EX to ((b<<16)/a)&0xffff. if a==0,
  109.    |      |          | sets b and EX to 0 instead. Rounds towards 0
  110.  3 | 0x07 | DVI b, a | compatibility alias for DIV (always signed)
  111.  3 | 0x08 | MOD b, a | sets b to b%a. if a==0, sets b to 0 instead.
  112.  3 | 0x09 | MDI b, a | compatibility alias for MOD (always signed; MOD -7, 16 == -7)
  113.  1 | 0x0a | AND b, a | sets b to b&a
  114.  1 | 0x0b | BOR b, a | sets b to b|a
  115.  1 | 0x0c | XOR b, a | sets b to b^a
  116.  1 | 0x0d | SHR b, a | sets b to b>>>a, sets EX to ((b<<16)>>a)&0xffff
  117.    |      |          | (logical shift)
  118.  1 | 0x0e | ASR b, a | sets b to b>>a, sets EX to ((b<<16)>>>a)&0xffff
  119.    |      |          | (arithmetic shift) (treats b as signed)
  120.  1 | 0x0f | SHL b, a | sets b to b<<a, sets EX to ((b<<a)>>16)&0xffff
  121.  2+| 0x10 | IFB b, a | performs next instruction only if (b&a)!=0
  122.  2+| 0x11 | IFC b, a | performs next instruction only if (b&a)==0
  123.  2+| 0x12 | IFE b, a | performs next instruction only if b==a
  124.  2+| 0x13 | IFN b, a | performs next instruction only if b!=a
  125.  2+| 0x14 | IFG b, a | performs next instruction only if b>a
  126.  2+| 0x15 | IFA b, a | performs next instruction only if b>a (signed)
  127.  2+| 0x16 | IFL b, a | performs next instruction only if b<a
  128.  2+| 0x17 | IFU b, a | performs next instruction only if b<a (signed)
  129.  - | 0x18 | -        |
  130.  - | 0x19 | -        |
  131.  3 | 0x1a | ADX b, a | sets b to b+a+EX, sets EX to 0x1 if there is an over-
  132.    |      |          | flow, -0x1 if there is an underflow, 0x0 otherwise
  133.  3 | 0x1b | SBX b, a | sets b to b-a+EX, sets EX to 0x1 if there is an over-
  134.    |      |          | flow, -0x1 if there is an underflow, 0x0 otherwise
  135.  - | 0x1c | -        |
  136.  - | 0x1d | -        |
  137.  2 | 0x1e | STI b, a | sets b to a, then increases I and J by 1
  138.  2 | 0x1f | STD b, a | sets b to a, then decreases I and J by 1
  139. ---+------+----------+-----------------------------------------------------------
  140.  
  141. * The conditional opcodes take one cycle longer to perform if the test fails.
  142.   When they skip a conditional instruction, they will skip an additional
  143.   instruction at the cost of one extra cycle. This continues until a non-
  144.   conditional instruction has been skipped. This lets you easily chain
  145.   conditionals. Interrupts are not triggered while the DCPU-16 is skipping.
  146.  
  147. * How the result of an operation is stored in determined by the data type of b.
  148.   If b is a float the value is stored as a float, otherwise it is converted to
  149.   an integer by rounding towards 0.
  150.  
  151. * All values are treated as signed dwords when doing operations. For compatibility
  152.   unsigned DCPU-16 operators are included as aliases for the signed operation.
  153.  
  154.    
  155. Special opcodes always have their lower eight bits unset, have one value and an
  156. eight bit opcode. In binary, they have the format:
  157. aaaaaaaaoooooooo00000000
  158. The value (a) is in the same eight bit format as defined earlier.
  159.  
  160. --- Special opcodes: (5 bits) --------------------------------------------------
  161.  C | VAL  | NAME  | DESCRIPTION
  162. ---+------+-------+-------------------------------------------------------------
  163.  - | 0x00 | n/a   | reserved for future expansion
  164.  3 | 0x01 | JSR a | pushes the address of the next instruction to the stack,
  165.    |      |       | then sets PC to a
  166.  - | 0x02 | -     |
  167.  - | 0x03 | -     |
  168.  - | 0x04 | -     |
  169.  - | 0x05 | -     |
  170.  - | 0x06 | -     |
  171.  - | 0x07 | -     |
  172.  4 | 0x08 | INT a | triggers a software interrupt with message a
  173.  1 | 0x09 | IAG a | sets a to IA
  174.  1 | 0x0a | IAS a | sets IA to a
  175.  3 | 0x0b | RFI a | disables interrupt queueing, pops A from the stack, then
  176.    |      |       | pops PC from the stack
  177.  2 | 0x0c | IAQ a | if a is nonzero, interrupts will be added to the queue
  178.    |      |       | instead of triggered. if a is zero, interrupts will be
  179.    |      |       | triggered as normal again
  180.  - | 0x0d | -     |
  181.  - | 0x0e | -     |
  182.  - | 0x0f | -     |
  183.  2 | 0x10 | HWN a | sets a to number of connected hardware devices
  184.  4 | 0x11 | HWQ a | sets A, B, and C registers to information about hardware a
  185.    |      |       | A is a 32 bit word identifying the hardware id
  186.    |      |       | B is a 32 bit word identifying the manufacturer
  187.    |      |       | the lowest 16 bits of C is the hardware version
  188.  4+| 0x12 | HWI a | sends an interrupt to hardware a
  189.  - | 0x13 | -     |
  190.  - | 0x14 | -     |
  191.  - | 0x15 | -     |
  192.  - | 0x16 | -     |
  193.  - | 0x17 | -     |
  194.  - | 0x18 | -     |
  195.  - | 0x19 | -     |
  196.  - | 0x1a | -     |
  197.  - | 0x1b | -     |
  198.  - | 0x1c | -     |
  199.  - | 0x1d | -     |
  200.  - | 0x1e | -     |
  201.  - | 0x1f | -     |
  202. ---+------+-------+-------------------------------------------------------------
  203.  
  204.  
  205.  
  206. === INTERRUPTS =================================================================  
  207.  
  208. The DCPU-32 will perform at most one interrupt between each instruction. If
  209. multiple interrupts are triggered at the same time, they are added to a queue.
  210. If the queue grows longer than 256 interrupts, the DCPU-32 will catch fire.
  211.  
  212. When IA is set to something other than 0, interrupts triggered on the DCPU-32
  213. will turn on interrupt queueing, push PC to the stack, followed by pushing A to
  214. the stack, then set the PC to IA, and A to the interrupt message.
  215.  
  216. If IA is set to 0, a triggered interrupt does nothing. Software interrupts still
  217. take up four clock cycles, but immediately return, incoming hardware interrupts
  218. are ignored. Note that a queued interrupt is considered triggered when it leaves
  219. the queue, not when it enters it.
  220.  
  221. Interrupt handlers should end with RFI, which will disable interrupt queueing
  222. and pop A and PC from the stack as a single atomic instruction.
  223. IAQ is normally not needed within an interrupt handler, but is useful for time
  224. critical code.
  225.  
  226.  
  227.  
  228.  
  229. === HARDWARE ===================================================================    
  230.  
  231. The DCPU-32 supports up to 65535 connected hardware devices, attempting to
  232. connect more than that will cause it to catch fire. These devices can be
  233. anything from additional storage, monitors, processors or speakers. How to control
  234. the hardware is specified per hardware device, but the DCPU-32 supports a standard
  235. enumeration method for detecting connected hardware via the HWN, HWQ and HWI
  236. instructions.
  237.  
  238. Interrupts sent to hardware can't contain messages, can take additional cycles,
  239. and can read or modify any registers or memory adresses on the DCPU-32. This
  240. behavior changes per hardware device and is described in the hardware's
  241. documentation.
  242.  
  243. The DPCU-32 does not support hot swapping hardware. The behavior of connecting
  244. or disconnecting hardware while the DCPU-32 is running is undefined.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement