Advertisement
Guest User

Untitled

a guest
Sep 14th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @ 10074690, Ben Kennedy
  2. @ Assignment 3
  3. /*
  4.  * A simple ARM assembler program to draw a rotating cube on the screen
  5.  * calls fb_setbuffer to set the address of the display frame buffer
  6.  * for double buffering.
  7.  * M.Johnson 2010
  8. */
  9.         .global _start
  10.         .equ    cx,320      @ centre of the
  11.         .equ    cy,240      @ screen (320,240)
  12.         .equ    sz,(120<<16)    @ size of cube
  13.  
  14. _start:     bl hardware_init    @ initialise the hardware
  15.  
  16.         ldr r8,=0x07800000  @ frame buffer 0
  17.         ldr r9,=0x07900000  @ frame buffer 1
  18.         adr r10,cube0       @ pointer to cube data
  19.         adr r11,cube1       @ cube data for fb1
  20.         ldr r12,=0x7fff     @ colour
  21.  
  22. redo:       mov r0,r8       @ fb0
  23.         mov r1,r10      @ cube0
  24.         mov r2,r12      @ colour
  25.         bl cube         @ draw cube0 in fb
  26.        
  27.         bl fb_setbuffer     @ flip the buffer
  28.  
  29.         mov r0,r9       @ fb1
  30.         mov r1,r11      @ cube1
  31.         mov r2,#0       @ black
  32.         bl cube                 @ undraw cube in fb1
  33.  
  34.         mov r0,r10      @ update cube0
  35.         mov r1,r11      @ and put in cube1
  36.         bl movecube
  37.  
  38.         mov r0,r9       @ fb1
  39.         mov r1,r11      @ cube1
  40.         mov r2,r12      @ colour
  41.         bl cube         @ draw cube1 in fb1
  42.  
  43.         bl fb_setbuffer     @ flip the buffer to fb1
  44.        
  45.         mov r0,r8       @ fb
  46.         mov r1,r10      @ cube0
  47.         mov r2,#0       @ black
  48.         bl cube                 @ undraw cube in fb
  49.  
  50.         mov r0,r11      @ update cube1
  51.         mov r1,r10      @ and put in cube0
  52.         bl movecube
  53.  
  54.         b redo          @ do again
  55.  
  56.  
  57. movecube:   stmfd sp!,{r0-r8,lr}    @ move the cube
  58.         mov r2,r0
  59.         mov r5,#8       @ 8 points
  60. moveloop:   ldr r3,[r2]     @ load x,y and z coords
  61.         ldr r4,[r2,#4]
  62.         ldr r8,[r2,#8]
  63.         add r6,r3,r4,asr#12 @ x=x+y/4096
  64.         sub r7,r4,r3,asr#12 @ y=y-x/4096
  65.         add r7,r7,r8,asr#13 @ y=y+x/8192
  66.         sub r8,r8,r4,asr#13 @ z=z-y/8192
  67.         add r6,r6,r8,asr#14     @ x=x+z/16384
  68.         sub r8,r8,r3,asr#14     @ z=z-x/16384
  69.         str r6,[r1]     @ store x,y and z coords
  70.         str r7,[r1,#4]
  71.         str r8,[r1,#8]
  72.         add r2,r2,#12       @ move to next
  73.         add r1,r1,#12       @ point
  74.         subs r5,r5,#1
  75.         bne moveloop
  76.         ldmfd sp!,{r0-r8,pc}         
  77.        
  78. @ draw a cube in fb given by r0
  79. @ with 8 points pointed to by r1 and colour in r2
  80.  
  81. cube:       stmfd sp!,{r0-r12,lr}
  82.         mov r8,r1       @ save pointer
  83.         mov r12,r2      @ save colour
  84.         mov r9,#0       @ 8 points
  85. cloop1:     add r10,r9,#1       @ look at other points
  86. cloop2:     eors r5,r9,r10      @ find bit difference
  87.         beq noline      @ don't draw if 0
  88.         sub r1,r5,#1        @ clear single bit
  89.         ands r5,r5,r1       @ using v = v & (v-1)
  90.         bne noline      @ don't draw if more than one bit set
  91.  
  92.         add r11,r8,r9,lsl#3 @ get address of start coords
  93.         add r11,r11,r9,lsl#2    @ by multiplying the index by 12
  94.         ldrsh r1,[r11,#2]   @ get top 16 bits of the x coord
  95.         ldrsh r2,[r11,#6]   @ get top 16 bits of the y coord
  96.         ldrsh r3,[r11,#10]  @ get top 16 bits of the z coord
  97.         mul r6,r3,r1        @ adjust perspective
  98.         add r1,r6,asr#9     @ x=x+(z*x/512)
  99.         mul r6,r3,r2
  100.         add r2,r6,asr#9     @ y=y+(z*y/512)
  101.  
  102.         add r11,r8,r10,lsl#3    @ get address of end coords
  103.         add r11,r11,r10,lsl#2   @ by multiplying the index by 12
  104.         ldrsh r3,[r11,#2]   @ get top 16 bits of the x coord
  105.         ldrsh r4,[r11,#6]   @ get top 16 bits of the y coord
  106.         ldrsh r5,[r11,#10]  @ get top 16 bits of the z coord
  107.         mul r6,r5,r3        @ adjust perspective
  108.         add r3,r6,asr#9     @ x=x+(z*x/512)
  109.         mul r6,r5,r4
  110.         add r4,r6,asr#9     @ y=y+(z*y/512)
  111.        
  112.         mov r5,r12      @ set colour
  113.         add r1,r1,#cx       @ move to centre
  114.         add r2,r2,#cy
  115.         add r3,r3,#cx
  116.         add r4,r4,#cy
  117.         bl line         @ draw line
  118. noline:     add r10,r10,#1      @ increment end point index
  119.         cmp r10,#8      @ while != 8
  120.         bne cloop2
  121.         add r9,r9,#1        @ increment start point index
  122.         cmp r9,#7       @ while != 7
  123.         bne cloop1
  124.         ldmfd sp!,{r0-r12,pc}
  125.  
  126. cube0:      .word -sz,-sz,-sz   @ cube starting position
  127.         .word -sz,-sz,sz
  128.         .word -sz,+sz,-sz
  129.         .word -sz,+sz,sz
  130.         .word +sz,-sz,-sz
  131.         .word +sz,-sz,sz
  132.         .word +sz,sz,-sz
  133.         .word +sz,sz,sz
  134. cube1:      .space 8*12     @ space for other cube
  135.  
  136.  
  137. @ draw line in fb at r0 from (r1,r2) to (r3,r4) with colour r5
  138. @ using: r6 = steep, r7 = abs(y1-y0) & deltay, r8 = abs(x1-x0) & deltax,
  139. @    r9 = x,r10 = temp,r11 = ystep,r12 = y
  140.  
  141. line:       stmfd sp!,{r6-r12,lr}   @ Save all registers
  142.         subs r7,r4,r2       @ y1-y0
  143.         rsbmi r7,r7,#0      @ makes it absolute value
  144.         subs r8,r3,r1       @ x1-x0
  145.         rsbmi r8,r8,#0      @ make it absolute value if negative
  146.        
  147.         cmp r7, r8      @ compare the absolutes
  148.         movgt r6,#1     @ if greater than then make steep 1
  149.         movle r6,#0     @ if less than make steep 0
  150.  
  151.         cmp r6,#1       @ Check if the line is steep
  152.         bne lif         @ if not equal to steep, branch to if
  153.         mov r10, r1         @ if steep swap value x0 with y0| move x0 to temp
  154.         mov r1, r2      @   | move y0 to x0
  155.         mov r2, r10     @   | move temp to y0  
  156.         mov r10, r3         @ and x1 with y1| move temp to x1
  157.         mov r3, r4      @   | move y1 to x1
  158.         mov r4, r10     @   | move temp to y1
  159.  
  160. lif:        cmp r1, r3      @ check if x0 > x1
  161.         blt ljump       @ if not skip if
  162.         mov r10, r1         @ swap x0 with x1| move x0 to temp
  163.         mov r1, r3      @   | move x1 to x0
  164.         mov r3, r10     @   | move temp to x1
  165.         mov r10, r2         @ swap y0 with y1| move y0 to temp
  166.         mov r2, r4      @   | move y1 to y0
  167.         mov r4, r10     @   | move temp to y1
  168.  
  169. ljump:      sub r8, r3, r1      @ r8=deltax| deltax = x1 - x0
  170.         subs r7,r4,r2       @ r7=deltay
  171.         rsbmi r7,r7,#0      @ deltay becomes absolute value if negative
  172.         movs r10,r8,asr #1  @ r10=error| error = deltax / 2
  173.         mov r12, r2         @ r12 = y| y = y0
  174.         cmp r2, r4      @ compare y0 with y1
  175.         movlt r11, #1       @ otherwise ystep is 1
  176.         movge r11, #-1      @ r11=ystep| if y0 greater or equal to y1. ystep is -1
  177.  
  178.         mov r9, r1      @ r9 = x| x = x0
  179.         b for           @ branch to for
  180.        
  181. lineloop:   bl plotxy       @ subroutine call to plotxy
  182.         subs r10,r10,r7     @ error = error-deltay
  183.         addmi r12,r12,r11   @ if error is minus, y=y+ystep
  184.         addmi r10, r10,r8   @ if error is minus, error=error+deltax
  185.         add r9, r9,#1       @ x++
  186.  
  187. for:        cmp r9, r3      @ compare x with x1
  188.         ble lineloop        @ lineloop if less than or equal
  189.         ldmfd sp!,{r6-r12,pc}   @ load all registers
  190.  
  191. plotxy:     stmfd sp!, {r0, lr} @ save r0 and lr
  192.         cmp r6, #0      @ compare steep with 0
  193.         beq plotyx      @ if steep = 0, branch to plotyx
  194.         add r0, r0, r9, lsl#10  @ r0=fb+(x*1280)+(y*2);
  195.         add r0, r0, r9, lsl#8   @ ..
  196.         add r0, r0, r12, lsl#1  @ ..
  197.         b pixel         @ branch to pixel
  198.  
  199. plotyx:     add r0, r0, r12, lsl#10 @ r0=fb+(y*1280)+(x*2);
  200.         add r0, r0, r12, lsl#8  @ ..
  201.         add r0, r0, r9, lsl#1   @ ..
  202.  
  203. pixel:      strh r5, [r0]       @ store pixel
  204.         ldmfd sp!,{r0,pc}   @ load registers r0 and pc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement