Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
cpu [2022/08/17 18:52] – [Sources of information] simercpu [2022/11/12 14:57] (current) – [Registers] pulkomandy
Line 8: Line 8:
 The V-Smile processor is a Sunplus µ'nSP implementing version **1.1** of the ISA (source: this is how the SPG200 projects in µ'nSP IDE are configured). The V-Smile processor is a Sunplus µ'nSP implementing version **1.1** of the ISA (source: this is how the SPG200 projects in µ'nSP IDE are configured).
  
-The documentation below was extracted from the datasheet of another chip that implements a later version (1.2). Instructions that are available only in that later version are annotated to avoid confusion, but kept for reference in case it is useful to someone else. 
- 
-This document does not include information about µ'nSP 2.0 architecture which is irrelevant to us and possibly not compatible (there are more registers, which would not fit the existing instruction format). 
 ===== Registers ===== ===== Registers =====
  
Line 26: Line 23:
 R3 stores the lower half and R4 stores the upper half. R3 stores the lower half and R4 stores the upper half.
  
-There is also a 4-bit shift buffer (SB) register used by the shift and rotate addressing modes.+There is also a 4-bit shift buffer (SB) register used by the shift and rotate addressing modes. There are separate SB registers for normal, FIQ and interrupt mode, which gets automatically switched.
  
 The BP register has a special addressing mode with a 6-bit immediate offset. It is usually (for example by unsp IDE C compiler) used as a frame pointer, to access a function local variables efficiently. But it can also be used as a normal register if needed. The BP register has a special addressing mode with a 6-bit immediate offset. It is usually (for example by unsp IDE C compiler) used as a frame pointer, to access a function local variables efficiently. But it can also be used as a normal register if needed.
-==== Shadow Registers ==== 
  
-**Only in ABI 1.2**+SP is generally used as a stack pointer but can be used for other things occasionally. When using the PUSH and POP instructions, the stack grows downwards (PUSH decrements SP) and the stack pointer points to the first empty space below the stack.
  
-Registers R1-R4 have 'shadow' copies that can be enabled via the ''SECBANK'' instruction. When enabledall reads and writes to R0-R4 are redirected to SR1-SR4. This can be used to avoid backing up, and restoring registers during Interrupt routines.+As a resultthe typical organization of the stack in VTech code is:
  
 +^Address^ Contents       ^ Notes                              ^
 +| NNNF  | Parameter 0    | Pushed by caller                   |
 +| NNNE  | Parameter 1    |                                    |
 +| NNND  | PC (from CALL) | Saved by CALL                      |
 +| NNNC  | SR (from CALL) | Saved by CALL                      |
 +| NNNB  | R4             | Saved by PUSH in function prelude  |
 +| NNNA  | R3             | (and more registers as needed)     |
 +| NNN9  | Local variable | Space allocated by decrementing SP |
 +| NNN8  | Local variable | <- BP points here                  |
 +| NNN7  | Empty          | <- SP points here                  |
 +
 +The SP can move when calling other functions, interrupts, ... or when saving registers inside the function code. But BP doesn't move and can be used to access both local variables and function parameters.
 +
 +The parameters are usually removed by the caller (by incrementing SP). It would also be possible to use a POP including SR and PC to return from the function while clearing parameters, but this requires loading them into registers, which is not always desirable since registers are rather used for return values.
 ==== Status Register ==== ==== Status Register ====
  
Line 47: Line 57:
 |10-15|DS  |Data Segment |Auto incremented with pre/post inc/dec addressing mode as needed | |10-15|DS  |Data Segment |Auto incremented with pre/post inc/dec addressing mode as needed |
  
- 
-==== Flags register ==== 
- 
-**Only in ABI 1.2** 
- 
-This register is not accessible as a general purpose register. It can be written to by some specific instructions. 
- 
-^16^15^14 ^13 ^12 ^10-7^6^5^4  ^3-0    ^ 
-|- |AQ|BNK|FRA|FIR|SB  |F|I|INE|IRQ PRI| 
- 
-  * AQ: Flag for DIV/DIVS instructions 
-  * BNK: Set when the shadow registers are active (''SECBANK ON'') 
-  * FRA: Fraction mode 
-  * FIR: FIR_MOV mode 
-  * SB: shift buffer used by the ''SHIFT'' instruction. It makes it easy to shift values that use more than 16 bits 
-  * F: FIQ enable (set by the ''INT'' and ''FIQ'' instructions) 
-  * I: IRQ enable (set by the ''INT'' and ''IRQ'' instructions) 
-  * INE: IRQ nesting mode 
-  * IRQ PRI: current IRQ priority 
- 
-In ABI 1.1 and older, there are separate SB registers for normal, FIQ and interrupt mode. The F, I and FIR flags exist, but are not exposed as a register. 
 ====== Address Space ====== ====== Address Space ======
  
Line 172: Line 161:
 ^Syntax                ^naken_asm form           ^Description                                                                ^ ^Syntax                ^naken_asm form           ^Description                                                                ^
 |%% R1 += R2         %%|%% ADD R1, R2          %%|Register                                                                   | |%% R1 += R2         %%|%% ADD R1, R2          %%|Register                                                                   |
-|%% R1 += R2 ASR 1   %%|%% ADD R1, R2 ASR 1    %%|Register with shift (ASR, LSL, LSR, ROR or ROLup to 4 bits               |+|%% R1 += R2 ASR 1   %%|%% ADD R1, R2 ASR 1    %%|Register with arithmetic right shift up to 4 bits                          | 
 +|%% R1 += R2 LSL 1   %%|%% ADD R1R2 LSL 1    %%|Register with logical left shift up to 4 bits                              | 
 +|%% R1 += R2 LSR 1   %%|%% ADD R1R2 LSR 1    %%|Register with logical left shift up to 4 bits                              | 
 +|%% R1 += R2 ROL 1   %%|%% ADD R1R2 ROL 1    %%|Register with 20-bit left rotation through SB up to 4 bits                 | 
 +|%% R1 += R2 ROR 1   %%|%% ADD R1, R2 ROR 1    %%|Register with 20-bit right rotation through SB up to 4 bits                |
 |%% R1 += 23         %%|%% ADD R1, #23         %%|6-bit immediate value (Rd cannot be PC)                                    | |%% R1 += 23         %%|%% ADD R1, #23         %%|6-bit immediate value (Rd cannot be PC)                                    |
 |%% R1 = R2 + 1234   %%|%% ADD R1, R2, #1234   %%|3-operand 16-bit immediate                                                 | |%% R1 = R2 + 1234   %%|%% ADD R1, R2, #1234   %%|3-operand 16-bit immediate                                                 |
Line 192: Line 185:
 The shift operations use the shift buffer (see "shift operations" section below). In particular the ROR and ROL operations do a 20-bit rotation resulting in bits from the shift buffer moving into the destination register. The shift operations use the shift buffer (see "shift operations" section below). In particular the ROR and ROL operations do a 20-bit rotation resulting in bits from the shift buffer moving into the destination register.
  
-===== Bit shift instructions ===== +=== Bit shift addressing modes ===
- +
-**Only in ISA 1.2** +
- +
-Flags affected: NZ +
- +
-Unlike the shift embedded in the parameters of an ALU operation, this is an independant opcode. The smasm syntax makes this clear, in the xasm syntax it can be hard to distinguish the different cases. +
- +
-The main differences in behavior are: +
-  * This instruction can shift by up to 16 bits, instead of just 4 +
-  * The shift operand can be a constant, but also a register, for variable shifts+
  
 Given a starting register like this: Given a starting register like this:
  
-^Bits 15-0^ +^  ^Bits 15-0^ 
-|Rs15-Rs0 |+^Rs|Rs15-Rs0 |
  
-And the SR bits in the flags register:+And the SB bits:
  
-^Bits 3-0^ +^  ^Bits 3-0^ 
-|SR3-SR0 |+^SB|SB3-SB0 |
  
 The result of the shift operations are (for a shift by 3 bits): The result of the shift operations are (for a shift by 3 bits):
  
-==== ASR ====+== ASR ==
  
 Arithmetic shift right (signed divide by two) Arithmetic shift right (signed divide by two)
  
-^Bits 15-13^Bits 12-0^ +^  ^Bits 15-13^Bits 12-0^ 
-|Rs15      |Rs15-Rs3 |+^Rs|Rs15      |Rs15-Rs3 |
  
-^Bits 3-1^Bit 0^ +^  ^Bits 3-1^Bit 0^ 
-|Rs2-Rs0 |SR3  |+^SB|Rs2-Rs0 |SB3  |
  
-==== LSL ====+== LSL ==
  
 Logical shift left (multiply by two) Logical shift left (multiply by two)
  
-^Bits 15-3^Bits 2-0^ +^  ^Bits 15-3^Bits 2-0^ 
-|Rs12-Rs0 |000 |+^Rs|Rs12-Rs0 |0       |
  
-^Bit 3^Bit 2-0^ +^  ^Bit 3^Bit 2-0  
-|SR0  |Rs15-Rs13|+^SB|SB0  |Rs15-Rs13|
  
-==== LSR ====+== LSR ==
  
 Logical shift right (unsigned divide by two) Logical shift right (unsigned divide by two)
  
-^Bits 15-13^Bits 12-0^ +^  ^Bits 15-13^Bits 12-0^ 
-|0      |Rs15-Rs3 |+^Rs|0      |Rs15-Rs3    |
  
-^Bits 3-1^Bit 0^ +^  ^Bits 3-1^Bit 0^ 
-|Rs2-Rs0 |SR3  |+^SB|Rs2-Rs0 |SB3  |
  
-==== ROL ====+== ROL ==
  
-Rotate left+Rotate left through shift buffer
  
-^Bits 15-3^Bits 2-0^ +^  ^Bits 15-3^Bits 2-0^ 
-|Rs12-Rs0 |SR3-SR1 |+^Rs|Rs12-Rs0 |SB3-SB1 |
  
-^Bit 3^Bit 2-0^ +^  ^Bit 3^Bit 2-0  
-|SR0  |Rs15-Rs13|+^SB|SB0  |Rs15-Rs13|
  
-==== ROR ====+== ROR ==
  
-Rotate right+Rotate right through shift buffer
  
-^Bits 15-13^Bits 12-0^ +^  ^Bits 15-13^Bits 12-0^ 
-|SR2-SR0   |Rs15-Rs3 |+^Rs|SB2-SB0   |Rs15-Rs3 |
  
-^Bits 3-1^Bit 0^ +^  ^Bits 3-1^Bit 0^ 
-|Rs2-Rs0 |SR3  |+^SB|Rs2-Rs0 |SB3  |
  
-==== ASR-OR, LSL-OR, LSR-OR ==== 
- 
-These operations do the shift as documented above, then OR it with the original register value. 
- 
-xasm syntax: 
- 
-''R1 |= R1 LSL R2'' 
- 
-==== 32-bit shifts ==== 
- 
-The assembler has a shorthand instruction for 32bit shifts (it is assembled as two consecutive 16bit instructions): 
- 
-''MR |= R4 asr 1'' 
- 
-FIXME 
-  * The documentation says ROL and ROR can't use R4 as a source register? 
-===== Bit operations ===== 
- 
-**Only in ISA 1.2** 
- 
-Flag affected: Z 
- 
-''TSTB'' - Test bit 
- 
-''CLRB'' - Clear bit 
- 
-''SETB'' - Set bit 
- 
-''INVB'' - Flip bit 
- 
-The destination operand can be a register, or an indirect register with or without D: prefix. 
-The source operand can be a register or an immediate value (in the range 0-15). If it is a register only the 4 LSB are used. 
- 
-The Z flag is set according to the value of the bit **before** the operation is executed. 
- 
-''CLRB R3,2'' Copies bit 2 of R3 in the Z flag, then clears the bit in the register. 
- 
-''TSTB D:[R1],13'' copies bit 13 of D:[R1] in the Z flag. 
 ===== Multiplication and division ===== ===== Multiplication and division =====
  
Line 313: Line 258:
 |%%MR = Rd x Rs          %%|%%MUL.SS Rd, Rs        %%|Signed multiplication                                                                                        | |%%MR = Rd x Rs          %%|%%MUL.SS Rd, Rs        %%|Signed multiplication                                                                                        |
 |%%MR = Rd x Rs,us       %%|%%MUL.US Rd, Rs        %%|Rd is unsigned, Rs is signed                                                                    | :::          | |%%MR = Rd x Rs,us       %%|%%MUL.US Rd, Rs        %%|Rd is unsigned, Rs is signed                                                                    | :::          |
-|%%MR = Rd x Rs,uu       %%|%%MUL.UU Rd, Rs        %%|Unsigned multiplication **ISA 1.2 only**                                                        | :::          | +|%%MR = [Rd] x [Rs],   %%|%%MAC [Rd], [Rs], N    %%|Multiply-accumulate two sets of N signed values pointed by Rd and Rs                            |              |
-|%%MR = [Rd] x [Rs],   %%|%%MAC [Rd], [Rs], N    %%|Multiply-accumulate two sets of N signed values pointed by Rd and Rs                            | S            |+
 |%%MR = [Rd] x [Rs],us,n %%|%%MAC.US [Rd], [Rs], N %%|Multiply-accumulate two sets of N values pointed by Rd (unsigned values) and Rs (signed values) | :::          | |%%MR = [Rd] x [Rs],us,n %%|%%MAC.US [Rd], [Rs], N %%|Multiply-accumulate two sets of N values pointed by Rd (unsigned values) and Rs (signed values) | :::          |
-|%%MR = [Rd] x [Rs],uu,n %%|%%MAC.UU [Rd], [Rs], N %%|Multiply-accumulate two sets of N unsigned values pointed by Rd and Rs **ISA 1.2 only**         | :::          | 
- 
-Multiply-accumulate operations do the computation with 36-bit precision. The result is stored in MR and the shift buffer in the flags register. S flag is set if there is an overflow. 
-==== Division ==== 
- 
-**Only in ISA 1.2** 
- 
-''DIVS MR, R2'' 
- 
-''DIVQ MR, R2'' 
- 
-These instructions implement 32-bit division. There are two divide primitives, DIVS 
-and DIVQ. A single precision divide, with a 32-bit numerator and a 16-bit 
-denominator, yielding a 16-bit quotient, executes in 16*3 cycles. Higher precision 
-divides are also possible. The division can be either signed or unsigned, but both the 
-numerator and denominator must be the same. Place the 32-bit numerator at R4: R3, 
-the 16-bit denominator at R2 and clear the AQ flag then executed with the divide 
-primitives, DIVS and DIVQ. Repeated execution of DIVQ implements a non-restoring 
-conditional add-subtract division algorithm. At the conclusion of divide operation the 
-quotient will be placed at R3. 
  
 +Multiply-accumulate operations do the computation with 32-bit precision with the result is stored in MR. The Rd and Rs pointers will automatically be incremented by N.
 +The values of the array pointed to by the Rd array will each be moved one index forward when the FIR_MOV setting is enabled, see the FIR_MOV section for details.
 ==== FIR_MOV ==== ==== FIR_MOV ====
  
Line 343: Line 269:
 ''FIR_MOV OFF'' ''FIR_MOV OFF''
  
-Affects the FIR bit in the flags register. +Affects the FIR setting used by the MULS instructions.
- +
-When enabled, the multiply-accumulate instructions automatically increment Rd and Rs by n. When disabled, they do not change Rd and Rs. +
- +
-==== FRACTION ==== +
- +
-''FRACTION ON'' +
- +
-''FRACTION OFF'' +
- +
-**Only in ISA 1.2** +
- +
-When enabled, multiply operations shift the result left by 1 bit +
- +
-===== Other operations ===== +
- +
-==== EXP ==== +
- +
-**Only in ISA 1.2** +
- +
-''R1 = EXP R2'' +
- +
-Returns the number of sign bits in R2, that is, count the from the MSB the number of bits that all have the same value. +
-===== Data Segment Access ===== +
- +
-**Only in ISA 1.2** +
- +
-''DS = Rs'' +
- +
-''Rd = DS'' +
- +
-''DS = 12''+
  
-Set or Get the DS value from SRThe other 10 bits of Rd are set to 0.+When enabled, MULS instructions will modify the array pointed to by the Rd register after finishing the calculation by moving each value one index forward with the last value discarded and the first value left in placeThis can be used for automatically advancing the time step when implementing FIR filters, with the Rd array storing the input signal and the Rs array storing the weights.
  
-On older architecture versions, this has to be done manually by accessing SR like a normal register (masking with AND and OR). 
 ===== Program Flow ===== ===== Program Flow =====
  
Line 419: Line 313:
  
 Because SR is pushed automatically, flags are always saved accross a CALL/RETF Because SR is pushed automatically, flags are always saved accross a CALL/RETF
- 
-=== Indirect call === 
- 
-**Only in ISA 1.2** 
- 
-''CALL MR'' 
- 
-The target address is formed from MR (R4 and R3). R4 contains the segment (loaded into CS) and R3 contains the offset (loaded into PC). 
  
 === GOTO === === GOTO ===
  
-Like a call, but does not save PC and SR on the stack. Both direct (ISA 1.1) and indirect (**ISA 1.2 only**) versions are available. +Like a call, but does not save PC and SR on the stack.
 In ISA 1.0, GOTO does not set the CS: so it is not possible to jump outside the current segment. In ISA 1.1 and above this problem is fixed. In ISA 1.0, GOTO does not set the CS: so it is not possible to jump outside the current segment. In ISA 1.1 and above this problem is fixed.
  
Line 445: Line 330:
  
 Jumps to the BREAK software IRQ handler. Jumps to the BREAK software IRQ handler.
- 
-=== NOP === 
- 
-**Only in ISA 1.2** 
- 
-Do nothing. 
  
 ===== Interrupt control ===== ===== Interrupt control =====
Line 466: Line 345:
 |INT OFF     |Disable FIQ and IRQ                  | |INT OFF     |Disable FIQ and IRQ                  |
  
-=== SECBANK === 
- 
-**Only in ISA 1.2** 
- 
-''SECBANK ON'' 
- 
-''SECBANK OFF'' 
- 
-Switches to the alternate registers SR1-SR4 or restores the normal R1-R4. 
- 
-The status is stored in the flags register. 
- 
-=== IRQNEST === 
- 
-**Only in ISA 1.2** 
- 
-''IRQNEST ON'' 
- 
-''IRQNEST OFF'' 
- 
-Enables IRQ nesting. IRQ will save the flags register on the stack (in addition to SR and PC) and the RETI instruction will restore it. 
- 
-The IRQ priority can be set in FR to determine which interruptions are allowed to happen. 
- 
-The status is stored in the INE bit of the flags register. 
- 
-=== Direct access to FR register === 
- 
-**Only in ISA 1.2** 
- 
-''FR = Rs'' 
- 
-''Rd = FR'' 
  
  
cpu.1660755145.txt.gz · Last modified: 2022/08/17 18:52 by simer
CC Attribution 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0