i. Tonc v1.4.2 : Table of Contents
Transcription
i. Tonc v1.4.2 : Table of Contents
28-03-13 Tonc : GBA Programming in rot13 i. Tonc v1.4.2 : Table of Contents i.1. Preface i Co n t e n t s i i I n t r o d u c t i o nt oTo n c 1 3 i.2. GBA Basics 1 2 3 4 5 6 7 8 9 GBAHa r d wa r e Se t t i n gu pad e v e l o p me n te n v i r o n me n t MyFi r s tGBADe mo Vi d e oI n t r o d u c t i o n Th eb i t ma pmo d e s Th eGBAk e y p a d Sp r i t ea n dt i l e db a c k g r o u n do v e r v i e w Re g u l a rs p r i t e s Re g u l a rt i l e db a c k g r o u n d s 7 10 21 34 38 56 62 67 78 i.3. GBA Extended 1 0 Th ea f f i n et r a n s f o r ma t i o nma t r i x 1 1 Af f i n es p r i t e s 1 2 Af f i n et i l e db a c k g r o u n d s 1 3 Gr a p h i ce f f e c t s 1 4 Di r e c tMe mo r yAc c e s s 1 5 Ti me r s 1 6 Ha r d wa r ei n t e r r u p t s 1 7 BI OSc a l l s 1 8 Be e p !GBAs o u n di n t r o d u c t i o n 92 98 109 115 126 133 136 147 155 i.4. Advanced / Applications 1 9 Te x ts y s t e ms 2 0 Mo d e7 2 1 Mo r eMo d e 7t r i c k s 2 2 To n c ' sTe x tEn g i n e 2 3 Wh i r l wi n dt o u ro fARM a s s e mb l y 2 4 Th eLa b 165 192 209 213 244 276 i.5. Appendixes A B C D E F G Nu mb e r s , b i t sa n db i to p e r a t i o n s Fi x e d p o i n tma t h& LUTs Ve c t o ra n dma t r i xma t h Mo r eo nma k e f i l e sa n dc o mp i l e ro p t i o n s Ma k ev i ae d i t o r s Re f e r e n c e s Ch a n g el o g 283 294 304 311 316 322 325 i.6. On revisions To n cv 1 . 4i sf i n a l .Ye a h , Is a i dt h a ta b o u tv 1 . 0a swe l l , b u tt h i st i meIme a ni t .Re a l l y .Ho n e s t .Cr o s smyh e a r ta n dh o p et od i e , e t c , e t c . We l l … ba r r i n gmi n o re r r a t a , t h i swi l l b ef i n a l . Ho n e s t , c r o s smyh e a r t ,ya d d aya d d aya d d a . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 1/331 28-03-13 file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm Tonc : GBA Programming in rot13 2/331 28-03-13 Tonc : GBA Programming in rot13 ii. Introduction to Tonc Or g a n i s a t i o n Te r mi n o l o g ya n dNo t a t i o n Pr e r e q u i s i t e s One r r o r s , s u g g e s t i o n s ii.1. Organisation TONCc o n s i s t so ft h r e ep a r t s : atext s e c t i o n , t h ea c t u a l t u t o r i a l , acode s e c t i o n , wh i c hc o n t a i n sa l l t h es o u r c e c o d ea n dma k e f i l e so ft h ev a r i o u sd e mo s , a n dabin s e c t i o nt h a tc o n t a i n sb i n a r i e so ft h ed e mo .Th o u g ht h e ya r e s e p a r a t ez i pf i l e s , t h e ywo r kb e s twh e nu s e dt o g e t h e r .Yo uc a nf i n dt h ez i pf i l e sf u r t h e rd o wnt h i sp a g e .I fy o u u n z i pt h e mi no n ed i r e c t o r y , s a ytonc, y o u ' l l g e tt h es t r u c t u r ed e p i c t e di nf i gi i . 1 . ii.1.1. Tonc text Th et e x ts e c t i o nc o v e r st h ep r i n c i p l e so fGBAp r o g r a mmi n gi nd e t a i l .Th ef o c u sh e r ei sn o ts omu c ho nh o wt o g e ts o me t h i n gd o n e , b u th o wt h i n g sa c t u a l l ywork, a n dwh yi t ' sd o n et h ewa yi t ' sd o n e .Af t e rt h a tt h eh o wo f t e n c o me sn a t u r a l l y .Ev e r yc h a p t e rh a so n eo fmo r ed e mo n s t r a t i o n so ft h ec o v e r e dt h e o r y , a n dab r i e fd i s c u s s i o no f t h ed e moi t s e l f .Pl e a s e , d on o tma k et h emi s t a k eo fo n l yr e a d i n gt h ed e mod i s c u s s i o n : t op r o p e r l yu n d e r s t a n d h o wt h i n g swo r ky o un e e dt or e a dt h et e x ti nf u l l .Wh i l et h e r ea r eo p t i o n a l p a r t s , a n dwh o l ep a g e so fb o r i n gt e x t t h a ts e e mt oh a v el i t t l et od owi t ha c t u a l GBAc o d i n g , t h e ya r et h e r ef o rar e a s o n , u s u a l l yt h e r e ' se x t r a Fig ii.1: di r e c t or i e s . c o n c e p t u a l i n f o r ma t i o no rg o t c h a s . Atf i r s t , t h et e x tp a r th a do n l yv e r yl i t t l ec o d ei ni t , b e c a u s eIf i g u r e dt h ed e moc o d ewo u l db ea th a n da n df l i c k i n gb e t we e nt h e m wo u l dn o tb ea n n o y i n g .We l l , I ' v er e a l i z e dt h a tIf i g u r e dwr o n ga n da mi nt h ep r o c e s so fi n c l u d i n gmo r eo ft h ec o d ei n t ot h e s ep a g e s ; ma y b en o tq u i t ee n o u g ht oc o p y p a s t ea n dg e tac l e a nc o mp i l e , b u te n o u g ht og owi t ht h ee x p l a n a t i o n so ft h ed e mo s . Th ema i nl a n g u a g ewi l l b eC, a n das mi d g e o no fa s s e mb l y .Th e s ea r et h et woma i nl a n g u a g e su s e di nGBAp r o g r a mmi n ge v e n t h o u g ht h e r ea r eo t h e r sa r o u n d .Si n c et h eb a s i c so fp r o g r a mmi n ga r ei n d e p e n d e n to fl a n g u a g e , i ts h o u l db ep o s s i b l et oa d a p tt h e mf o r y o u rc h o s e nl a n g u a g ee a s i l y . GBAp r o g r a mmi n gi sd o n ec l o s et ot h eh a r d wa r e , s oIh o p ey o uk n o wy o u rp o i n t e r s , h e x a d e c i ma l n u mb e r sa n db o o l e a na l g e b r a / b i t o p e r a t i o n s .Th e r e ' sa l s oaf a i ra mo u n to fma t hh e r e , mo s t l yv e c t o ra n dma t r i xs t u f fs oIh o p ey o u rl i n e a ra l g e b r ai su pt os p e e d .La s t l y , I a ma s s u mi n gy o u ri n t e l l e c t u a l c a p a c i t i e se x c e e dt h o s eo far a n d o ml a bmo n k e y , s oIwo n ' te l a b o r a t eo nwh a tIc o n s i d e rt r i v i a l ma t t e r s t o omu c h . As i d ef r o mt h ei n t r o d u c t i o na n da p p e n d i c e s , t h et e x ti sd i v i d e di n t o3p a r t s .Fi r s tt h e r e ' s‘ b a s i c s ’ , wh i c he x p l a i n st h ea b s o l u t e e s s e n t i a l sf o rg e t t i n ga n y t h i n gd o n e .Th i si n c l u d e ss e t t i n gu pt h ed e v e l o p me n te n v i r o n me n t , b a s i cu s eo fg r a p h i c sa n db u t t o n s .I ta l s o c o n t a i n st e x to nwh a ti tme a n st od ol o wl e v e l p r o g r a mmi n ga n dp r o g r a mmi n ge f f i c i e n t l y ;i t e mst h a ti nmyv i e wy o u ' db e t t e rl e a r ns o o n e r r a t h e rt h a nl a t e r .Th es e c o n dp a r tc o v e r smo s to ft h eo t h e ri t e mso ft h eGBAl i k es p e c i a l g r a p h i ce f f e c t s , t i me r sa n di n t e r r u p t s .Th ef i n a l s e c t i o nc o v e r smo r ea d v a n c e di t e mst h a tu s e se l e me n t sf r o ma l l c h a p t e r s . Th i si n c l u d e swr i t i n gt e x t( y e s , t h a t ' sa na d v a n c e dt o p i co nt h e GBA) , mo d e7g r a p h i c sa n dac h a p t e ro nARM a s s e mb l y . I n d i v i d u a l h t ml : t o n c t e x t . z i p( 6 6 3k b ) Co mp i l e dh t ml ( CHM, v 1 . 4v e r s i o n ) : t o n c . c h m( 1 . 2MB) . PDF: t o n c . p d f( 3 . 1MB) ii.1.2. Tonc code Th es o u r c ec o d et oa l l t h ed e mo sme n t i o n e di nt h et e x tc a nb ef o u n di nt h ecoded i r e c t o r y .Li k et h et e x t , t h ec o d ei sd i v i d e di n t o3ma i n p a r t s : b a s i c , e x t e n d e da n da d v a n c e d .Th eb a s i cp r o j e c t sa r e( h o p e f u l l y )p r e t t yn e wb i ef r i e n d l y .Th e y ' r ec o mp l e t e l ys e l f c o n t a i n e ds oy o u c a nt o ywi t ht h e ma n dn o two r r ya b o u ts c r e wi n gu po t h e rp a r t s .Th ee x t e n d e dp r o j e c t st a k et h e i rGBAr e l a t e dc o d ef r o mtonclib, a l i b r a r ywi t ha l l my#d e f i n e sa n di mp o r t a n tf u n c t i o n s .Th ea d v a n c e dd e mo sa l s ou s etonclibl i b e r a l l ya n dwi l l u s es o mea s s e mb l yf i l e s , e v e ni fi ti sj u s tf o rd a t a .Th e r ei sa l s oalabd i r e c t o r ywi t haf e wi n t e r e s t i n gp r o j e c t s , b u twh i c hmi g h tn o tb eq u i t er e a d y .St i l l i n t e r e s t i n gt ol o o ka t , t h o u g h . Un l i k emo s tGBAt u t o r i a l s , t o n cu s e smakefiles r a t h e rt h a nb a t c h f i l e st ob u i l dt h ep r o j e c t s , b e c a u s et h e y ' r ej u s tPl a i nBe t t e rt h a n b a t c h f i l e s .Ho we v e r , a sal o to fy o uwi l l p r o b a b l yn e v e rs e e nt h e mb e f o r e , t h e s ea l s oc o mei nt h r e es t a g e so fd i f f i c u l t y .Th e r ei sa ma s t e rma k e f i l e , tonc.mak, i nt h ec o d er o o td i r e c t o r yt h a tc a nd r i v et h ep r o j e c t s , a n dt h evc6d i r e c t o r yh a saVi s u a l C++p r o j e c tt h a t y o uc a nu s ei fy o u ' r eo nWi n d o wsa n dh a v eVi s u a l St u d i ov e r s i o n . Pr e t t ymu c ha l l o ft h eg e n e r a l f u n c t i o n su s e di nt o n cc a nb ef o u n di ntonclib.Th i si n c l u d e st e x twr i t e r sf o ra l l mo d e s , BI OS r o u t i n e s , ap r e t t ya d v a n c e di n t e r r u p td i s p a t c h e r , s a f ea n df a s tme mo r yc o p ya n df i l l r o u t i n e sa n dmu c hmo r e .I t ' swe l l wo r t hs t e a l i n g f r o m. Do wn l o a dt o n c c o d e . z i p( 1 9 8k b ) . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 3/331 28-03-13 Tonc : GBA Programming in rot13 ii.1.3. Tonc binaries Th ebind i r e c t o r yc o n t a i n st h eb i n a r i e so ft h ed e mo s .Ea c ho ft h e mh a sb e e nt e s t e do nan u mb e ro fe mu l a t o r s , a n do nh a r d wa r eu s i n ga h o me ma d eXb o oc o mmu n i c a t i o nc a b l e( s e ewww. d e v k i t p r o . o r gf o ri n s t r u c t i o n so nh o wt oma k eo n e ) .I nmo s ti n s t a n c e st h e yb e h a v e e x a c t l yt h es a me .Th er e a d metonc_bins.txti n d i c a t e swh e na n dwh e r eh a r d wa r ea n de mu l a t o r sd i s a g r e e . Do wn l o a dt o n c b i n . z i p( 1 6 7k b ) . ii.1.4. Statement of Purpose Iwr o t eTo n cf o rt wor e a s o n s .Fi r s t l y , a sawa yt oo r g a n i z emyo wnt h o u g h t s .Yo uo f t e ns e et h i n g si nad i f f e r e n tl i g h twh e ny o uwr i t e t h i n g sd o wna n dl e a r nf r o mt h a te x p e r i e n c e .Se c o n d l y , t h e r ei sal o to fvery bad i n f o r ma t i o ni no t h e rt u t o r i a l so u tt h e r e( t h eo n l y e x c e p t i o n sIk n o wo fa r et h en e wPERNa n dDe k u ' ss o u n dt u t o r i a l [b0rked]) .Ye s , Ia ma wa r eo fh o wt h a ts o u n d s , b u tu n f o r t u n a t e l yi t h a p p e n st ob et r u e .An u mb e ro fe x a mp l e s : On l yv e r yb a s i ci n f o r ma t i o ng i v e n , s o me t i me se v e ni n c o r r e c ti n f o . St r o n gf o c u so nb i t ma pmo d e s , wh i c ha r eh a r d l ye v e ru s e df o rs e r i o u sGBAp r o g r a mmi n g . Ba dp r o g r a mmi n gh a b i t s .Ad d i n gc o d e / d a t at op r o j e c t sb y#i n c l u d i n gt h ef i l e s , Us i n ga n c i e n tt o o l c h a i n s , n o n o p t i ma l c o mp i l e r s e t t i n g sa n dd a t a t y p e s , a n di n e f f i c i e n t( s o me t i me svery i n e f f i c i e n t )c o d e . I fy o ua r en e wa n dh a v ef o l l o we dt h eo t h e rt u t o r i a l s , e v e r y t h i n gwi l l s e e mt owo r kf i n e , s owh a t ' st h ep r o b l e m?We l l , t h a t ' sp a r to ft h e p r o b l e ma c t u a l l y .Ev e r y t h i n gwi l l seem f i n e , u n t i l y o us t a r tb i g g e rp r o j e c t s , a twh i c ht i mey o u ' l l f i n dh i d d e ne r r o r sa n dt h a ts l o wc o d e r e a l l yb o g st h i n g sd o wna n dy o u ' l l h a v eu n l e a r na l l t h eb a dh a b i t sy o up i c k e du pa n dr e d oe v e r y t h i n gf r o mt h es t a r t .Th eGBAi so n eo f t h ef e wp l a t f o r mswh e r ee f f i c i e n tc o d i n gs t i l l me a n ss o me t h i n g , a n ds o me t i me sa l l i tt a k e si sac h a n g eo fd a t a t y p eo rc o mp i l e rs wi t c h . Th e s et h i n g sa r eb e t t e rd o n er i g h tf r o mt h es t a r t . I ' v et r i e dt og of o rc o mp l e t e n e s sf i r s t , s i mp l i c i t ys e c o n d .Asac e r t a i nwi l d h a i r e ds c i e n t i s to n c es a i d : “ Ma k et h i n g sa ss i mp l ea sp o s s i b l e , b u tn os i mp l e r . ”Th i sme a n st h i n g sc a ns e e mal i t t l et e c h n i c a l a tt i me s , b u tt h a t ' so n l yb e c a u s et h i n g sare p r e t t yt e c h n i c a l a tt i me s , a n d t h e r e ' sn os e n s ei np r e t e n d i n gt h e y ' r en o t . I ns h o r t , To n ci snot “ GBAPr o g r a mmi n gf o rDu mmi e s ” , n e v e rwa s , n e v e rwi l l b e .Th e r e ' sf a rt o omu c ho fs t u f ff o rDu mmi e s a l r e a d ya n y wa y .I fy o uc o n s i d e ry o u r s e l fad u mmy( a n dId ome a nd u mmy , n o tn e wb i e ) , ma y b eTo n ci s n ' tt h er i g h tp l a c e .I fy o u ' r e s e r i o u sa b o u tl e a r n i n gGBAp r o g r a mmi n g , h o we v e r , a c c e p tn os u b s t i t u t e . ii.2. Terminology and Notation I ' map h y s i c i s tb yt r a i n i n gwh i c hme a n st h a tIk n o wmyma t ha n di t sn o t a t i o n a l c o n v e n t i o n s .Iu s eb o t hq u i t eo f t e ni nTo n c , a swe l l a sa n u mb e ro fh t ml t a gc o n v e n t i o n s .Toma k es u r ewe ' r ea l l o nt h es a mep a g eh e r e ' sal i s t : Type notation example b i tni nafoofoo{ n } REG_DISPCNT{4}( a c t i v ep a g eb i t ) c o d e sx <c o d e >t a g c o mma n d / f i l e<t t >t a g vid.h ma t r i x b o l d , u p p e r c a s eP me mo r y h e x+c o d e 0400:002eh n e wt e r m b o l d , i t a l i c charblock v a r i a b l e i t a l i c s x v e c t o r b o l d , l o we r c a s ev Ia l s ou s es o men o n ASCI Is y mb o l st h a tma yn o ts h o wu pp r o p e r l yd e p e n d i n go nh o wo l dy o u rb r o ws e ri s .Th e s ea r e : symbol description α, β, γ Gr e e kl e t t e r s ≈ a p p r o x i ma t e l y ½ o n eh a l f ¼ o n eq u a r t e r ¾ t h r e eq u a r t e r s ≥ g r e a t e ro re q u a l ↔ d o u b l e s i d e da r r o w ∈ i si n( a ni n t e r v a l ) ‘ b r a ’& ‘ k e t ’ → r i g h ta r r o w ² s u p e r s c r i p t2 file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 4/331 28-03-13 Tonc : GBA Programming in rot13 × t i me s Ia l s oma k el i b e r a l u s eo fs h o r t h a n df o rp r i mi t i v eCt y p e sl i k echara n dinta n ds u c h .Th e s ea r et y p e d e f st h a tb e t t e ri n d i c a t et h es i z e o ft h ev a r i a b l et h a t ' su s e d .Si n c et h i si sv e r yi mp o r t a n ti nc o n s o l ep r o g r a mmi n g , t h e y ' r eq u i t ec o mmo n .An y wa y , h e r e ' sal i s t . base type alt name unsigned signed char b y t e short int u 8 volatile s 8 v u 8/v s 8 h a l f wo r du 1 6 s 1 6 v u 1 6/v s 1 6 wo r d s 3 2 v u 3 2/v s 3 2 u 3 2 Fi n a l l y , t h e r ea r ean u mb e ro fd i f f e r e n tn o t a t i o n sf o rh e xt h a tIwi l l s wi t c hb e t we e n , d e p e n d i n go nt h es i t u a t i o n .Th eCn o t a t i o n( ‘ 0 x ’ p r e f i x , 0 x 0 4 0 0 )i sc o mmo nf o rn o r ma l n u mb e r s , b u tI ' l l a l s ou s et h ea s s e mb l ya f f i xa tt i me s( ‘ h ’ , 0 4 0 0 : 0 0 0 0 h ) .Th ec o l o nh e r ei sme r e l y f o re a s eo fr e a d i n g .I t ' sh a r dt ot e l l t h en u mb e ro fz e r o swi t h o u ti t . ii.2.1. Register names and descriptions Ge t t i n gt h eGBAt od ot h i n g so f t e ni n v o l v e st h eu s eo ft h es o c a l l e dIO registers.Ce r t a i nb i t sa tc e r t a i na d d r e s s e so fme mo r yc a nb e u s e da ss wi t c h e sf o rt h ev a r i o u se f f e c t st h a tt h eGBAi sc a p a b l eo f .Ea c hr e g i s t e ri sa l i a s e da san o r ma l v a r i a b l e , a n dy o un e e dt o s e t / c l e a rb i t su s i n gb i to p e r a t i o n s .We ' l l g e tt owh e r et h e s er e g i s t e r sa r ea n dwh a tb i td o e swh a tl a t e r ;r i g h tn o wIwa n tt os h o wy o uh o w Iwi l l present t h e s e , a n dr e f e rt ot h e mi nt h et e x t . Ea c hr e g i s t e r( o rr e g i s t e r l i k ea d d r e s s )i sma p p e dt oad e r e f e r e n c e dp o i n t e r , u s u a l l y1 6 b i t sl o n g .Fo re x a mp l e , t h ed i s p l a ys t a t u s r e g i s t e ri s #define REG_DISPSTAT *(u16*)0x04000004 Ev e r yt i meIi n t r o d u c ear e g i s t e rIwi l l g i v ea no v e r v i e wo ft h eb i t sl i k et h i s : REG_DISPSTAT @ 0400:0004h FEDCBA98 76 VcT 5 4 3 2 1 0 - VcI HbI VbI VcS HbS VbS Th et a b l el i s t st h er e g i s t e r ' sn a me( REG_DISPSTAT, i t sa d d r e s s( 0 4 0 0 : 0 0 0 0 h )a n dt h ei n d i v i d u a l b i t so rb i t f i e l d s .So me t i me s , b i t so r e n t i r er e g i s t e r sa r er e a d -o rwr i t e o n l y .Read-only i si n d i c a t e dwi t har e do v e r b a r( a su s e dh e r e ) .Write-only u s e sab l u eu n d e r b a r . Af t e ri twi l l b eal i s tt h a td e s c r i b e st h ev a r i o u sb i t s , a n da l s og i v e st h e#d e f i n eo r#d e f i n e sIu s ef o rt h a tb i t : bits name define description 0 Vb S DSTAT_I N_VBLVBl a n ks t a t u s , r e a do n l y .Wi l l b es e ti n s i d eVBl a n k , c l e a ri nVDr a w. 8 F Vc T DSTAT_VCT# other fields VCo u n tt r i g g e rv a l u e .I ft h ec u r r e n ts c a n l i n ei sa tt h i sv a l u e , b i t2i ss e ta n da n i n t e r r u p ti sf i r e di fr e q u e s t e d . Th ef u l l l i s to fREG_DI SPSTATc a nb ef o u n dh e r e .Th e#d e f i n e sa r eu s u a l l ys p e c i f i ct ot o n c , b yt h ewa y .Ea c hs i t ea n dAPIh a si t s o wnt e r mi n o l o g yh e r e .Th i si sp o s s i b l eb e c a u s ei t ' sn o tt h en a me st h a ta r ei mp o r t a n t , b u tt h en u mb e r st h e ys t a n df o r .Th a tg o e sf o rt h e n a me so ft h er e g i s t e r st h e ms e l v e st o o , o fc o u r s e .On el a s tp o i n to nt h e#d e f i n e s : s o meo ft h eo n e sl i s t e dh a v eah a s h( ‘ #’ )a f f i x .Th i si s as h o r t h a n dn o t a t i o nt oi n d i c a t et h a tt h a tf i e l dh a sfoo_SHIFTa n dfoo_MASK#d e f i n e s , a n dafoo()ma c r o .Fo re x a mp l e , t h ed i s p l a y r e g i s t e rh a sa n8 b i tt r i g g e rVCo u n tf i e l d , wh i c hh a s‘ DSTAT_VCT#’l i s t e di nt h ed e f i n ec o l u mn .Th i sme a n st h a tt h ef o l l o wi n gt h r e e t h i n g se x i s ti nt h et o n ch e a d e r s : #define DSTAT_VCT_MASK #define DSTAT_VCT_SHIFT #define DSTAT_VCT(_n) 0xFF00 8 ((_n)<<DSTAT_VCT_SHIFT) La s t l y , a ss h o r t h a n df o ras p e c i f i cb i ti nar e g i s t e r , Iwi l l u s ea c c o l a d e s .Th en u mb e rwi l l b eah e x a d e c i ma l n u mb e r .Fo re x a mp l e , REG_DI SPCNT{ 0 }i st h eVBl a n ks t a t u sb i t( Vb Sa b o v e ) , a n dREG_DI SPCNT{ 8 F}wo u l db et h ewh o l eb y t ef o rt h eVCo u n tt r i g g e r . ii.3. Prerequisites Th et e x ta n dc o d eh a v eb e e nc r e a t e da n df o u n dt owo r ku n d e rt h ef o l l o wi n gc o n d i t i o n s .I fy o uf i n dy o uh a v eap r o b l e m, s h o wmey o u r s a n dma y b ewec a nf i n da n df i xi t . Programming language.Th el a n g u a g ewe ' l l b eu s i n gi sC wi t had a s ho fa s s e mb l y( b u tnot C++) .Ia mwo r k i n gu n d e rt h e a s s u mp t i o nt h a ty o ua r ef a mi l i a rwi t ht h i sl a n g u a g e .I fn o t , g ol e a r ni tf i r s tb e c a u s eI ' mn o tg o i n gt os h o wy o u ;t h i si sn o taC c o u r s e .Id oh a v es o mel i n k st oCt u t o r i a l si nt h er e f e r e n c e s . Compiler.Ob v i o u s l y , y o un e e dac o mp i l e rt h a tc a nt u r nCc o d ei n t oaGBAb i n a r y .I ' mu s i n gt h ec o mp l e t e l yfree d e v k i t ARM ( d k ARM)t o o l c h a i n , wh i c hi sb a s e do nt h eGNUc o mp i l e rc o l l e c t i o n( GCC) .Se t u pi n s t r u c t i o n sa n ds a mp l e sc a nb ef o u n do n t h es i t e , a n di nmys e t u ps e c t i o n , o fc o u r s e .d k ARM h a sb e c o met h es t a n d a r do v e rt h ep a s ty e a ro rs o ;i fy o ua r es t i l l u s i n gt h e o l d e rDe v Ki tAd v a n c e , y o umi g h tt h i n ka b o u ts wi t c h i n g .Ic a n ' ts a yh o wd i f f i c u l ti two u l db et og e tHAMb0rked t oa c c e p tmy file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 5/331 28-03-13 Tonc : GBA Programming in rot13 c o d e / ma k e f i l e s , b u ti ts h o u l d n ' tb et o oh a r d .I fy o uu s et h eo f f i c i a l Ar mADS, s o r r yb u ty o u ' r eo ny o u ro wnt h e r e . Towr i t ea n dma n a g emyc o d eIu s eVi s u a l C++6 . 0 , b u tIa mn o tu s i n gi t sn a t i v ema k et o o l ( NMAKE) , b e c a u s ei t ' sn o tv e r y p o r t a b l ea n ds e e mst ob ep r e t t ywe a ka swe l l .I n s t e a d , Iu s eGNU make, wh i c hu s u a l l yc o me swi t ha n yo ft h ed e v k i t s . Binaries.Yo u ' l l a l s on e e ds o me t h i n gt h a tc a nr u nt h eb i n a r i e s .Ont h ee mus i d e , Iu s ee i t h e rVi s u a l Bo yAd v a n c e( VBA)v 1 . 7 o rh i g h e r , a n dn o $ g b a , f r e e wa r ee d i t i o n .Bu ta sn i c ea st h e s ee mu l a t o r sa r e , y o umust b ea b l et ot e s to nar e a l GBAt o oi f y o u ' r es e r i o u sa b o u tt h i ss t u f f .Ge ty o u r s e l faf l a s hc a r do r , i fy o u ' r eo nab u d g e t , y o uc a na l wa y sb u i l damu l t i b o o tc a b l el i k e Xb o oy o u r s e l f .I fy o us h o pa r o u n dy o us h o u l db ea b l et og e tt h ec o mp o n e n t sf o ra b o u t$ 5 . OS.Iu s et h eWi n d o wso p e r a t i o ns y s t e m( Wi n9 8a n dWi n XP)a n dwh e nr e f e r r i n gt oc o mma n d so rp r o g r a msI ' mmo r eo rl e s s a s s u mi n gy o ua r et o o .Th eGBAa n dd e v k i t sc o u l d n ' tc a r el e s s .Ho we v e r , aLi n u xu s e r , f o re x a mp l e , wo u l dh a v el i t t l eu s ef o r aVi s u a l Cp r o j e c ta n dwo u l d n ' tb ea b l et ou s eUs e n t i ( myb i t ma pe d i t o r )o re x c e l l u t( myEx c e l LUTb u i l d e r ) . Browser.I ' v et e s t e dt h e s ep a g e swi t hMSI E6a n d7 , Fi r e f o x , a n ds o me t i me sOp e r at o o .Wi t ho n eo rt woe x c e p t i o n s , ( <n o b r >) , e v e r y t h i n gi sv a l i dHTML4 . 0 1a n dCSS2 .Ho we v e r , I Ei s n ' tac o mp l i a n tb r o ws e rs ot h e r ea r eo n eo rt womi n o r g l i t c h e s .Fi r e f o xo nt h eo t h e rh a n di stoo c o mp l i a n ta n dp r e t t ymu c hi g n o r e sc o l u mn b a s e dl a y o u ti nt a b l e s( CSSa n dHTML s t a n d a r d sc o n f l i c to nt h i si s s u e ) .Bu ta g a i n , t h e s es i t u a t i o n sa r e n ' tt o os i g n i f i c a n t .I nc a s ey o uwa n tap r i n t o u t , i t ' l l c o meo u ta s i ft h eb r o ws e rwi n d o wwa sr o u g h l y6 4 0p i x e l swi d e .Pe r s o n a l l y , Ip r e f e rs e t t i n gt h ef o n tt o‘ s ma l l e r ’ ;If e e l t h a tIl o s et h e p e r s p e c t i v ei nt h e‘ n o r ma l ’f o n t s i z e .No t et h a tt h ee n t i r et e x ti sr o u g h l y3 0 0p a g e sl o n g , s oc o n s i d e rs h r i n k i n gt h ep a g e sa n d d o n ' tf o r g e tt of i l l t h et r a ywh e ny o u ' r ed o n e . ii.4. On errors, suggestions Asmu c ha sI ' v et r i e dt owe e do u tt h i n g sl i k es p e l l i n g / g r a mma re r r o r sa n db r o k e nl i n k s , I ' ms u r es o meh a v es l i p p e db y .I fy o uf i n ds o me , ma i l mea b o u ti t .Th a t ' sr i g h t , I ' ma c t u a l l ya s k i n gf o rs p e l l f l a me s .Cu r r e n t l y , I ' mr e f i t t i n gt h ep a g e st of i tmyHTMLa u t o n u mb e r i n gt o o l , b u ti t ' sal o ts t u f ft og ot h r o u g h( o v e r1 Mi np l a i nt e x t ) , a n dIma ymi s ss o me t h i n g ;i fy o us e et h i n g sl i k e[[ref:foo]] i nu n f i n i s h e ds e c t i o n s , t h o s ep r o b a b l ys h o u l d n ' tb et h e r e .Ofc o u r s e , i ft h i n g sa r eu n c l e a ro r* g a s p *i n c o r r e c t , o ri fy o uh a v es u g g e s t i o n s , I ' dl i k et ok n o wt h a ta s we l l . 2 0 1 3 0 3 2 4 , ADDENDUM.Si n c ei t ' sn o wb e e no v e rh a l fad e c a d es i n c ea l l o ft h i swa swr i t t e n , I ' mf a i r l yc e r t a i nma n yo ft h el i n k s a r ed e a db yn o w.I ' l l t r yt oc l e a nt h e mu pwh e nIc a n , b u tI ' mn o ts u r ei t ' swo r t ht h et r o u b l e .Yo uh a v eb e e nwa r n e d . An d , o fc o u r s e : Th i sd i s t r i b u t i o ni sp r o v i d e da si s , wi t h o u twa r r a n t yo fa n yk i n d .Ic a n n o tb eh e l dl i a b l ef o ra n yd a ma g ea r i s i n go u to ft h e u s eo ri n a b i l i t yt ou s et h i sd i s t r i b u t i o n .Co d eh a sb e e nt e s t e do ne mu l a t o ra n dr e a l h a r d wa r ea swe l l a sIc o u l d , b u tIc a n ' t g u a r a n t e e1 0 0 %c o r r e c t n e s s . Th ec o d ema yb eu s e da n d / o rmo d i f i e da sy o us e ef i t .Th et e x ta n dc o d ewe r ei n t e n d e dt og ot o g e t h e r ;i fy o uh a v et o s e p a r a t et h et e x tf r o mt h ec o d e , a tl e a s tp r o v i d eal i n kt owh e r eb o t ho r i g i n a t e d( i . e . , t h i ss i t e ) . Bo t ht e x ta n dc o d ec a nb emo d i f i e db ymea ta n yt i me .Ch e c ki no n c ei nawh i l et os e ei fa n y t h i n g ' sc h a n g e d .Ti mes t a mp s a r ea tt h eb o t t o mo fe v e r yp a g e , a n da tt h et o po fa l l s o u r c e f i l e s .Th e r ei sa l s oal o gi nt h ea p p e n d i c e s . OKt h a t ' si t .Ha v ef u n . -J a s p e rVi j n( c e a r na tc o r a n a cd o tc o m) ( Ma r2 4 , 2 0 1 3 ) file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 6/331 28-03-13 Tonc : GBA Programming in rot13 1. GBA Hardware Me e tt h eGBA. GBASp e c sa n dCa p a b i l i t i e s . Me mo r ySe c t i o n s . 1.1. Meet the GBA Th eNi n t e n d oGa me Bo yAd v a n c e( GBA)i sap o r t a b l eg a me sc o n s o l e .Asi fy o ud i d n ' tk n o wa l r e a d y .Th eCPUi sa3 2 b i tARM7 t d mi c h i pr u n n i n ga t1 6 . 7 8MHz .I th a san u mb e ro fd i s t i n c tme mo r ya r e a s( l i k ewo r kRAM, I Oa n dv i d e ome mo r y )wh i c hwewi l l l o o ki n t o s h o r t l y .Th eg a me sa r es t o r e do nGame Paks, c o n s i s t i n go fROM f o rc o d ea n dd a t a , a n df a i r l yo f t e ns o meRAM f o rs a v i n gg a mei n f o . Th eGBAh a sa2 4 0 x 1 6 0LCDs c r e e nc a p a b l eo fd i s p l a y i n g3 2 7 6 8c o l o r s( 1 5b i t s ) .Un f o r t u n a t e l y , t h es c r e e ni sn o tb a c k l i t , wh i c hma d e al o to fp e o p l ev e r ya n g r ya n dh a sg e n e r a l l yb e e nr e g a r d e da sab a dmo v e .So , i n2 0 0 3Ni n t e n d ol a u n c h e dt h eGBASP, as o r to fGBA 2 . 0 , wh i c hf e a t u r e saf o l d u ps c r e e nr e mi n i s c e n to ft h eo l dGa me& Wa t c hg a me s( r e me mb e rt h o s e ?Yo ud o ?Bo y , y o ua r eold!( Fo r t h er e c o r d , Is t i l l h a v emi n et o o:)) ) .Th e ni nc a met h ef i n a l GBAv e r s i o n , t h eGBAmi c r o , av e r y , v e r ys ma l l GBAwh i c he a s i l yf i t si n e v e r y o n e ' sp o c k e t s .Th ed i f f e r e n c e st h eGBA, GBASPa n dMi c r oa r ema i n l yc o s me t i c , t h o u g h , t h e y ' r et h es a met h i n gf r o ma p r o g r a mmi n gp o i n to fv i e w. Th eo r i g i n a l Ga me Bo ywh i c ht o o kt h ewo r l db ys t o r mi n1 9 8 9 .No tb a df o ramo n o c h r o meh a n d h e l dc o n s o l e , e h ?La t e rt h eGa me Bo y Co l o rwa sr e l e a s e dwh i c hf i n a l l yp u ts o mec o l o ro nt h ea g i n gma c h i n e , b u ti twa ss t i l l v e r ymu c has i mp l eGa me Bo y .Th et r u es u c c e s s o r wa st h eGBA, r e l e a s e di n2 0 0 2 .Th eGBAi sb a c k wa r dc o mp a t i b l ewi t ht h eGa me Bo y , s oy o uc a np l a ya l l t h eo l dGBg a me sa swe l l . I nt e r mso fc a p a b i l i t i e st h eGBAi sal o tl i k et h eSu p e rNES( SNES) : 1 5 b i tc o l o r , mu l t i p l eb a c k g r o u n dl a y e r sa n dh a r d wa r er o t a t i o n a n ds c a l i n g .An ds h o u l d e rb u t t o n s , o fc o u r s e .Ac y n i cmi g h tl o o ka tt h ee n o r mo u sa mo u n to fSNESp o r t sa n ds a yt h a tt h eGBAis a SNES, o n l yp o r t a b l e .Th i si st r u e , b u ty o uc a nh a r d l yc a l l t h a tab a dt h i n g . Fig 1.1: o r i g i n a l GBA. Fig 1.2: GBAS P. 1.2. GBA specs and capabilities Be l o wi sal i s to ft h es p e c i f i c a t i o n sa n dc a p a b i l i t i e so ft h eGBA.Th i sn o taf u l l l i s t , b u tt h e s ea r et h emo s ti mp o r t a n tt h i n g sy o un e e dt o k n o w. Vi d e o 2 4 0 x 1 6 0p i x e l , 1 5 b i tc o l o rLCDs c r e e n .Th eo r i g i n a l GBAs c r e e nwa sn o tb a c k l i t , b u tt h eSP' sa n dMi c r o ' sa r e . 3b i t ma pmo d e sa n d3t i l e ma pmo d e sa n ds p r i t e s . 4i n d i v i d u a l t i l e ma pl a y e r s( b a c k g r o u n d s )a n d1 2 8s p r i t e s( o b j e c t s ) . Af f i n et r a n s f o r ma t i o n s( r o t a t e / s c a l e / s h e a r )o n2b a c k g r o u n d sa n d3 2o b j e c t s . Sp e c i a l g r a p h i ce f f e c t s : mo s a i c , a d d i t i v eb l e n d , f a d et owh i t e / b l a c k . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 7/331 28-03-13 Tonc : GBA Programming in rot13 So u n d 6c h a n n e l st o t a l 4t o n eg e n e r a t o r sf r o mt h eo r i g i n a l Ga me Bo y : 2s q u a r ewa v e , 1g e n e r a l wa v ea n do n en o i s eg e n e r a t o r . 2' Di r e c t So u n d ' c h a n n e l sf o rp l a y i n gs a mp l e sa n dmu s i c . Mi s c e l l a n e o u s 1 0b u t t o n s( o rk e y s ) : 4 wa yd i r e c t i o n a l p a d , Se l e c t / St a r t , f i r eb u t t o n sA/ B, s h o u l d e rb u t t o n sL/ R. 1 4h a r d wa r ei n t e r r u p t s . 4 p l a y e rmu l t i p l a y e rmo d ev i aamu l t i b o o tc a b l e . Op t i o n a l i n f r a r e d , s o l a ra n dg y r o s c o p i ci n t e r f a c e s .Ot h e ri n t e r f a c e sh a v ea l s ob e e nma d eb ys o me . Ma i np r o g r a mmi n gp l a t f o r ms : C/ C++a n da s s e mb l y , t h o u g ht h e r ea r et o o l sf o rPa s c a l , Fo r t h , LUAa n do t h e r sa s we l l .Ea s yt os t a r twi t h , y e th a r dt ot r u l yma s t e r . Fr o map r o g r a mmi n gp o i n to fv i e w, t h eGBA( o ra n yo t h e rc o n s o l ef o rt h a tma t t e r )i st o t a l l yd i f f e r e n tf r o maPC.Th e r ei sn oo p e r a t i n g s y s t e m, n ome s s i n gwi t hd r i v e r sa n dh a r d wa r ei n c o mp a t i b i l i t i e s ;i t ' sb i t sa sf a ra st h ee y ec a ns e e .We l l , PCsa r ea l s oj u s tb i t s , b u tt h a t ' s s e v e r a l l a y e r sd o wn ;o nc o n s o l e si t ' sj u s ty o u , t h eCPUa n dme mo r y .Ba s i c a l l y , i t ' st h eRe a l Pr o g r a mme r ' sd r e a m. Tog e ta n y t h i n gd o n e , y o uu s ememory-mapped IO.Sp e c i f i ca r e a so fme mo r ya r ema p p e dd i r e c t l yt oh a r d wa r ef u n c t i o n s .I nt h e f i r s td e mo , f o re x a mp l e , wewi l l wr i t et h en u mb e r0x0403t ome mo r ya d d r e s s0400:0000h.Th i st e l l st h eGBAt oe n a b l e b a c k g r o u n d2a n ds e tt h eg r a p h i c smo d et o3 .Wh a tt h i sa c t u a l l ymeans i s , o fc o u r s e , wh a tt h i st u t o r i a l i sf o r:). 1.2.1. CPU 24c Ass a i d , t h eGBAr u n so naARM7 t d mi RI SCc h i pa t1 6 . 7 8MHz( 2 y c l e s / s e c o n d ) .I ti sa3 2 b i tc h i pt h a tc a nr u no nt wod i f f e r e n t i n s t r u c t i o ns e t s .Fi r s t , t h e r e ' si sARM code, wh i c hi sas e to f3 2 b i ti n s t r u c t i o n s .Th e nt h e r e ' sTHUMB, wh i c hu s e s1 6 b i ti n s t r u c t i o n s . THUMBi n s t r u c t i o n sa r eas u b s e to ft h eARM i n s t r u c t i o ns e t ;s i n c et h ei n s t r u c t i o n sa r es h o r t e r , t h ec o d ec a nb es ma l l e r , b u tt h e i rp o we r i sa l s or e d u c e d .I ti sr e c o mme n d e dt h a tn o r ma l c o d eb eTHUMBc o d ei nROM, a n df o rt i me c r i t i c a l c o d et ob eARM c o d ea n dp u ti n I WRAM.Si n c ea l l t o n c d e mo sa r es t i l l r a t h e rs i mp l e , mo s t( b u tn o ta l l )c o d ei sTHUMBc o d e . Fo rmo r ei n f o r ma t i o no nt h eCPU, g ot owww. a r m. c o mo rt ot h ea s s e mb l yc h a p t e r 1.3. Memory Sections Th i ss e c t i o nl i s t st h ev a r i o u sme mo r ya r e a s .I t ' sb a s i c a l l yas u mma r yo ft h eGBATe ks e c t i o no nme mo r y . end length portdescription size area start System ROM 0000:0000h0000:3FFFh1 6 k b 3 2b i tBI OSme mo r y .Yo uc a ne x e c u t ei t , b u tn o tr e a di t( i . o . w, t o u c h , d o n ' tl o o k ) . Ex t e r n a l wo r kRAM.I sa v a i l a b l ef o ry o u rc o d ea n dd a t a .I fy o u ' r eu s i n ga mu l t i b o o tc a b l e , t h i si swh e r et h ed o wn l o a d e dc o d eg o e sa n de x e c u t i o n 5 6 k b1 6b i t EWRAM 0200:0000h0203:FFFFh2 s t a r t s( n o r ma l l ye x e c u t i o ns t a r t sa tROM) .Du et ot h e1 6 b i tp o r t , y o uwa n t t h i ss e c t i o n ' sc o d et ob eTHUMBc o d e . Th i si sa l s oa v a i l a b l ef o rc o d ea n dd a t a .Th e3 2 b i tb u sa n dt h ef a c tt h a ti t ' s e mb e d d e di nt h eCPUma k et h i st h ef a s t e s tme mo r ys e c t i o n .Th e3 2 b i tb u s 2 k b 3 2b i t IWRAM 0300:0000h0300:7FFFh3 me a n st h a tARM i n s t r u c t i o n sc a nb el o a d e da to n c e , s op u ty o u rARM c o d e h e r e . k b IO RAM 0400:0000h0400:03FFh1 Me mo r y ma p p e dI Or e g i s t e r s .Th e s eh a v en o t h i n gt od owi t ht h eCPU r e g i s t e r sy o uu s ei na s s e mb l ys ot h en a mec a nb eab i tc o n f u s i n g .Do n ' t 1 6b i t b l a memef o rt h a t .Th i ss e c t i o ni swh e r ey o uc o n t r o l g r a p h i c s , s o u n d , b u t t o n s a n do t h e rf e a t u r e s . PAL RAM 0500:0000h0500:03FFh1 k b Me mo r yf o rt wop a l e t t e sc o n t a i n i n g2 5 6e n t r i e so f1 5 b i tc o l o r se a c h .Th e 1 6b i t f i r s ti sf o rb a c k g r o u n d s , t h es e c o n df o rs p r i t e s . VRAM Vi d e oRAM.Th i si swh e r et h ed a t au s e df o rb a c k g r o u n d sa n ds p r i t e sa r e 0600:0000h0601:7FFFh9 6 k b 1 6b i ts t o r e d .Th ei n t e r p r e t a t i o no ft h i sd a t ad e p e n d so nan u mb e ro ft h i n g s , i n c l u d i n gv i d e omo d ea n db a c k g r o u n da n ds p r i t es e t t i n g s . OAM 0700:0000h0700:03FFh1 k b 3 2b i tOb j e c tAt t r i b u t eMe mo r y .Th i si swh e r ey o uc o n t r o l t h es p r i t e s . PAK ROM 0800:0000hv a r v a r Ga mePa kROM.Th i si swh e r et h eg a mei sl o c a t e da n de x e c u t i o ns t a r t s , e x c e p twh e ny o u ' r er u n n i n gf r o mamu l t i b o o tc a b l e .Th es i z ei sv a r i a b l e , b u t 1 6b i tt h el i mi ti s3 2MB.I t ' sa1 6 b i tb u s , s oTHUMBc o d ei sp r e f e r a b l eo v e r ARM c o d eh e r e . Cart RAM 0E00:0000hv a r v a r Th i si swh e r es a v e dd a t ai ss t o r e d .Ca r tRAM c a nb ei nt h ef o r mo fSRAM, 8b i t Fl a s hROM o rEEPROM.Pr o g r a mma t i c a l l yt h e ya l l d ot h es a met h i n g : s t o r ed a t a .Th et o t a l s i z ei sv a r i a b l e , b u t6 4 k bi sag o o di n d i c a t i o n . Th ev a r i o u sRAM s e c t i o n s( a p a r tf r o mCa r tRAM)a r ez e r o e da ts t a r t u pb yBI OS.Th ea r e a sy o uwi l l d e a l wi t ht h e mmo s ta r eI O, file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 8/331 28-03-13 Tonc : GBA Programming in rot13 PAL, VRAM a n dOAM.Fo rs i mp l eg a me sa n dd e mo si twi l l u s u a l l ys u f f i c et ol o a dy o u rg r a p h i c sd a t ai n t oPALa n dVRAM a tt h es t a r t u s eI Oa n dOAM t ot a k ec a r eo ft h ea c t u a l i n t e r a c t i o n .Th el a y o u to ft h e s et wos e c t i o n si sq u i t ec o mp l e xa n da l mo s ti mp o s s i b l et of i g u r e o u to ny o u ro wn( a l mo s t , b e c a u s ee mu l a t o rb u i l d e r so b v i o u s l yh a v ed o n ej u s tt h a t ) .Wi t ht h i si nmi n d , r e f e r e n c es h e e t sl i k et h eGBATe k a n dt h eCo wBi t eSp e ca r eu n mi s s a b l ed o c u me n t s .I nt h e o r yt h i si sa l l y o un e e dt og e ty o us t a r t e d , b u ti np r a c t i c eu s i n go n eo rmo r e t u t o r i a l s( s u c ha st h i so n e )wi t he x a mp l ec o d ewi l l s a v eal o to fh e a d a c h e s . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 9/331 28-03-13 Tonc : GBA Programming in rot13 2. Setting up a development environment I n t r o d u c t i o n d e v k i t Pr oa n dd e v k i t ARM Al t e r n a t i v ed e v e l o p me n te n v i r o n me n t s Co mma n dl i n ed e t a i l sa n dl e g a c yt o p i c s 2.1. Introduction Un l e s sy o uwa n tt op u n c hi nt h ei n s t r u c t i o n si nb i n a r yi nah e xe d i t o r( “ Lu x u r y !Wh e nwewe r ey o u n gweh a dt ot o g g l ee a c hb i t i n d i v i d u a l l ywi t hma g n e t s ! ” ) , y o u ' l l n e e dad e v e l o p me n te n v i r o n me n tt ot u r nh u ma nr e a d a b l ec o d ei n t oma c h i n el a n g u a g e .Th e r ea r e s e v e r a l o p t i o n sh e r e , b u tt h ema i no n ei nGBAh o me b r e wi sd e v k i t Pr oa n dt h eARM c r o s s c o mp i l e rd e v k i t ARM.Th i sc h a p t e rwi l l s h o w y o uh o wt os e t u pt h en e c e s s a r yc o mp o n e n t sa n dh o wt og e ti tr u n n i n ga n dh o wt oc o mp i l et o n c ' sc o d ewi t hi t .I ' l l a l s os h o wwh e r ey o u c a nf i n ds o meo t h e rd e v e l o p me n tp a c k a g e sc u r r e n t l ya v a i l a b l e , b u tt h ef o c u si nt h i sa n do t h e rc h a p t e r swi l l b ed e v k i t Pr o / ARM. Th el a s ts e c t i o ne x p l a i n ss o meo ft h ed e t a i l sa b o u tu s i n gt h ec o mma n d l i n ea n dma k e f i l e s .I ti se s s e n t i a l l yo p t i o n a l , b u tf o rh i s t o r i c a l r e a s o n sIh a v et oc o v e ri tb e f o r et h er e s to ft h ec h a p t e r si n s t e a do fp u t t i n gi ti na na p p e n d i x . 2.2. devkitPro and devkitARM 2.2.1. Installation I nt h el a s tf e wy e a r s , devkitPro ( DKP)h a sb e c o met h es t a n d a r dt o o l c h a i nf o rGBAh o me b r e w a n di sa v a i l a b l ef o rWi n d o ws , Ma ca n dLi n u xp l a t f o r ms .De v k i t Pr oi sa c t u a l l yap a c k a g e , c o n t a i n i n g , c o mp i l e r sf o ran u mb e ro fs y s t e ms( i n c l u d i n gGBA) , l i b r a r ya n de x a mp l ec o d ea n da n e d i t o r .Yo uc a nf i n dt h ea c t u a l d o wn l o a d si nt h ed o wn l o a ds e c t i o no ft h es o u r c e f o r g ep a g e : h t t p : / / s o u r c e f o r g e . n e t / p r o j e c t s / d e v k i t p r o / . Fo rt h eGBA, y o uwi l l n e e d : devkitARM ( DKA) .Th eARM c r o s s c o mp i l e r , b a s e do nt h eGCCt o o l c h a i n . MSys.As h e l l wi t hb a s i cUn i xc o mma n d sl i k emakea n drm.Pr o b a b l yo n l yn e e d e df o r Wi n d o wsp l a t f o r ms , wh i c hu s u a l l yl a c kt h e s et o o l s . Ot h e rr e c o mme n d e di t e msa r e : Programmer's Notepad 2: a na d v a n c e dp l a i nt e x te d i t o rwi t hc o d eh i g h l i g h t e r s , c o d e f o l d i n gc a p a b i l i t i e sa n ds h e l l e x e c u t i o nc o mma n d s .Is u p p o s ey o uc o u l dc a l l i tami n i I DE. Ev e ni fy o uh a dy o u ro wne d i t o r , i ti sr e c o mme n d e dt h a ty o ug e tt h i so n ea swe l l b e c a u s e b o t hDKP' sa n dTo n c ' se x a mp l e sc o n t a i nPN2p r o j e c tf i l e s , wh i c hma k e si te a s i e rt ob u i l d GBAp r o j e c t s . libgba: as e to fb a s i ct y p e s , ma c r o sa n df u n c t i o n sf o ru s ei nGBAd e v e l o p me n t .Wh i l eI Fig 2.1: d e v k i t Pr od i rt r e e . wo n ' tb eu s i n gi th e r e , i ti ss t i l l wo r t hal o o k .Cu r r e n t l ylibgbaa n dt o n c ' so wnc o d el i b r a r y libtonca r ep r e t t ymu c hi n c o mp a t i b l e( mu l t i p l ed e f i n i t i o n sa n ds u c h ) , Ia mt r y i n gt oma k es u r et h a tt h e r ewo n ' tb ea n y c o n f l i c t s . GBA examples: as e to fe x a mp l ep r o j e c t su s i n gl i b g b a . Fo rWi n d o ws , t h e r ei sa ni n s t a l l e rt h a td o wn l o a d sa n di n s t a l l st h ec o mp o n e n t sa u t o ma t i c a l l y .Fo rMa ca n dLi n u x , y o u ' l l h a v et oi n s t a l l t h i n g sy o u r s e l f .Th ei n s t a l l a t i o np r o c e s sa l s oc r e a t e san u mb e ro fe n v i r o n me n tv a r i a b l e sf o rd i r e c t o r i e st od e v k i t Pr oa n dd e v k i t ARM, a n d a d d smsys/bint ot h ePATH. Wh e ni n s t a l l n gDKPo nWi n d o ws , t h e r e ' so n et h i n gy o umu s tb ea wa r eo f .GCCb a s e dt o o l sh a v et h e i ro r i g i n si nUn i x , a n dUn i xd o e s n ' t t a k ek i n d l yt os p a c e si np a t h s .Th e r e f o r edo not i n s t a l l i n t oad i r e c t o r ywi t hs p a c e s( l i k ec:\Program Files)a n dd o n ' tp u ty o u r p r o j e c t si naf o l d e rwi t hs p a c e si nt h en a mee i t h e r( l i k eMy Documents, wh i c hi sa c t u a l l y‘ s h o r t ’f o rc:\Documents And Settings\UserName\Blah Blah Blah More Ridiculously Long Directory Names That Never Fit In Textboxes\My Documents\) .Ba s i c a l l y , d o n ' tu s et h es t a n d a r dWi n d o wsd i r e c t o r i e s .Myo wni n s t a l l a t i o nt r e el o o k sl i k e f i g2 . 1 , b u ti t ' sc u s t o ma r yt op u td e v k i t Pr oi nc:\devkitPro. Do not use spaces in paths GCCma k e su s eo ft h eGCCt o o l c h a i n , wh i c hd o e s n ' tc o p ewe l l wi t hs p a c e si np a t h s( t h i n kMy Documents) .Sp a c e sa r e u s e da sas e p a r a t o rb e t we e nc o mma n d l i n eo p t i o n sa n dwh e ny o uh a v et h e mi np a t h st h et o o l swi l l i n t e r p r e tt h a ta sn e w o p t i o n s .Wh i l et h e r ea r ewa y st ou s et h e ma n y wa y , y o uc a ns a v ey o u r s e l fal o to fh e a d a c h e sb ys i mp l ys t a y i n gc l e a ro ft h e m. 2.2.2. Building projects with DevkitARM Th e r ea r es e v e r a l wa y so fb u i l d i n gGBAp r o j e c t s , b u tt h er e c o mme n d e dp r o c e s si tt ou s ema k e f i l e s .I np a r t i c u l a r , d e v k i t Pr o ' st e mp l a t e file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 10/331 28-03-13 Tonc : GBA Programming in rot13 ma k e f i l e s .Th eGBAt e mp l a t ema k e f i l e sc a nb ef o u n di n$(DEVKITPRO)/examples/gba/template.Wh e nc r e a t i n gan e w p r o j e c to fy o u ro wn , b a s ei to nt h i so n e .Yo uc a ns e et h eb a s i cs t r u c t u r eo ft h et e mp l a t ep r o j e c ti nf i g2 . 2 .Th e buildd i r e c t o r yi swh e r ea l l t h ei n t e r me d i a t ef i l e sg o .Yo u ' dr a r e l yh a v et ol o o kt h e r e .Th esourced i r e c t o r yi s wh e r ey o up u tt h es o u r c ec o d e : t h eC, C++a n dp e r h a p sa s s e mb l yf i l e s .I fy o uh a v eh e a d e rf i l e s , p u tt h o s ei n include. No t et h a tt h ebuilda n dincluded i r e c t o r i e sd o n ' ta c t u a l l ye x i s ti nt h et e mp l a t ep r o j e c ty e t ;buildi s c r e a t e db yt h eb u i l dp r o c e s si t s e l f , a n ds i n c et h e r ea r en oh e a d e r st oi n c l u d e , t h eincludef o l d e ri s n ' tn e c e s s a r yi n t h i sc a s ea n dh a sb e e nr e mo v e d , b u ti fy o ud oh a v eh e a d e r s , y o u ' dp u tt h e mt h e r e . a t e Th et e mp l a t ed i r e c t o r yi t s e l fh a st wof i l e s : t h ePN2p r o j e c tf i l e s , template.pnproja n dt h eMakefile. Fig 2.2:Templ pr o j e c t . On c ey o u ' v eo p e n e dt h ep r o j e c ti nPN2 , y o uc a nbuild t h ep r o j e c twi t hAlt+1, a n dclean t h ep r o j e c twi t hAlt+2. I fa l l i swe l l , y o us h o u l dg e ts o me t h i n gl i k et h i s : > "make" template.c arm-none-eabi-gcc -MMD -MP -MF /e/dev/devkitPro/examples/gba/template/build/template.d -g -Wall -O3 -mcpu=arm7tdmi -mtune=arm7tdmi -fomit-frame-pointer -ffast-math -mthumb -mthumb-interwork -I/e/dev/devkitPro/libgba/include -I/e/dev/devkitPro/examples/gba/template/build -c /e/dev/devkitPro/examples/gba/template/source/template.c -o template.o linking multiboot built ... template_mb.gba ROM fixed! > Process Exit Code: 0 > Time Taken: 00:02 Th eo u t p u tc o n s i s t so f6l i n e s : 1 . m̀ake' .I n v o k e smaket or u nt h ema k e f i l e . 2 . t̀emplate.c' .Th ef i l ewe ' r ec o mp i l i n g . 3 . àrm-none-eabi-gcc -MMD ...' .Th i sv e r yl o n gl i n e , s p l i to v e rmu l t i p l el i n e sh e r e , i n v o k e st h ec o mp i l e r .gcci sf r o n t e n do ft h ec o mp i l e r , a n darm-none-eabii st h ep r e f i xt h a td e v k i t ARM u s e st os e ti ta p a r tf r o ma l l t h eo t h e rv e r s i o n so f g c c .Th er e s ta r et h ec o mp i l e ro p t i o n s .Ba s i c a l l y , t h i swh o l et h i n gt u r n st h es o u r c ef i l etemplate.ci n t oa nobject file c a l l e d template.o. 4 . l̀inking multiboot' .Af t e rc o mp i l a t i o n , a l l o b j e c tf i l e sh a v et ob elinked t o g e t h e ri n t ot h ef i n a l b i n a r y .Th ea c t u a l c a l l i n go ft h el i n k e ri sh i d d e nh e r e , b u ti ti sa n o t h e rc a l l t oarm-none-eabi-gccwi t had i f f e r e n ts e to fo p t i o n s .I ' l l c o v e r wh a t“ mu l t i b o o t ”me a n sl a t e ri nt h es e c t i o n . 5 . b̀uilt ... template_mb.gba' .I n d i c a t e de v e r y t h i n gwo r k e d , a n dwen o wh a v eaGBAb i n a r yc a l l e d t e mp l a t e _mb . g b a . 6 . R̀OM fixed!' .Ea c hGBAROM s t a r t swi t hah e a d e rt h a tt h eGBAc h e c k st os e ei fi t ' sav a l i dGBAp r o g r a m.I ft h eh e a d e r c h e c kf a i l s , t h eGBAwi l l r e j e c tt h ep r o g r a m( e v e nt h o u g he mu l a t o r swi l l s t i l l a c c e p ti t ) .Th e r ei sat o o l c a l l e dgbafixt h a t p a t c h e st h eROM wi t hav a l i dh e a d e r , wh i c hi swh a tt h i sl i n ei sa b o u t . Fig 2.3: t e mpl a t e ( _ mb ) . g b a . Th eR̀OM fixed!' l i n eme a n st h eb u i l dh a ss u c c e e d e d .Yo us h o u l de n du pwi t hatemplate_mb.gba.Wh e ny o uo p e ni ti nVBA o rn o $ g b ay o us h o u l ds e es o me t h i n gl i k ef i g2 . 3 .I fy o ud o n ' ts e ea. g b af i l eo ri ts h o wsawh i t es c r e e n , s o me t h i n gb e y o n dy o u rc o n t o l we n twr o n g .Bu tb e f o r eweg e tt owh a tc o u l db ea mi s s , Iwa n ty o ut ot a k eal o o ki n s i d et h eMa k e f i l ei t s e l ff i r s t . Using other editors to manage projects Pr o g r a mme r ' sNo t e p a d2i sj u s to n eo ft h ema n ye d i t o r sy o uc a nwo r kwi t h .I np r i n c i p l e , a l l y o un e e di sa ne d i t o rc a p a b l eo f r u n n i n ge x t e r n a l t o o l sl i k emake.De v k i t Pr o ' sFAQh a san i c eo v e r v i e wo fs o meo ft h eo t h e ro p t i o n s . Ev e ni fy o ud ou s ea n o t h e re d i t o r , i t ' sag o o di d e at oa d dapnprojf i l ei fy o uwa n to t h e r st ob u i l dy o u rp r o j e c ts i n c et h e y ma yn o th a v et h es a mee d i t o r .Ev e na ne mp t yo n ewi l l s u f f i c e . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 11/331 28-03-13 Tonc : GBA Programming in rot13 Prefix changes in devkitARM r41 I nd e v k i t ARM r 4 1 , t h ec o mmo np r e f i xf o rGCC' st o o l sc h a n g e df r o marm-eabit oarm-none-eabi.Th i sme a nt h a ta l l o l d e rma k e f i l e swo n ' two r ka n y mo r e( i n c l u d i n gt o n c ' s ) .Tof i xt h i s , j u s tr e p l a c et h eo l dp r e f i xwi t ht h en e wo n e . Icould h a v ea v o i d e dt h i sb yu s i n gt h es t a n d a r dma k e f i l e s , b u tt h e yd i d n ' te x i s twh e nIs t a r t e d , a n dn o wi t ' sj u s tt o ol a t et o s wi t c h:(. 2.2.3. DKP's makefile Amakefile i sas c r i p tu s e dt oma n a g et h ef i l e so fap r o j e c ta n dt h es t e p sn e c e s s a r yt ob u i l d , c l e a no ri n s t a l l ap r o g r a m.Th e yc o n s i s to f r u l e st h a td e s c r i b et h ed e p e n d e n c i e sb e t we e nt h ev a r i o u sf i l e so ft h ep r o j e c ta n dwh i c hc o mma n d st ou s e .Th ed e v k i t Pr ot e mp l a t e ma k e f i l e sa r ea l mo s tc o mp l e t e l ya u t o ma t e d : a l l t h er e l e v a n tr u l e sa r ea l r e a d yi np l a c ea n da l l y o uh a v et od ot oa d ds o u r c ef i l e st oa p r o j e c ti st e l l t h ema k e f i l ewh i c hd i r e c t o r i e st h es o u r c e sa r ei n .Ba s i c a l l y , t h e y ' r ep r e t t yf u c k i n ga we s o me .Th e y ' r ea l s op r e t t yf u c k i n g my s t i f y i n gf o rf i r s t t i meu s e r s .I fy o us t i c kt ot h es t a n d a r dp r o c e d u r ee v e r y t h i n gs h o u l dwo r kr i g h to u to ft h eb o x , b u ti fy o uwa n tt we a k h o wt h i n g sa r ed o n e , h e r ea r et h emo s ti mp o r t a n tp a r t sf r o mau s e r ' sp e r s p e c t i v e . Th eMakefileb e g i n sl i k et h i s : #--------------------------------------------------------------------------------# Clear the implicit built in rules #--------------------------------------------------------------------------------.SUFFIXES: #--------------------------------------------------------------------------------ifeq ($(strip $(DEVKITARM)),) $(error "Please set DEVKITARM in your environment. export DEVKITARM=<path to>devkitARM) endif include $(DEVKITARM)/gba_rules #--------------------------------------------------------------------------------# TARGET is the name of the output, if this ends with _mb a multiboot image is generated # BUILD is the directory where object files & intermediate files will be placed # SOURCES is a list of directories containing source code # DATA is a list of directories containing data files # INCLUDES is a list of directories containing header files #--------------------------------------------------------------------------------TARGET := $(shell basename $(CURDIR)) BUILD := build SOURCES := source DATA := INCLUDES := #--------------------------------------------------------------------------------# options for code generation #--------------------------------------------------------------------------------ARCH := -mthumb -mthumb-interwork CFLAGS := -g -Wall -O3\ -mcpu=arm7tdmi -mtune=arm7tdmi\ -fomit-frame-pointer\ -ffast-math \ $(ARCH) CFLAGS += $(INCLUDE) CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions ASFLAGS := $(ARCH) LDFLAGS = -g $(ARCH) -Wl,-Map,$(notdir $@).map #--------------------------------------------------------------------------------# path to tools - this can be deleted if you set the path to the toolchain in windows #--------------------------------------------------------------------------------export PATH := $(DEVKITARM)/bin:$(PATH) #--------------------------------------------------------------------------------# any extra libraries we wish to link with the project #--------------------------------------------------------------------------------LIBS := -lgba #--------------------------------------------------------------------------------# list of directories containing libraries, this must be the top level containing file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 12/331 28-03-13 Tonc : GBA Programming in rot13 # include and lib #--------------------------------------------------------------------------------LIBDIRS := $(LIBGBA) ## more ... Th i sp a r to ft h ema k e f i l es e t su pc e r t a i nv a r i a b l e st h a ta r eu s e dl a t e r .Th ev a r i o u sFLAGSv a r i a b l e sa r ec o mp i l e r , a s s e mb l ya n dl i n k e r f l a g s .Yo ud o n ' tr e a l l yh a v et ot o u c ht h o s e , t h o u g hy o uma ywa n tt ou s e-O2i n s t e a do f-O3b e c a u s e-O3t e n d st ob l o a tc o d ep r e t t y s e v e r e l y .Th er e a l l yi mp o r t a n tp a r ti st h i s : #--------------------------------------------------------------------------------# TARGET is the name of the output, if this ends with _mb a multiboot image is generated # BUILD is the directory where object files & intermediate files will be placed # SOURCES is a list of directories containing source code # DATA is a list of directories containing data files # INCLUDES is a list of directories containing header files #--------------------------------------------------------------------------------TARGET := $(shell basename $(CURDIR))_mb BUILD := build SOURCES := source DATA := INCLUDES := Li k et h ec o mme n t ss a y , t h eSOURCESv a r i a b l el i s t st h ed i r e c t o r i e swh e r ey o u rc o d ei s .I nt h i sc a s e , a l l t h ec o d ei si nsource.I fy o u h a v ec o d ei no t h e rd i r e c t o r i e sa swe l l , a d dt h e mh e r es e p a r a t e db ys p a c e s .Ye s , spaces;t h a t ' swh a tmakeu s e st ot e l l t o k e n sa p a r t( a n d t h i si sa l s owh yy o us h o u l d n ' tp u ts p a c e si np a t h s ) .I fy o uh a v es u b d i r e c t o r i e sa swe l l , u s ef o r wa r ds l a s h e s( ' / ' ) , n o tb a c k wa r ds l a s h e s ( ' \ \ ' ) . Si mi l a r l y , DATAa n dINCLUDESa r et h el i s t sf o rb i n a r yd a t aa n dh e a d e rf i l e s .I nt h i sc a s et h e y ' r ee mp t yb e c a u s et h e r e ' sn oe x t r a d a t ao rh e a d e r s .Th ed i r e c t o r i e sa r er e l a t i v et ot h el o c a t i o no ft h ema k e f i l e ;t oi n d i c a t es o u r c ei si nt h a td i r e c t o r y , u s eap e r i o d( ' . ' ) . Th eTARGETl i n ei sa l s oi n t e r e s t i n g .I ti st h en a meo ft h eo u t p u tf i l e , wi t h o u ta ne x t e n s i o n .$̀(shell basename $(CURDIR))' g i v e st h el a s tp a r to ft h ec u r r e n td i r e c t o r y , wh i c hi nt h i sc a s ewo u l db etemplate.I no t h e rwo r d s , i ta u t o ma t i c a l l yu s e st h en a meo f t h ep r o j e c t ' sd i r e c t o r yf o rt h eROM n a mea swe l l . Th ee x t r a` _mb' h e r ei n d i c a t e st h i ss h o u l db eb u i l ta samu l t i b o o tg a me .Th e r ea r et wok i n d so fGBAb u i l d s : c a r t r i d g ea n dmu l t i b o o t . Th ema i nd i f f e r e n c ei swh e r et h ec o d ea n dc o n s t a n td a t ar e s i d e s .I nac a r t r i d g eg a mei t ' si nROM ( 3 2 MB) ;i nmu l t i b o o ti t ' si nEWRAM ( 2 5 6 k b ) .Te c h n i c a l l y , c a r t i d g ei st h en o r ma l k i n db i n a r y , b u tmu l t i b o o tc a nb er u no v e ramu l t i b o o tc a b l e . Cart vs multiboot builds Th e r ea r et wod i f f e r e n tk i n d so fg b ab u i l d s : ‘ c a r t ’b u i l d sa n d‘ mu l t i b o o t ’b u i l d s .Ac a r tb u i l dp u t st h ema i nc o d ea n dd a t ai n t h e3 2 MBROM ( 0800:0000h)o fac a r t .Amu l t i b o o tb u i l dp u t st h a ts t u f fi nt h e2 5 6 k BEWRAM ( 0200:0000) . Co mme r c i a l g a me sa r eo b v i o u s l yc a r tb u i l d s , b u tma k eu s eo fmu l t i b o o tb u i l d st oma k es i n g l e c a r tmu l t i p l a y e rp o s s i b l e . Ot h e rt h a nt h ema x i mu ms i z e , t h e r ei sl i t t l ed i f f e r e n c ei ng a me p l a yb e t we e nb o t h .Fo rh o me b r e w, mu l t i b o o td o e sh a v eo n e a d v a n t a g e , n a me l yt h a ty o uc a nl o a dag a meu pt oh a r d wa r ewi t h o u tt h en e e do fa ne x p e n s i v ef l a s h c a r t ;y o uc a nb u i l dy o u r o wnPCGBAc a b l ef o rp e a n u t s . Ch o o s i n gt h ek i n do fb u i l di sd o n ea tl i n k t i met h r o u g hl i n k e rs p e c s .Fo rc a r t b u i l d su s e-specs=gba.specsa n df o r mu l t i b o o tb u i l d su s e-specs=gba_mb.specs.I ft h eTARGETe n d swi t h_mb, t h et e mp l a t ema k e f i l ewi l l l i n ki ta sa mu l t i b o o tg a me . 2.2.4. When compilers attack I nmo s tc a s e s , t h es t e p sg i v e nt h u sf a rwi l l ‘ J u s tWo r k ’ .Ho we v e r , i ti sp o s s i b l et h a tt h ei n s t a l l a t i o no rt h eb u i l dd i d n ' tq u i t eg ot h ewa yi t s h o u l d .He r ei sas h o r tl i s to fp o t e n t i a l e r r o r sy o uma yc o mea c r o s swh e nb u i l d i n gt h et e mp l a t ep r o j e c t . `make not found' Al s ok n o wna sF̀a i l e dt oc r e a t ep r o c e s s : Th es y s t e mc a n n o tf i n dt h ef i l es p e c i f i e d . ' o rm̀a k e : u n k n o wnc o mma n do rf i l e n a me ' o ra n yo f t h eo t h e rv a r i a n t s .Th i sme a n st h a ti tc a n ' tf i n dt h emakec o mma n d .Th i ss h o u l db ei n$(DEVKITPRO)/msys/bin, a n dt h i sp a t h should h a v eb e e na d d e dt ot h es y s t e mp a t hb yt h ei n s t a l l a t i o n .Lo o ki nt h er i g h td i r e c t o r yi fmake.exei st h e r e .I fi ti st h e r e , c h e c kt h e s y s t e mp a t hi nM̀y Computer/Advanced/Environment Variables/System Variables' .Asy o uc a np r o b a b l yt e l l , t h i ss h o u l dmo s t l yb eaWi n d o wsi s s u e . `arm-none-eabi-gcc: no such file or directory' makewo r k s , b u ti tc a n ' tf i n dt h ec o mp i l e r .Th ec o mp i l e ra n do t h e rt o o l sa r ei n$(DEVKITARM)/bina n dt h ema k e f i l ea l r e a d ya d d s t h a tt ot h ep a t hv i aèxport PATH := $(DEVKITARM)/bin:$(PATH)' .s ot h i se r r o rs h o u l d n ' th a p p e n .DEVKITARMi so n eo f t h es y s t e mv a r i a b l e st h ei n s t a l l a t i o nc r e a t e s ;i fy o u ' v emo v e dt h ed i r e c t o r i e swi t h o u tu p d a t i n gt h e s ev a r i a b l e s , t h i se r r o rc o u l dh a p p e n .I t i sa l s op o s s i b l ey o uh a v ea no l d e rv e r s i o no fd e v k i t ARM;b e f o r er 1 9t h ep r e f i xwa sarm-elf, n o tarm-none-eabi. file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 13/331 28-03-13 Tonc : GBA Programming in rot13 `This application has requested the Runtime to terminate it in an unusual way.' Th i si sa ne r r o rIs o me t i me sg e twh e nc o mp i l i n gf r o mt h eVi s u a l C++I DE.Th i si sn o taDKAe r r o r , b u tmo r eaWi n d o ws / MSVCo n e . Th en e x tc o mp i l a t i o na l wa y swo r k s . Windows Vista Th i swa sap r o b l e mb e f o r ed e v k i t ARM r 2 1 .Vi s t aa n dGCCd i d n ' tr e a l l yg e ta l o n gb e f o r et h a t . Build works; ROM shows white screen. Byd e f a u l t , t h eGBAs c r e e ni swh i t ea n di fy o uh a v ea ne mp t ymain(), t h i swo u l db et h er e s u l t .Ho we v e r , i fy o u ' r es u r et h a t s o me t h i n gshould h a v es h o wn , i ti sl i k e l yt h a ts o me t h i n gwe n twr o n ge v e nb e f o r ey o u rc o d ewa se v e rc a l l e d .Be f o r emain()t h e ROM' sboot code i sc a l l e d( $(DEVKITARM)/arm-none-eabi/lib/gba_crt0.s, i fy o u ' r ec u r i o u s ) , wh i c ht a k e sc a r eo fs o me h o u s e k e e p i n g .Wi n t e r mu t e( t h ed e v k i t Pr oma i n t a i n e r )s o me t i me st i n k e r swi t ht h eb o o t c o d eo rl i n k s c r i p t st oi mp r o v et h ep r o c e s s , b u t s o me t i me st h i n g sg owr o n g( s o r r y , Da v e , y o uk n o wi t ' st r u e ) . Ca s ei np o i n t : i fy o ub u i l dt h et e mp l a t ep r o j e c tu n d e rd e v k i t ARM r 2 1e x a c t l ya ss h o wnb e f o r ey o u ' l l g e tawh i t es c r e e nb e c a u s e t h e r ei sa nb u gi nt h el i n k s c r i p tf o rmu l t i b o o tb u i l d s .Th ee a s i e s twa yo u to ft h i si st os i mp l yn o tb u i l da smu l t i b o o twi t hr 2 1 .Al t e r n a t i v e s o l u t i o n sc a nb ef o u n da tf o r u m: 1 4 4 9 3 . I fy o ue v e rg e tawh i t es c r e e na f t e ru p g r a d i n gd e v k i t ARM e v e nt h o u g hi two r k e df i n eb e f o r e , t h i si sal i k e l ys u s p e c t .Th e r ei su s u a l l y a na n n o u n c e me n tt h r e a di nt h eg b a d e vf o r u ma n dc h a n c e sa r et h a ti fi ti sab o o t c o d e / l i n k s c r i p te r r o ry o u ' r en o tt h ef i r s tt on o t i c e . 2.2.5. Building Tonc's examples with devkitARM Al l o fTo n c ' sd e mo sa n dt h ec o d el i b r a r ytonclibh a v ePN2p r o j e c t s , s oi t ' sma i n l yama t t e ro fo p e n i n gt h o s ei nPr o g r a mme r ' s No t e p a d2a n dh i t t i n gAl t +1 . Th e r ea r ea l s op r o j e c tf i l e sf o ru s eo nVi s u a l C++6a n dh i g h e r .Th e s ema k eu s eo fama s t e rma k e f i l e , tonc.mak.Th i sma k e f i l e s e r v e sa sah u bf o rb u i l d i n ga n dc l e a n i n gi n d i v i d u a l o ra l l p r o j e c t s .Fo ri n d i v i d u a l p r o j e c t s , s e tt h eDEMOt h en a meo ft h ed e moy o uwa n t t ob u i l d .Fr o mwi t h i nMSVC, c h o o s et h ep r o p e rb u i l dc o n f i g u r a t i o na n db u i l da su s u a l .Ta b l e2 . 1h a sa no v e r v i e wo ft h eo p t i o n s . to ... run ... MSVC config b u i l dlibtonc.ama k el i b t o n c Bu i l dl i b t o n c b u i l dfood e mo ma k eDEMO=f o o Bu i l dSi n g l e c l e a nfood e mo ma k eDEMO=f o oc l e a nCl e a nSi n g l e b u i l da l l d e mo s ma k eb u i l d _a l l Bu i l dAl l c l e a na l l d e mo s ma k ec l e a n _a l l Cl e a nAl l Table 2.1: b u i l d i n gt o n cpr o j e c t s . 2.3. Alternative development environments De v k i t ARM i st h es t a n d a r dt o o l c h a i nf o rGBAh o me b r e wr i g h tn o wa n da l mo s tt h eo n l yo n es t i l l b e i n ga c t i v e l yma i n t a i n e d .De v e l o p i n g wi t hDKAme a n sC, C++o ra s s e mb l ya n db u i l d i n gu pe v e r y t h i n gf r o ms c r a t c h( o ra tl e a s tn e a r l ys c r a t c h ) .I fy o u ' dl i k ea n o t h e rl a n g u a g e o rar i c h e rAPI , t h e s ea l t e r n a t i v e sma yb ewo r t hat r y . devkit Advance Io n l yme n t i o nt h i sh e r eb e c a u s ei ti ss t i l l t e c h n i c a l l ya na l t e r n a t i v e , a n dmo s tt u t o r i a l ss t i l l r e f e rt oi t .d e v k i tAd v a n c ei sa n o t h e rGCCb a s e dt o o l c h a i na n dc a nb ec o n s i d e r e dt h es p i r i t u a l p r e d e c e s s o rt od e v k i t ARM.No wa d a y s , Ic a n ' tt h i n ko fa n yr e a s o nt ou s ed e v k i t Ad v a n c ei n s t e a do fd e v k i t ARM a s i d ep e r h a p sf r o mc o mp a t i b i l i t ywi t hv e r yo l dp r o j e c t s .I fy o u ' r es t i l l u s i n gi t , c o n s i d e rs wi t c h i n g . DKA vs DKA Bo t hd e v k i t ARM a n dd e v k i tAd v a n c ea r ea b b r e v i a t e da s“ DKA” , wh i c hmi g h tc a u s es o mec o n f u s i o n .Th e r ei sn or e a l wa y t ok n o wwh i c ho n eo n ei sr e f e r r e dt oe x c e p tp e r h a p sb yd a t e : d o c u me n t sp r i o rt o2 0 0 4 / 2 0 0 5wi l l r e f e rt od e v k i tAd v a n c e ; mo r er e c e n tt e x t swi l l p r o b a b l yme a nd e v k i t ARM. HAM, visualHAM and HEL HAM i sa n o t h e rGCCb a s e dt o o l c h a i n , b u ti ta l s oc o me swi t hHAMl i b , a nAPIf o rma n a g i n gb a c k g r o u n d s , s p r i t e sa n ds o u n d .Th e wi n d o wsi n s t a l l a t i o na l s oc o n t a i n sa nI DEc a l l e dv i s u a l HAM. Se t t i n gu pHAM i se a s y : s i mp l yd o wn l o a dt h ef r e e wa r ev e r s i o nf r o mwww. n g i n e . d ea n di n s t a l l .An dt h e ni n s t a l l a g a i nb e c a u s ei t ' s o n l yt h ei n s t a l l e rt h a ty o u ' v ej u s ti n s t a l l e d:P.Af t e rt h es e c o n di n s t a l l e v e r y t h i n gwi l l b er e a d y , b u ty o u ' l l a c t u a l l yh a v et woc o p i e so f e a c h , o n eo ft h e mc a ns a f e l yb er e mo v e d .Aswi t hDKA, d o n ' tu s es p a c e si np a t h s . HAM i su s e f u l i fy o ud o n ' twa n tt oh a v et oi n v o l v ey o u r s e l fwi t ht h eg u t so fGBAp r o g r a mmi n g , b u ty o ustill n e e dt os o mei d e ao f n o wt h eGBAf u n c t i o n st oma k eu s eo fHAM p r o p e r l y .Hi d i n gt h el o we rl e v e l sc a nb ed a n g e r o u so ns y s t e mswh e r er e s o u r c e sa r e s p a r s e , a n dt h eGBAc e r t a i n l yq u a l i f i e s .Is h o u l da l s op o i n to u tt h a tHAMl i bi s n ' te x a c t l ye f f i c i e n twh e ni tc o me st os p e e d .I fy o u ' r e u s i n gHAM, a l s og e tt h ea d d o nl i b r a r yc a l l e dHELb yPe r t e rSc h r a u tf r o mwww. c o n s o l e d e v . d e .Un l i k ema n yo fHAM' sf u n c t i o n s , HEL' sc o d eh a sb e e no p t i mi z e dt oma k et h emo s to ft h eGBA' sc a p a b i l i t i e s .HELi sa l s os t i l l b e i n gma i n t a i n e d . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 14/331 28-03-13 Tonc : GBA Programming in rot13 HAM vs HEL Vi s u a l HAM' sc r e a t o r , Pe t e rSc h r a u th a sa l s owr i t t e na na d d o nl i b r a r yc a l l e dHEL.Un l i k eHAM, s o met i meh a sb e e ns p e n t o no p t i mi z i n gHEL' sc o d e , o ra tt h ev e r yl e a s tt oma k ei tn o ts l o w.I fy o u ' r eu s i n gHAM, c o n s i d e ru s i n gHELa swe l l . Other languages Th e r ea r es o men o n C/ a s me n v i r o n me n t sf o rGBAo u tt h e r e , b u ta sf a ra sIk n o wt h e s ep r o j e c t sh a v emo s t l yb e e na b a n d o n e db yt h e i r o r i g i n a l a u t h o r s .No t et h a tmyk n o wl e d g eo ft h e s ep a c k a g e si se x t r e me l yl i mi t e d , s oIc a n ' td omu c hmo r et h a nl i n kt ot h es i t e swh e r e y o uc a nf i n dt h e m. Th e r ei sd r a g o n BASI C, wh i c hp r o v i d e saBASI Cl i k es y n t a x .Th i ss h o u l db es u i t a b l ef o rs ma l l p r o j e c t s , b u tI ' mn o ts u r ei tc a nb e u s e df o rf u l l g a me sl i k eaMa r i oc l o n e .Yo uc a nf i n daFr e e Pa s c a l f o rGBA/ NDSa ti t a p r o g a mi n g . f r e e . f r , a n di n s t r u c t i o n sf o ru s i n gFo r t h o rLu aa twww. t o r l u s . c o m.Fi n a l l y , t h e r ei s( o ra tl e a s twa s )s o me t h i n gc a l l e dCa t a l p u l ta twww. n o c t u r n a l c e n t r a l . c o m.Th i si sav e r y c o mp l e t ee n v i r o n me n twi t ha ne mu l a t o ra n dIt h i n kI ' v es e e nad e b u g g e rt h e r ea swe l l .It h i n kt h i sc o u l db ec o mp a r e dt oGa me Ma k e r , b u tt h e na g a i nIma yb ewr o n g . 2.4. Command line details and legacy topics Th i ss e c t i o ns e r v e st wop u r p o s e s : t og i v et h o s eu s e dt od e a l i n gs o l e l ywi t hGUI ss o meb a c k g r o u n di n f o r ma t i o no nh o wt owo r kwi t h c o mma n d l i n et o o l s( a n dh o wn o tt owo r kwi t ht h e m) .No w, t h i swo u l db eas u b j e c tf o ra na p p e n d i xi fi twe r e n ' tf o rh o wTo n c ' se a r l i e r c h a p t e r sa n di t se x a mp l e sa r es t r u c t u r e d . 2.4.1. Working with command-line tools. Fo rmo s tp e o p l en o wa d a y s , wo r k i n gwi t hp r o g r a msme a n sd o u b l e c l i c k i n go nad e s k t o ps h o r t c u to rd o u b l e c l i c k i n go naf i l ei nEx p l o r e r ( I ' mf o c u s s i n go nWi n d o wsh e r e .So r r y , o t h e r1 0 %) .Fo ro f f i c ewo r kt h i si su s u a l l ye n o u g h , a n dt h a t ' l l b et h ee n do fi t .Fo rd e v e l o p me n t wo r k( p a r t i c u l a r l yc o n s o l ed e v ) , i tr e a l l yp a y st oh a v ead e e p e ru n d e r s t a n d i n go fwh a t ' sg o i n go n . Mo s to ft h i ss u b s e c t i o nwi l l h a v eah i g hduh!f a c t o r .Fe e l f r e et os k i pi ti fi tg e t sal i t t l et o of a mi l i a r . Li k ea n yo t h e rf i l e s , p r o g r a mf i l e s( e x e c u t a b l e s )a r es t o r e ds o me wh e r ei nt h ef i l eh i e r a r c h y .Fo re x a mp l e , t h ema i ne x e c u t a b l eo fOf f i c e Wo r di sc a l l e dwinword.exea n dma yb ef o u n da tC:/Program Files/(... More Directories ...)/winword.exe.Th ep a t h n a mei sa l s ot h ec o mma n dt or u nt h ep r o g r a m: s i mp l yp a s st h ep a t h n a met ot h es h e l l t h eOSwi l l e x e c u t et h ep r o g r a m.Us u a l l yy o uwi l l d ot h i sv i as h o r t c u t so fs o mes o r t : d o u b l e c l i c k i n go nas h o r t c u tt e l l st h eGUIt or u nt h ea s s o c i a t e d t a r g e t .Yo uc a na l s oi n v o k ei tv i at h ec o mma n dl i n e .I nt h eStart Menu, y o uc a nf i n dRun....En t e r i n gwinwordt h e r ewi l l a l s o l a u n c hWo r d , j u s ta sad o u b l ec l i c kd i d . Fig 2.4: S t a r t >Ru nwi n d o w. Pr o g r a mso f t e na l l o wcommand-line options a swe l l , s e p a r a t e db ys p a c e s .Th et y p e so fo p t i o n sa v a i l a b l ed e p e n d so nt h ep r o g r a mi n q u e s t i o n , o fc o u r s e .Fo rwo r d , t h ema i no p t i o ni st op a s saf i l e n a met oo p e n .Fo re x a mp l e , winword "C:\foo\bar.doc" wi l l o p e nC:\foo\bar.doc( s e ef i g2 . 4 ) .Th es a met h i n gh a p p e n swh e ny o ud o u b l e c l i c kaWo r dd o c u me n t : Wi n d o wsp i c k su pt h e f i l e n a me , l o o k su pwh i c ha p p l i c a t i o ni t ' sa s s o c i a t e dt oa n dc a l l st h a ta p p l i c a t i o nwi t ht h ef i l e n a mea sa no p t i o n . The value of the command-line Ofc o u r s e , u s i n gt h ec o mma n d l i n et oo p e naWo r dd o c u me n tma ys e e ms l i g h t l ys i l l yc o n s i d e r i n gy o uc a nd ot h es a met h i n gb yj u s t d o u b l e c l i c k i n gt h ef i l ei t s e l f , b u tt h e r ea r ei n s t a n c e swh e r et h er e v e r s ei st r u e .Fo re x a mp l e , y o uc a nu s ei tt oo p e nmu l t i p l ed o c u me n t s a to n c e(ẁinword C:\a.doc C:\b.doc' )o rma k ei tp r i n tt h e m, o rwh a t e v e rt h ep r o g r a ma l l o ws .GUI sma yb ee a s i e r s o me t i me s , b u tu s i n gt h ec o mma n d l i n ea l l o wsf o rmo r econtrol. As e c o n dg r e a tt h i n ga b o u tt h ec o mma n d l i n ei st h a ty o uc a nautomate p r o c e s s e s .Th i si sp a r t i c u l a r l yi mp o r t a n ti np r o g r a mmi n g , b e c a u s et h a tg e n e r a l l yi n v o l v e st a k i n gmu l t i p l es t e p sf o re a c hf i l ei nt h ep r o j e c t .Do i n ga l l o ft h a tma n u a l l yf o re a c hf i l ei nt h ep r o j e c ta n d e a c ht i mey o ur e b u i l di ss i mp l yb e y o n da n yr a t i o n a l c o n s i d e r a t i o n ;y o u ' l l wa n tas c r i p tf o rt h a t .Ba t c h f i l e sa n dma k e f i l e sa r ee x a mp l e so f s u c hs c r i p t s . Basic steps for building a GBA project file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 15/331 28-03-13 Tonc : GBA Programming in rot13 Co n v e r t i n gy o u rC/ C++/ a s ms o u r c e si n t oav a l i dGBAb i n a r yr e q u i r e st h ef o l l o wi n gf o u rs t e p s : 1 .Compile/assemble the sources.Th ef i r s ts t e pi st u r n i n gt h eh u ma nr e a d a b l eCo rC++f i l e s( .c/ .cpp)o ra s s e mb l yf i l e s ( .s/ .asm)t oab i n a r yf o r ma tk n o wna so b j e c tf i l e s( .o) .Th e r ei so n eo b j e c tf i l ef o re a c hs o u r c ef i l e . Th et o o l f o rt h i si sc a l l e darm-none-eabi-gcc.Ac t u a l l y , t h i si sj u s taf r o n t e n df o rt h er e a l c o mp i l e r , b u tt h a t ' sj u s t d e t a i l s .Th earm-none-eabi-h e r ei sj u s tap r e f i xs p e c i f i ct od e v k i t ARM;o t h e rt o o l c h a i n so rp l a t f o r msh a v ed i f f e r e n t p r e f i x e s .No t et h a tC++u s e sg++i n s t e a do fgcc. 2 .Link the object files.Af t e rt h a t , y o un e e dt ol i n kt h es e p a r a t eo b j e c tf i l e si n t oas i n g l ee x e c u t a b l eELFf i l e .An yp r e c o mp i l e d c o d el i b r a r i e s( .a)y o uma yh a v ea r el i n k e da tt h i ss t a g et o o . Yo uc a na c t u a l l yc o mp i l ea n dl i n ka tt h es a met i me , b u ti ti sg o o dp r a c t i c et h a ty o uk e e pt h e ms e p a r a t e : s e r i o u sp r o j e c t s u s u a l l yc o n t a i nmu l t i p l ef i l e sa n dy o ud o n ' twa n tt oh a v et owa i tf o rt h ewh o l ewo r l dt or e c o mp i l ewh e ny o uo n l yc h a n g e do n e . Th i sb e c o me se v e nmo r ei mp o r t a n twh e ny o us t a r ta d d i n gd a t a( g r a p h i c s , mu s i c , e t c ) . Ag a i n , arm-none-eabi-gcci su s e df o ri n v o k i n gt h el i n k e r , a l t h o u g ht h ea c t u a l l i n k e ri sc a l l e darm-none-eabi-ld. 3 .Translate/strip to pure executable.Th eELFf i l es t i l l c o n t a i n sd e b u gd a t aa n dc a n ' ta c t u a l l yb er e a db yt h eGBA( t h o u g h mo s te mu l a t o r swi l l a c c e p ti t ) .arm-none-eabi-objcopys t r i p st h ed e b u gd a t aa n dma k e ss u r et h eGBAwi l l a c c e p ti t . We l l , a l mo s t . 4 .Validate header.Ea c hGBAg a meh a sah e a d e rwi t hac h e c k s u mt oma k es u r ei t ' sav a l i dGBAb i n a r y .No r ma l l y , c o mp i l a t i o nd o e s n ' ts u p p l yo n e , s oweh a v et ou s eat o o l l i k eDa r k Fa d e r ' sgbafixt of i xt h eh e a d e r .Th i st o o l c o me swi t h DKA, s oy o ud o n ' th a v et od o wn l o a di ts e p a r a t e l y . Th ed e moi nt h en e x tc h a p t e ri sc a l l e dfirst, wh i c hu s e sas i n g l es o u r c ef i l e , first.c.Toc r e a t et h eb i n a r yfirst.gba, y o u ' l l n e e dt oe x e c u t et h ef o l l o wi n gc o mma n d s . # Compile first.c to first.o arm-none-eabi-gcc -mthumb -mthumb-interwork -c first.c # Link first.o (and standard libs) to first.elf arm-none-eabi-gcc -specs=gba.specs -mthumb -mthumb-interwork first.o -o first.elf # Strip to binary-only arm-none-eabi-objcopy -O binary first.elf first.gba # Fix header gbafix first.gba No t et h a ta p a r tf r o mt h ef i l e n a me s( b o l d e d ) , t h e r ea r ea l s od i f f e r e n to p t i o n sf o rt h et o o l s( a n y t h i n gt h a ts t a r t swi t hah y p h e n ) .Th e o p t i o n si ni t a l i c sa r et e c h n i c a l l yn o tr e q u i r e d , b u tr e c o mme n d e dn o n e t h e l e s s .I ' v ec o l l e c t e daf e wo ft h emo r ec o mmo nf l a g si nt h e ma k e f i l ea p p e n d i x , s ol o o kt h e mu pi fy o uwa n tt ok n o w.Yo uc a nl o o ku pt h ef u l l l i s to fo p t i o n si nt h ema n u a l s , t h o u g hIs h o u l dwa r ny o u t h a tt h en u mb e ro fo p t i o n sc a nb ev e r yl a r g e . devkitARM's linker requires a -specs option. Un l i k eo t h e rGBAt o o l c h a i n s , d e v k i t ARM r e q u i r e st h a te i t h e r-specs=gba.specso r-specs=gba_mb.specsi s p r e s e n ta sal i n k e ro p t i o n .Th e s es p e c sc o n t a i nt h eme mo r yma pwi t h o u twh i c ht h el i n k e rc a n ' td oi t sj o b .I fy o u ' r emi g r a t i n g f r o ma no l d e rt o o l c h a i na n df i n dt h a ts u d d e n l yt h eb i n a r yd o e s n ' two r ka n y mo r e , t h i si sal i k e l yc a u s e . I ti sa l s oag o o di d e at oa l wa y sh a v e-mthumb -mthumb-interworki nt h ec o mp i l e ra n dl i n k e rf l a g s .En a b l i n gc o mp i l e r o p t i mi z a t i o n( l i k e-O2)a n dwa r n i n g s( -Wall)a r eh e l p f u l a swe l l . Better living through automation Yo uc a nb u i l daGBAb i n a r yb yt y p i n gt h ec o mma n d sg i v e na b o v ei n t oac o mma n d l i n ei n t e r f a c ee a c ht i me .I ti sa l s op o s s i b l et oc l e a n t o i l e t swi t hat o o t h b r u s hb e f o r eu s ei to ny o u rt e e t h–j u s tb e c a u s ey o uc a nd o e s n ' ta l wa y sme a ny o us h o u l d .Toma n u a l l ye n t e re a c hl i n e wh e n e v e ry o uwa n tt or e b u i l di s , we l l , i n s a n e .I t ' smu c hmo r eu s e f u l t ou s es o mes o r to fs c r i p tt od ot h i sf o ry o u .Te c h n i c a l l y , y o uc a n u s ea n yk i n do fs c r i p t i n ge n v i r o n me n ty o uwa n t , b u tI ' l l f o c u so nt woi np a r t i c u l a rh e r e : b a t c h f i l e sa n dma k e f i l e s . Batch-files ( .bat)a r eWi n d o wss h e l l s c r i p tt h a th a v eb e e nt h e r es i n c ey eo l d eMSDOS.Ba t c h f i l e sa r ep r e t t ye a s yt ou s e : s i mp l y d r o pt h ec o mma n d si na.batf i l ea n dr u nt h a t .Bu ta su s u a l , c o mp l e xq u e s t i o n sh a v ee a s yt ou n d e r s t a n d , wr o n ga n s we r s .Wh i l eb a t c h f i l e sa r ei n d e e dv e r ye a s yt ou s e , t h e ya r eu t t e r l yi n a d e q u a t ef o ra n y t h i n gb u tt h emo s ts i mp l ep r o j e c t s .Mo r ec o mp l e xp r o j e c t swi l l h a v e mu l t i p l ef i l e sa n da d d i n ge x t r ac o mp i l a t i o nl i n e se v e r yt i mey o ua d daf i l eb e c o me sa n n o y i n g .Tob ef a i r , i ti sp o s s i b l et ou s ev a r i a b l e sa n d l o o p sa n ds t u f fi nb a t c h f i l e st oe a s et h i sal i t t l e , b u tn oo n ee v e rme n t i o n st h o s e . An o t h e rp r o b l e mi st h a ti fy o ur u nab a t c h f i l e , y o ur u nt h ewh o l et h i n g .Th i sme a n st h a ty o u ' r ec o mp i l i n gevery f i l ee v e r yt i me , a n d t h a ti ft h e r ea r ee r r o r s , y o u ' l l g e tt h ee r r o r sf o re v e r yf i l ei nt h ep r o j e c t .Th i sc a nb ev e r yt r i c k yt on a v i g a t ea n ds o me t i me si tma yn o tb e p o s s i b l ea ta l l b e c a u s et h ef i r s te r r o r sa r ep a s tt h es c r o l l l i mi t .( Th i swa se s p e c i a l l yt r u ef o rWi n d o wsv e r s i o n s9 8a n de a r l i e r , wh i c h d i d n ' te v e nh a v eas c r o l l b a rf o raDOSb o x .Ee e k ! ) La s t l y , t h es y n t a xf o rb a t c h f i l e sa r eDOS/ Wi n d o wso n l y .Th i sma k e st h e mu n s u i t e df o rp l a t f o r mi n d e p e n d e n td e v e l o p me n t . Ab e t t e rs o l u t i o ni su s i n gma k e f i l e s .Makefiles a r es c r i p t sr u nb yat o o l c a l l e dmake ( wh i c hwi n d o wsu s u a l l yd o e s n ' th a v e , b u ti tc o me s file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 16/331 28-03-13 Tonc : GBA Programming in rot13 wi t hMSy s ) .Ma k e f i l e sa r ep l a t f o r mi n d e p e n d e n ta n dma k ema n a g i n gf i l e se a s i e rb ywo r k i n gwi t hr u l e si n s t e a do fj u s tc o mma n d s .Yo u c a nh a v ep a t t e r nr u l e st h a tt e l l y o uh o wt ot u r nf i l e sf r o mo n et y p ei n t of i l e so fa n o t h e rt y p e( l i k ec o mp i l i n g.ci n t o.of i l e s )a n dmake wi l l t a k ec a r eo fi t ;a l l y o un e e dt od oi sg i v eal i s to ff i l e swh i c hn e e dt ob ec o mp i l e d .Makewi l l a l s oc h e c kwh e t h e rt h ec o mp i l a t i o ni s n e c e s s a r yi nt h ef i r s tp l a c es on ou n n e c e s s a r ywo r kwi l l b ed o n ei ft h eo u t p u tf i l ei sa l r e a d yu pt od a t e . Th ep r o b l e mwi t hma k e f i l e si st h a tt h e y ' r eh a r d e rt oc r e a t et h a nb a t c h f i l e s–a tl e a s tf o rt h eu n i n i t i a t e d .Bu tt h a n k st ot h ed e v k i t Pr o t e mp l a t ema k e f i l e s , y o ug e n e r a l l yd o n ' th a v et owo r r ya b o u tt h a ta n y mo r e : y o uc a nj u s ts e tt h ec o r r e c td i r e c t o r i e sa n dg o .Th a ts a i d , i ti s s t i l l wo r t hl e a r n i n gab i tmo r ea b o u th o wma k e f i l e swo r k .Fo rt h a tr e a s o n , t h en e x ts e c t i o ne x p l a i n sab i ta b o u tt h ema k e f i l ep r o c e s s .Th e ma k e f i l e si nt h eTo n ce x a mp l e sa l s oh a v eama k e f i l e st h a ti n c r e a s ei nc o mp l e x i t y . I fy o u ' r ea n n o y e dt h a tma k e f i l e sc a n ' tb ed o u b l e c l i c k e dt or u n , y o uc a na l wa y sc r e a t eab a t c h f i l et h a tr u n st h ema k e f i l e .So me t h i n gl i k e t h i ss h o u l ds u f f i c e . REM batch-file to run make make pause Don't s t a r tt h i sb a t c h f i l ewi t hm̀ake clean' t h o u g h , a st h a two u l df o r c eac o mp l e t er e b u i l d–s o me t h i n gwe ' r et r y i n gt oa v o i d . .I ' d a l s oa d v i s ea g a i n s tc a l l i n gi tmake.bat, b e c a u s et h a tma yc l a s hwi t ht h en a meo ft h ea c t u a l maket o o l . I ' dr e c o mme n da g a i n s tt h i sme t h o dt h o u g h .Th eb a t c h f i l eo u t p u twi l l g oi n t oaDOSb o x , wh i c hd o e s n ' te x a c t l yn a v i g a t en i c e l y .I two u l d b eb e t t e rt ou s ean o t e p a dt h a tc a ne x e c u t es h e l l c o mma n d sa n dc a p t u r ei t so u t p u t .Mo s to ft h e s ewi l l a l s oa l l o wy o ut og ot oe r r o r sb y d o u b l e c l i c k i n go nt h ee r r o rme s s a g e .PN2i so n eo ft h ema n ye d i t o r st h a tc a nd ot h e s et h i n g s . Prefer makefiles over batch-files Fo ra l l i t si n i t i a l e a s e , u s i n gb a t c h f i l e swi l l o n l yh u r ty o ui nt h el o n gr u n .I t ' sb e t t e rt ou s es o me t h i n gt h a tc a nd e a l wi t h c o mp l e xp r o j e c t sa swe l l f r o mt h eg e tg o . Ad o wns i d et oma k e f i l e si st h a ty o uc a n ' ta c t i v a t et h e mb yd o u b l e c l i c k i n g .I t ' sp o s s i b l et oc r e a t ead u mmyb a t c h f i l et o i n v o k et h ema k e f i l e , b u tab e t t e ra p p r o a c hwo u l db et ou s eac o d ee d i t o rt h a tc a na l s oe x e c u t es h e l l c o mma n d s . Paths and system variables I fy o ut r yt ob u i l da n y t h i n gu s i n gt h ec o mma n d sg i v e ne a r l i e r , y o u ' l l p r o b a b l yf i n dt h a ti td o e s n ' tq u i t ewo r k .Th i si sb e c a u s eIo mi t t e da n i mp o r t a n tb i to fi n f o r ma t i o n : t h ep a t h .f o rt h es h e l l t oe x e c u t et h ec o mma n d s , i tn e e d st ob ea b l et of i n dt h e mf i r s ta n dme r e l yu s i n g arm-none-eabi-gcci s n ' te n o u g hb e c a u s et h ef i l ei t s e l fi sa c t u a l l ya t[initial dirs]/devkitPro/devkitARM/bin/arm-none-eabi-gcc.Th efull path n e e d st ob ev i s i b l et ot h es h e l l i no r d e rf o r a n y t h i n gt oh a p p e n , n o tj u s tt h ef i l e n a me . Be c a u s et y p i n go u tt h ewh o l et h i n gi sr a t h e ra n n o y i n ga n db e c a u s emyd i r e c t o r ys t r u c t u r ema yb ed i f f e r e n tt h a ny o u r s , t h eo p e r a t i n g s y s t e mh a sav a r i a b l ec a l l e dPATHf o rs t a n d a r dd i r e c t o r i e s .I fy o uo n l yg i v et h ef i l e n a me , t h es h e l l wi l l s e a r c hi nt h ec u r r e n td i r e c t o r y a n da l l t h ep a t h si nt h ePATHf o rama t c h . I ti sp o s s i b l et oa d dt h eDKAbind i r e c t o r yt ot h ep a t hd i r e c t l y , b u td e v k i t Pr oh a sc h o s e nac l e a n e ra p p r o a c h .I n s t e a do fa d d i n gi tt o t h ePATH, t h ei n s t a l l e rc r e a t e san u mb e ro fe n v i r o n me n tv a r i a b l e st os o meo ft h ec o r ed i r e c t o r i e s , a n dy o uc a nu s et h e s ed u r i n gt h eb u i l d p r o c e s st op o i n tt ot h er e a l p a t h s .Fo re x a mp l e , t h e r ei saDEVKITARMv a r i a b l e , wh i c hi nmyc a s ee q u a t e st o /e/dev/devkitPro/devkitARM.Yo u r swi l l p r o b a b l yb eal i t t l ed i f f e r e n t , b u tt h ep o i n ti st h a ti nb o t hc a s e s $(DEVKITARM)/binwi l l b et h ed i r e c t o r ywh e r et h ema i nt o o l sa r e . No t et h a tt h es t a n d a r dWi n d o wsf o r ma tf o rd i r e c t o r i e si ss o me t h i n gl i k ec:/foo/bar, wh e r e a st h eDEVKITPARMv a r i a b l ei s f o r ma t t e da saPOSI Xp a t h n a mewi t hf o r wa r ds l a s h e s .Asf a ra sIk n o w, Wi n d o wsi st h eo n l yOSt h a td o e s n ' ta l l o wPOSI Xn a me s wh i c h , we l l , k i n d as u c k s .Th i si swh e r eMSy sc o me si n .MSy si sac o l l e c t i o no ft o o l st oma k et h es t a n d a r dUn i xt o o l sa v a i l a b l eo n DOS/ Wi n d o wss y s t e ms .Ap a r tf r o mmake, i ta l s oh a st h eb a s hs h e l l wh e r ey o uc a nu s ePOSI Xn a me sl i k ee v e r yo t h e rp r o g r a mme r . Tos wi t c ht ob a s hi naDOSb o x , t y p es̀h' .Ont h ewh o l e , b a s hi samo r eu s e f u l s h e l l t h a nDOS, t h o u g hy o uma yh a v et og e tu s e dt ot h e d i f f e r e n tc o mma n ds e t .Bu tt h a t ' swh yweh a v ema n u a l s . 2.4.2. Basic Makefilese Li k eb a t c h f i l e s , ma k e f i l e sa r es c r i p t st h a tc a na i dy o ui nb u i l d i n gap r o j e c t .Th ema i nd i f f e r e n c ei nh o wt h e ywo r ki st h a tb a t c h f i l e su s e s as e q u e n t i a l l i s to fc o mma n d s , wh i l ema k e f i l e su s eac h a i no frules t h a td e f i n eh o wf i l e sa r ec o n v e r t e di n t oo t h e r s , e v e n t u a l l yl e a d i n gt o t h eb i n a r y .Th i si st h eb a s i cf o r ma to far u l e : # Makefile rule example target : prerequisite command Th etarget c a nb et h eo u t p u tf i l eo rf i l e s , o rj u s ta ni d e n t i f i e rf o rt h er u l e , t h eprerequisite(s) a r et h ef i l e st h et a r g e td e p e n d so na n dt h e command(s) a r eal i s to fc o mma n d st h a tt u r nt h ep r e r e q u i s i t e si n t ot h et a r g e t s( a l t h o u g ht e c h n i c a l l yt h e yc a nd oo t h e rt h i n g sa swe l l ) . No t et h a tt h ei n d e n t a t i o no ft h ec o mma n d smust b eat a b( ASCI I9 ) , n o ts p a c e s .Th i si sa na n n o y i n gl i t t l er e q u i r e me n tt h a tc a nt r i py o u file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 17/331 28-03-13 Tonc : GBA Programming in rot13 u pwh e nc o p y p a s t i n gma k e f i l e s , s or e me mb e ri twe l l . Th ed i r e c te q u i v a l e n to ft h ec o mma n d su s e de a r l i e rt ob u i l dfirst.gbawo u l db el i k et h i s : # # Equivalent makefile for the earlier build procedure. # PATH := $(DEVKITARM)/bin:$(PATH) first.gba : first.c arm-none-eabi-gcc -mthumb -mthumb-interwork -c first.c arm-none-eabi-gcc -specs=gba.specs -mthumb -mthumb-interwork first.o -o first.elf arm-none-eabi-objcopy -v -O binary first.elf first.gba gbafix first.gba Th e r ei so n l yo n er u l eh e r e , wi t ht a r g e tfirst.gbaa n dp r e r e q u i s i t efirst.c.Th ec o mma n d sa r ej u s twh a twet y p e di ne a r l i e r . Tabs, not spaces, before make commands NOTE: GNU' smaker e q u i r e st a b sb e f o r ea c t u a l c o mma n d s , n o ts p a c e s .I fy o uc o p y p a s t e , y o uma yh a v et op l a c et h et a b s ma n u a l l y . Running makefiles Yo uc a ni n v o k emaket or u nt h ema k e f i l el i k et h i s : make -f file-name target-name Th e-̀f' f l a gi n d i c a t e swh i c hma k e f i l et oe x e c u t e ;t h etarget-name t e l l swh i c hr u l et os t a r tt h ec h a i nwi t h .Bo t ho ft h e s eo p t i o n sa r e a c t u a l l yo p t i o n a l .Wi t h o u tt h e-̀f' o p t i o n , makewi l l l o o ki nt h ec u r r e n td i r e c t o r yf o rf i l e sc a l l e d' GNUmakefile' , ' Makefile' o r ' makefile' a n dr u nt h a t .Th i si swh yma k e f i l e sa r eu s u a l l yc a l l e d‘ Ma k e f i l e ” .I ft h et a r g e tn a mei sa b s e n t , t h ec h a i ns t a r t sa tt h ef i r s t r u l ei nt h ef i l e . I t ' sn o tn e c e s s a r yt og ot ot h ec o mma n d l i n ea n dt y p ei nm̀ake' y o u r s e l f : I DEsc a no f t e nd ot h a tf o ry o u , a l t h o u g hs e t t i n gt h eI DEu p f o rt h a tc a nt a k es o med o i n g .Be c a u s et h e r ea r es oma n ye d i t o r s , Iwi l l n o tc o v e rt h i sh e r e ;g o o g l eo ru s et h eh e l pf i l e st of i g u r eo u twh a t n e e d st ob ed o n ef o ry o u re d i t o r .Ih a v ee x a mp l e sf o rs e t t i n gu pc o n TEXT, a na l t e r n a t i v ef o rPN, a n dMSVi s u a l St u d i o( 5a n d6 )i nt h i s a p p e n d i x .Th eDKPs i t ea l s oh a saf e we x a mp l e si ni t sFAQ Makefiles, version 2 Th ema k e f i l es h o wna b o v ewa sj u s ta ne x t r e me l ys i mp l e( a n dl i mi t e d )e x a mp l eo fwh a tama k e f i l ewo u l dl o o kl i k e .Pr o p e rma k e f i l e s h a v emu l t i p l er u l e sa n dma yu s evariables t od e f i n ec o mmo n l y u s e dd a t a .Th ef o l l o wi n gi samo r ec o mp l e x , b u ta l s omo r eu s e f u l . # # A more complicated makefile # PATH := $(DEVKITARM)/bin:$(PATH) # --- Project details ------------------------------------------------PROJ := first TARGET := $(PROJ) OBJS := $(PROJ).o # --- Build defines --------------------------------------------------PREFIX := arm-none-eabiCC := $(PREFIX)gcc LD := $(PREFIX)gcc OBJCOPY := $(PREFIX)objcopy ARCH SPECS := -mthumb-interwork -mthumb := -specs=gba.specs CFLAGS := $(ARCH) -O2 -Wall -fno-strict-aliasing LDFLAGS := $(ARCH) $(SPECS) .PHONY : build clean file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 18/331 28-03-13 Tonc : GBA Programming in rot13 # --- Build ----------------------------------------------------------# Build process starts here! build: $(TARGET).gba # Strip and fix header (step 3,4) $(TARGET).gba : $(TARGET).elf $(OBJCOPY) -v -O binary $< $@ -@gbafix $@ # Link (step 2) $(TARGET).elf : $(OBJS) $(LD) $^ $(LDFLAGS) -o $@ # Compile (step 1) $(OBJS) : %.o : %.c $(CC) -c $< $(CFLAGS) -o $@ # --- Clean ----------------------------------------------------------clean : @rm -fv *.gba @rm -fv *.elf @rm -fv *.o #EOF Th et o ph a l fo ft h i sma k e f i l ei ss p e n td e f i n i n gv a r i a b l e sf o rl a t e ru s e .So me t h i n gl i k eF̀OO := bar' d e f i n e sav a r i a b l ec a l l e dFOO wh i c hc a nt h e nb eu s e dv i a$(FOO).Al t h o u g hI ' mo n l yu s i n g:=h e r e , t h e r ea r eo t h e rme t h o d sa swe l l : r e c ts u b s t i t u t i o nv a r i a b l e( l i k eaCma c r o ) . = Di s i cv a r i a b l e( o v e r r i d e sp r e v i o u sd e f i n i t i o n ) . := Ba e a t ev a r i a b l ei fi td i d n ' te x i s ty e t . ?= Cr dt oe x i s t i n gv a r i a b l e . += Ad Table 2 . 2 : v a r i a b l ed e f i n i t i o n s . Th ev a r i a b l e sc r e a t e dh e r ea r emo s t l ys t a n d a r dt h i n g s : n a me sf o rt h ec o mp i l e ra n dl i n k e r( CCa n dLD)a n dt h e i rf l a g s( CFLAGSa n d LDFLAGS) .Th e s ea r e n ' ts t r i c t l yn e c e s s a r y , b u tt h e ya r eu s e f u l .Th et h i n g sa c t u a l l yr e l a t e dt ot h ep r o j e c ta r eTARGETa n dOBJS. TARGETi st h eb a s e n a meo ft h eo u t p u tb i n a r y , a n dOBJSi st h el i s to fo b j e c tf i l e s .No t e : list o fo b j e c tf i l e s !Ri g h tn o wt h e r e ' so n l ya s i n g l ef i l e , b u tl a t e rp r o j e c t swi l l h a v emu l t i p l es o u r c ef i l e st h a ta l l h a v et ob ec o mp i l e da n dl i n k e d .Byu s i n gav a r i a b l el i k et h i s , a d d i n ga n e wf i l et ot h ep r o j e c ti sama t t e ro fe x t e n d i n gt h i sl i s t .I ti sa l s oal i s to fobject f i l e s , n o ts o u r c ef i l e s .Th er u l e ss t a r ta r eb a s e do nt h e t a r g e tn a me s , n o tt h ep r e r e q u i s i t en a me s . Th e r ea r ea l s omo r er u l e sn o w.Th ep r i ma r yr u l e sa r ebuilda n dclean( t h e.PHONYi sj u s tt oi n d i c a t et h a tt h e y ' r en o ta c t u a l l y f i l e n a me st h e ms e l v e s ) .I nt h ebuildr u l ey o us e eh o wt h ec h a i n i n gwo r k s : buildd e p e n d so nt h e. g b ab i n a r y , wh i c hd e p e n d so nt h e . e l ff i l e , wh i c hd e p e n d so nt h eo b j e c tf i l e s , wh i c hd e p e n d so nt h es o u r c ef i l e s .I t ' sb a s i c a l l yt h eb a s i cs t e p sIg a v ee a r l i e ri nr e v e r s e . Pa r to ft h ema k e f i l ema g i ci st h a tar u l ewi l l o n l yb ee x e c u t e di ft h ep r e r e q u i s i t e sa r ey o u n g e rt h a nt h et a r g e t s .Fo re x a mp l e , i fa p a r t i c u l a rs o u r c e f i l eh a sb e e nmo d i f i e d , i twi l l b ey o u n g e rt h a ni t s. of i l ea n dt h ec o mp i l a t i o nr u l ewi l l r u nf o rt h a tp a r t i c u l a rf i l eb u tn o t t h eo t h e r s .Th i si sp a r t l ywh yd i v i d i n gt h ep r o c e s si n t os e p a r a t er u l e si su s e f u l . Th ef u n n yt h i n g swi t hwi t hd o l l a rs i g n s( $@, e t c )a r eautomatic variables.Th e ya r es h o r t h a n df o rt h et a r g e ta n dp r e r e q u i s i t en a me s . Yo uc a nf i n dwh a tt h e yme a ni nt a b l e2 . 3 .Th i si sj u s tt h r e eo ft h ea u t o ma t i cv a r i a b l e sa v a i l a b l e ;f o raf u l l l i s t , g ot ot h emakema n u a l . meo ft h ef i r s tp r e r e q u i s i t e $< Na s to fa l l t h ep r e r e q u i s i t e s $^ Li meo ft h et a r g e t $@ Na Table 2 . 3 : a u t o ma t i cv a r i a b l e sf o rr u l e s . Th el a s tt h i n gIwa n tt od i s c u s sh e r ec o n c e r n i n gt h i sp a r t i c u l a rma k e f i l ei st h ec o mp i l a t i o nr u l e .Th ef o r m %̀o : %.ci sa ne x a mp l ea static pattern rule.I tb a s i c a l l ys a y s“ f o re v e r yf i l ei nOBJSwi t ht h ee x t e n s i o n‘ . o ’ , l o o kf o rt h ec o r r e s p o n d i n g‘ . c ’f i l ea n dr u nt h e c o mma n d ” .Li k eIs a i de a r l i e r , OBJSc a nh a v emultiple f i l e n a me s , e a c ho fwh i c hwi l l c o mp i l ea u t o ma t i c a l l yv i at h i so n er u l e .Ag a i n , t h i s i so n eo ft h en i c et h i n g sa b o u tma k e f i l e s : t oa d daf i l ef o rt h ep r o j e c t , y o ud o n ' th a v et owr i t ea n o t h e rr u l e , j u s ta d di t so b j e c tn a met o OBJSa n dy o u ' r ed o n e .Th e r ea r ea l s op o s s i b i l i t i e st og e ta l l f i l e si nad i r e c t o r ys ot h a ty o uwo n ' th e v e nh a v et oa d di ty o u r s e l f , b u tt h a t ' s o u to ft h es c o p eo ft h i ss e c t i o n . 2.4.3.Legacy: on Tonc's structure Th i sl a s ts e c t i o ns h o u l d n ' tr e a l l yb eh e r e .Wi t hd e v k i t Pr o ' st e mp l a t ema k e f i l e s , ma n a g i n gp r o j e c t ss h o u l db ee a s ye n o u g hwi t h o u th a v i n g t ok n o wa n y t h i n ga b o u tma k e f i l e s , s ot h i ss t u f fc o u l db et u c k e ds a f e l yi na na p p e n d i x .Sowh yi si th e r e ? Th er e a s o ni t ' sp u ti nf r o n ti sh i s t o r i c a l i nn a t u r e .Wh e nIs t a r t e dt h i sa r o u n d2 0 0 4 , d e v k i t ARM wa ss t i l l y o u n ga n dl i b g b a , t h ei n s t a l l e r a n dt h et e mp l a t e ss i mp l yd i d n ' te x i s ty e t .Th e r eweah a n d f u l o fGBAt u t o r i a l swh i c hd i de x p l a i nt h eb a s i c s , b u ta l l u s e dp o o r( s o me t i me s file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 19/331 28-03-13 Tonc : GBA Programming in rot13 very p o o r )p r o g r a mmi n gs t a n d a r d sa n dp r o j e c ts t r u c t u r e .Wi t ht h el a t t e rIme a nt h r e et h i n g s : u s i n gt h ewr o n gc o mp i l e rf l a g s ; #i n c l u d i n gt h ewh o l ep r o g r a mi n t oas i n g l ef i l e( c o v e r e di ns o med e t a i l i nt h ed a t as e c t i o ni nt h ec h a p t e ro nb i t ma p s ) ; u s i n gb a t c h f i l e si n s t e a do fma k e f i l e s . c o d et h a twa ss i mp l yi n c o r r e c to ra tb e s tv e r yi n e f f i c i e n t . I n s t e a do fj u s ts a y i n gh o wt od ot h i n g s , Ia l s ot r i e dt oma k eap o i n ta b o u th o wnot t od ot h i n g s .Kn o wi n gwh a tt oa v o i dc a nb ej u s ta s i mp o r t a n ta sk n o wi n gt h er i g h tmo v e s .I ' v ea l s ot r i e dt oe a s ei n t oma k e f i l e ss ot h a tt h e ywo u l d n ' ts e e ms od a u n t i n gf o rn e wu s e r s .Th i s r e s u l t e di nd i v i d i n gTo n ci n t ot h r e ema i np a r t s : basic: c o mp l e t e l ys t a n d a l o n ep r o j e c t s ;wi t hv e r ys i mp l ema k e f i l e s . extended: p r o j e c t su s et o n c l i b ;ma k e f i l e sa r emo r ec o mp l e x . advanced: p r o j e c t su s et o n c l i ba n dma k e f i l e sd e r i v e df r o md e v k i t Pr o ' sma k e f i l e s . I nt h e‘ b a s i c s ’s e c t i o n , Is p e n dmu c ht i meo ng o o d / b a dp r a c t i c e st og e ti to u to ft h ewa y .Th i sr e q u i r e sk n o wi n ge l e me n t a r yma k e f i l e s , h e n c et h i ss e c t i o n .I fIh a dt h et i meo ri ft h e r ewa sar e a l n e e dI ' dd ot h i n g sd i f f e r e n t l yn o w, b u tt h er e q u i r e me n to ft h eg o o d / b a d p r a c t i c e sh a v ema d et h ee a r l i e rp a r t ss o me wh a th a r d e rt oma i n t a i nt h a nt h el a t e rc h a p t e r s .On eo fl i f e ' sl i t t l ei r o n i e s . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 20/331 28-03-13 Tonc : GBA Programming in rot13 3. My first GBA demo Fi n a l l y , y o u rf i r s tGBAp r o g r a m. Yo u rs e c o n df i r s tGBAp r o g r a m. Ge n e r a l n o t e so nGBAp r o g r a mmi n g Te s t i n gy o u rc o d eo nar e a l GBA. 3.1. Finally, your first GBA program No wt h a ty o uh a v ey o u rd e v e l o p me n te n v i r o n me n tr e a d y , i t ' st i met ot a k eal o o ka tas i mp l eGBAp r o g r a m.Fo rt h i swewi l l u s et h ec o d e f r o mt h eCf i l efirst.c.Th ep l a na tt h i sp o i n ti sn o tf u l l u n d e r s t a n d i n g ;t h ep l a ni st og e ts o me t h i n gt oc o mp i l ea n dg e ts o me t h i n g r u n n i n g .Th ec o d ewi l l b ed i s c u s s e di nt h i sc h a p t e r , b u twh a ti ta l l me a n swi l l b ec o v e r e di nl a t e rc h a p t e r s . // First demo. You are not expected to understand it // (don't spend too much time trying and read on). // But if you do understand (as a newbie): wow! int main() { *(unsigned int*)0x04000000 = 0x0403; ((unsigned short*)0x06000000)[120+80*240] = 0x001F; ((unsigned short*)0x06000000)[136+80*240] = 0x03E0; ((unsigned short*)0x06000000)[120+96*240] = 0x7C00; while(1); return 0; } Do n ' two r r ya b o u tt h ec o d ej u s ty e t , t h e r e ' st i mef o rt h a tl a t e r .An dd o n ' tl e a v ej u s ty e t , I ' l l g i v ean i c e rv e r s i o nl a t e ro n .Al l t h a tma t t e r s n o wi st h a ty o u ' r ea b l et oc o mp i l ea n dr u ni t .Th ema k e f i l et ob u i l dt h ep r o j e c twa sg i v e ni nt h es e t u p , b u tI ' l l r e p e a ti th e r e .Yo uc a na l s o f i n di to nTo n c ' se x a mp l e sf o l d e ru n d e rcode/basic/first. # # Makefile for first.gba # PATH := $(DEVKITARM)/bin:$(PATH) # --- Project details ------------------------------------------------PROJ := first TARGET := $(PROJ) OBJS := $(PROJ).o # --- Build defines --------------------------------------------------PREFIX := arm-none-eabiCC := $(PREFIX)gcc LD := $(PREFIX)gcc OBJCOPY := $(PREFIX)objcopy ARCH SPECS := -mthumb-interwork -mthumb := -specs=gba.specs CFLAGS := $(ARCH) -O2 -Wall -fno-strict-aliasing LDFLAGS := $(ARCH) $(SPECS) .PHONY : build clean # --- Build ----------------------------------------------------------# Build process starts here! build: $(TARGET).gba file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 21/331 28-03-13 Tonc : GBA Programming in rot13 # Strip and fix header (step 3,4) $(TARGET).gba : $(TARGET).elf $(OBJCOPY) -v -O binary $< $@ -@gbafix $@ # Link (step 2) $(TARGET).elf : $(OBJS) $(LD) $^ $(LDFLAGS) -o $@ # Compile (step 1) $(OBJS) : %.o : %.c $(CC) -c $< $(CFLAGS) -o $@ # --- Clean ----------------------------------------------------------clean : @rm -fv *.gba @rm -fv *.elf @rm -fv *.o #EOF Bu i l dt h ep r o j e c tb yo p e n i n gfirst.pnproja n dh i t t i n gAl t +1o rd o u b l e c l i c k i n g build.bat.Th i swi l l compile first.ct ofirst.o( t h e$(PROJ)i sr e p l a c e dwi t h‘ f i r s t ’ , r e me mb e r ? ) , link t h el i s to fo b j e c tf i l e s( c u r r e n t l yo n l yfirst.o)t ofirst.elf, translate first.elft ofirst.gbab ys t r i p p i n ga l l e x c e s sELFi n f o r ma t i o n , fix the header s ot h a tt h eGBAwi l l a c c e p ti t . Af t e rt h ema k e f i l eh a sr u n , y o us h o u l dh a v eaf i l ec a l l e dfirst.gba, i fy o ud o n ' tt h e r e ' sa p r o b l e mwi t hy o u rs e t u pb e c a u s et h ec o d es u r ei s n ' twr o n g .I ' v ema d eal i s to fp o t e n t i a l Fig 3.1: pi c t u r eo ft h ef i r s td e mo p r o b l e mss e t u p : d k p ;c h e c ki fy o u r si so n eo ft h e m. I fy o ud of i n dy o u r s e l fwi t haGBAe x e c u t a b l e , r u ni to nh a r d wa r eo ry o u re mu l a t o ro f c h o i c ea n dy o us h o u l dg e tar e d , ag r e e n , a n dab l u ep i x e l a tp o s i t i o n s( 1 2 0 , 8 0 ) , ( 1 3 6 , 8 0 )a n d( 1 2 0 , 9 6 ) , r e s p e c t i v e l y . No w, f o rt h ec o d ei t s e l f… 3.1.1. Huh? I fy o u ' r es o me wh a tc o n f u s e db yi t , y o uwo u l d n ' tb ea l o n e .Ie x p e c tt h a tu n l e s sy o ua l r e a d yk n o wat h i n go rt woa b o u tGBA p r o g r a mmi n go rh a v ee x p e r i e n c ewi t hl o wl e v e l p r o g r a mmi n gf r o mo t h e rp l a t f o r ms , t h ec o d ewi l l b eat o t a l my s t e r y .I fy o u ' r ep r o f i c i e n t e n o u g hi nCy o uma yh a v es o mei d e awh a t ' sma k i n gt h et h r e ep i x e l sa p p e a r , b u tIa d mi tt h a ti ti svery h a r dt os e e . An dt h a twa sk i n do fmyp o i n ta c t u a l l y .I fo n ewe r et oh a n dt h i si nf o rat e s ta tap r o g r a mmi n gc l a s s , y o uwo u l df a i l so h a r d .An di f n o t , t h ep r o f e s s o r ss h o u l db ef i r e d .Wh i l et h ec o d es h o wa b o v ed o e swo r k , t h ef a c tt h a ti t ' sa l mo s tu n r e a d a b l ema k e si tb a dc o d e . Wr i t i n gg o o dc o d ei sn o to n l ya b o u tg e t t i n gr e s u l t s , i t ' sa l s oa b o u tma k i n gs u r eother p e o p l ec a nu n d e r s t a n dwh a t ' sh a p p e n i n gwi t h o u tt o o mu c ht r o u b l e . Th ec o d eo ffirst.ca l s os e r v e sa n o t h e rp u r p o s e , n a me l ya sar e mi n d e rt h a tGBAp r o g r a mmi n gi svery l o wl e v e l .Yo ui n t e r a c t d i r e c t l ywi t ht h eme mo r y , a n dn o tt h r o u g hmu l t i p l el a y e r so fa b s t r a c t i o nb r o u g h tb yAPI s .Tob ea b l et od ot h a tme a n sy o uh a v et or e a l l y u n d e r s t a n dh o wc o mp u t e r swo r k , wh i c ha l l p r o g r a mme r ss h o u l dk n o wa tl e a s tt os o med e g r e e .Th e r ea r eAPI s( f o rl a c ko fab e t t e r wo r d )l i k eHAM t h a tt a k ec a r eo ft h el o we s tl e v e l s , wh i c hd e f i n i t e l yh a si t sme r i t sa si ta l l o wsy o ut od e a l wi t hmo r ei mp o r t a n ts t u f fl i k e a c t u a l game p r o g r a mmi n g , b u to nt h eo t h e rh a n di th i d e sal o to fd e t a i l s–d e t a i l st h a ts o me t i me sa r eb e t t e rl e f ti nt h eo p e n . Th o s ewh owa n tab e t t e r , mo r ei n t e l l i g i b l e , v e r s i o no ft h ep r e v i o u sc o d ec a ns k i pt h en e x ts e c t i o na n dmo v eo nt ot h es e c o n df i r s td e mo . Th ewa r p e dmi n d swh oc a n ' tj u s tl e ti tg oa n dwa n tt oh a v ea ne x p l a n a t i o nr i g h tn o w( f o rt h er e c o r d , Ic o u n tmy s e l fa mo n gt h e m) , h e r e ' s wh a t ' sg o i n go n . 3.1.2. Explanation of the code Th i si saq u i c ka n dd i r t ye x p l a n a t i o no ft h ee a r l i e rc o d e .Th o s ep r e v i o u s l yme n t i o n e dwa r p e dmi n d sf o rwh o mt h i ss e c t i o ni si n t e n d e dwi l l p r o b a b l yp r e f e ri tt h a twa y .Amo r ed e t a i l e dd i s c u s s i o nwi l l b eg i v e nl a t e r . AsIs a i d , GBAp r o g r a mmi n gi sl o wl e v e l p r o g r a mmi n ga n ds o me t i me sg o e sr i g h td o wnt ot h eb i t .Th e0x04000000a n d 0x06000000a r ep a r t so ft h ea c c e s s i b l eme mo r ys e c t i o n s .Th e s en u mb e r st h e ms e l v e sd o n ' tme a nmu c h , b yt h ewa y ;t h e yj u s tr e f e rt o d i f f e r e n ts e c t i o n s .Th e r ea r e n ' tr e a l l y0x02000000b e t we e nt h e s et wos e c t i o n s .Asy o uc a ns e ei nt h eme mo r yma p , t h e s et wo s e c t i o n sa r ef o rt h eI Or e g i s t e r sa n dVRAM, r e s p e c t i v e l y . Towo r kwi t ht h e s es e c t i o n si nC, weh a v et oma k ep o i n t e r so u to ft h e m, wh i c hi swh a tt h e‘ unsigned int*’a n d‘ unsigned short*’d o .Th et y p e su s e dh e r ea r ea l mo s ta r b i t r a r y ;a l mo s t , b e c a u s es o meo ft h e ma r emo r ec o n v e n i e n tt h a no t h e r s .Fo re x a mp l e , t h eGBAh a san u mb e ro fd i f f e r e n tv i d e omo d e s , a n di nmo d e s3a n d5VRAM i su s e dt os t o r e1 6 b i tc o l o r s , s oi nt h a tc a s ec a s t i n gi tt o file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 22/331 28-03-13 Tonc : GBA Programming in rot13 h a l f wo r dp o i n t e r si sag o o di d e a .Ag a i n , i ti sn o trequired t od os o , a n di ns o mec a s e sd i f f e r e n tp e o p l ewi l l u s ed i f f e r e n tt y p e so f p o i n t e r s .I fy o u ' r eu s i n gs o me o n ee l s e ' sc o d e , b es u r et on o t et h ed a t a t y p e so ft h ep o i n t e r su s e d , n o tj u s tt h en a me s . Th ewo r da t0 4 0 0 : 0 0 0 0c o n t a i n st h ema i nb i t sf o rt h ed i s p l a yc o n t r o l .Bywr i t i n g0x0403i n t oi t , wet e l l t h eGBAt ou s ev i d e omo d e 3a n da c t i v a t eb a c k g r o u n d2 .Wh a tt h i sa c t u a l l yme a n swi l l b ee x p l a i n e di nt h ev i d e oa n db i t ma pmo d ec h a p t e r s . I nmo d e3 , VRAM i sa1 6 b i tb i t ma p ;wh e nwema k eah a l f wo r dp o i n t e rf o ri t , e a c he n t r yi sap i x e l .Th i sb i t ma pi t s e l fi st h es a me s i z ea st h es c r e e n( 2 4 0 x 1 6 0 )a n db e c a u s eo ft h ewa yb i t ma p sa n dCma t r i c e swo r k , b yu s i n gs o me t h i n go ft h ef o r m‘ a r r a y [ y* width + x] ’a r et h ec o n t e n t so fc o o r d i n a t e s( x, y)o ns c r e e n .Th a tg i v e su so u r3p i x e l l o c a t i o n s .Wef i l l t h e s ewi t ht h r e e1 6 b i tn u mb e r st h a t h a p p e nt ob ef u l l r e d , g r e e na n db l u ei n5 . 5 . 5BGRf o r ma t .Ori st h a tRGB, Ia l wa y sf o r g e t .I na n yc a s e , t h a t ' swh a tma k e st h ep i x e l s a p p e a r .Af t e rt h a tt h e r ei so n emo r ei mp o r t a n tl i n e , wh i c hi st h ei n f i n i t el o o p .No r ma l l y , i n f i n i t el o o p sa r et h i n g st ob ea v o i d e d , b u ti nt h i s c a s ewh a th a p p e n sa f t e rmain()r e t u r n si sr a t h e ru n d e f i n e db e c a u s et h e r e ' sl i t t l et or e t u r nto, s oi t ' sb e s tt oa v o i dt h a tp o s s i b i l i t y . An dt h a t ' sa b o u ti t .Wh i l et h eSp a r t a np u r i t yo ft h ec o d ed o e sa p p e a l t oap a r to fme , Iwi l l s t r e s sa g a i nt h a tt h i si snot t h er i g h twa y t op r o g r a mi nC.Sa v et h er a wn u mb e r sf o ra s s e mb l yp l e a s e . 3.2. Your second first GBA program So , l e t ' ss t a r to v e ra g a i na n dd oi tright t h i st i me .Ora tl e a s tmo r er i g h tt h a nb e f o r e .Th e r ea r ean u mb e ro fs i mp l ewa y st oi mp r o v et h e l e g i b i l i t yo ft h ec o d e .He r ei st h el i s to ft h i n g swe ' l l d o . Fi r s ta n df o r e mo s ti st h eu s eo fnamed literals, t h a ti st os a y#d e f i n e dn a me sf o rt h ec o n s t a n t s .Th en u mb e r st h a twe n ti n t o t h ed i s p l a yc o n t r o l wi l l g e tp r o p e rn a me s , a swi l l t h ec o l o r st h a twep l o t t e d . We ' l l a l s ou s e#d e f i n ef o rt h ememory mapping: t h ed i s p l a yc o n t r o l a n dVRAM wi l l t h e nwo r kmo r el i k en o r ma l v a r i a b l e s . We ' l l a l s oc r e a t es o metypedefs, b o t hf o re a s eo fu s ea n dt oi n d i c a t ec o n c e p t u a l t y p e s .Fo ri n s t a n c e , a1 6 b i tc o l o ri s e s s e n t i a l l yah a l f wo r dl i k ea n yo t h e r , b u ti fy o ut y p e d e fi ta s , s a y , COLOR, e v e r y o n ewi l l k n o wt h a ti t ' sn o tan o r ma l h a l f wo r d , b u th a ss o me t h i n gt od owi t hc o l o r s . Fi n a l l y , i n s t e a do fp l o t t i n gp i x e l swi t ha na r r a ya c c e s s , wh i c hc o u l ds t i l l me a na n y t h i n g , we l l u s easubroutine f o ri ti n s t e a d . Na t u r a l l y , t h i swi l l e x p a n dt h et o t a l l i n e so fc o d eab i t .Qu i t eab i t , i nf a c t .Bu ti ti swe l l wo r t hi t .Th ec o d ei sa c t u a l l yat wo p a r t e r .Th e a c t u a l c o d e , t h et h i n gt h a th a sa l l t h ef u n c t i o n a l i t yo ft h ef i r s td e mo , c a nb ef o u n di nsecond.c.Al l t h ei t e msd i s c u s s e da b o v e , t h e t y p e d e f s , #d e f i n e sa n di n l i n e s , a r ep u ti ntoolbox.h. // toolbox.h: // // === NOTES === // * This is a _small_ set of typedefs, #defines and inlines that can // be found in tonclib, and might not represent the // final forms. #ifndef TOOLBOX_H #define TOOLBOX_H // === (from tonc_types.h) ============================================ typedef unsigned char u8; typedef unsigned short u16; typedef unsigned int u32; typedef u16 COLOR; #define INLINE static inline // === (from tonc_memmap.h) =========================================== #define MEM_IO #define MEM_VRAM #define REG_DISPCNT 0x04000000 0x06000000 *((volatile u32*)(MEM_IO+0x0000)) // === (from tonc_memdef.h) =========================================== // --- REG_DISPCNT defines --#define DCNT_MODE0 0x0000 #define DCNT_MODE1 0x0001 #define DCNT_MODE2 0x0002 #define DCNT_MODE3 0x0003 #define DCNT_MODE4 0x0004 #define DCNT_MODE5 0x0005 // layers #define DCNT_BG0 0x0100 #define DCNT_BG1 0x0200 file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 23/331 28-03-13 Tonc : GBA Programming in rot13 #define DCNT_BG2 #define DCNT_BG3 #define DCNT_OBJ 0x0400 0x0800 0x1000 // === (from tonc_video.h) ============================================ #define SCREEN_WIDTH 240 #define SCREEN_HEIGHT 160 #define vid_mem ((u16*)MEM_VRAM) INLINE void m3_plot(int x, int y, COLOR clr) { vid_mem[y*SCREEN_WIDTH+x]= clr; } #define CLR_BLACK #define CLR_RED #define CLR_LIME #define CLR_YELLOW #define CLR_BLUE #define CLR_MAG #define CLR_CYAN #define CLR_WHITE 0x0000 0x001F 0x03E0 0x03FF 0x7C00 0x7C1F 0x7FE0 0x7FFF INLINE COLOR RGB15(u32 red, u32 green, u32 blue) { return red | (green<<5) | (blue<<10); } #endif // TOOLBOX_H #include "toolbox.h" int main() { REG_DISPCNT= DCNT_MODE3 | DCNT_BG2; m3_plot( 120, 80, RGB15(31, 0, 0) ); m3_plot( 136, 80, RGB15( 0,31, 0) ); m3_plot( 120, 96, RGB15( 0, 0,31) ); // or CLR_RED // or CLR_LIME // or CLR_BLUE while(1); return 0; } Asy o uc a ns e e , t h en u mb e ro fl i n e si ntoolbox.hi sa c t u a l l ymu c hl a r g e rt h a nt h a to ft h er e a l c o d e .Th i sma ys e e ml i k eab i to fa wa s t en o w, b u tt h i si so n l yb e c a u s ei t ' ss u c has ma l l d e mo .No n eo ft h ec o n t e n t so ftoolbox.hi sa c t u a l l yc o mp i l e d , s ot h e r ei sn o c o s ti nt e r mso fme mo r yu s e .I nf a c t , i fi td i di two u l d n ' tb e l o n gi nah e a d e rf i l e , b u tt h a t ' sad i s c u s s i o nI ' l l g oi n t oa n o t h e rt i me .Ri g h t n o w, l e t ' ss e ewh a twea c t u a l l yh a v ei ntoolbox.h 3.2.1. The toolbox Types and typedefs Fi r s to fa l l , wec r e a t es o mes h o r t h a n dn o t a t i o n so fc o mmo n l yu s e dt y p e s .Noma t t e rwh a ta n y o n es a y s , brevity is a virtue.Fo r e x a mp l e , u n s i g n e dt y p e sa r ev e r yc o mmo na n dwr i t i n go u tt h ef u l l n a me s( e . g , ‘ unsigned short’ )s e r v e sl i t t l ep u r p o s e .Th e s h o r t h a n d‘ u16’i sj u s tmu c hmo r ec o n v e n i e n t .Be s i d e sc o n v e n i e n c e , i ta l s og i v e sb e t t e ri n f o r ma t i o no nt h es i z eo ft h ev a r i a b l e , wh i c hi s a l s oo fg r e a ti mp o r t a n c eh e r e . I ' v ea l s oa d d e daconceptual typedef.Wh i l ei t ' st r u et h a t , i np r i n c i p l e , a ni n ti sa ni n tn oma t t e rwh a ti t ' su s e df o r , i ti sh e l p f u l i fy o u c a nt e l l wh a ti t ss u p p o s e du s ei sf r o mi t st y p e .I nt h i sc a s e , Ih a v eaCOLORa l i a sf o ru16wh e nIwa n tt oi n d i c a t eap a r t i c u l a rh a l f wo r d c o n t a i n sc o l o ri n f o r ma t i o n . Memory map defines Tob ea b l et owo r kd i r e c t l ys p e c i f i ca d d r e s s e si nme mo r y , y o u ' l l h a v et oc a s tt h e mt op o i n t e r so ra r r a y sa n dwo r kwi t ht h o s e .I nt h i s d e mo ' sc a s e , t h ea d d r e s s e swe ' r ei n t e r e s t e di na r e0600:0000( VRAM)a n d0400:0000( t h ed i s p l a yc o n t r o l r e g i s t e r ) .I nt h ef i r s t d e moId i dt h ec a s t sma n u a l l y , b u ti t ' sb e t t e rt ou s en a me sf o rt h e ms ot h a ty o ud o n ' th a v et or e me mb e ra l l t h en u mb e r sa n da l s ob e c a u s e n o b o d ye l s ewo u l dh a v ea n yc l u et owh a t ' sg o i n go n . Fo rt h eI Or e g i s t e r sI ' mu s i n gt h eo f f i c i a l n a me s , wh i c ha r er e c o g n i z e db ya l l p a r t i e s .Th ed i s p l a yc o n t r o l i sk n o wna s REG_DI SPCNT, a n di sd e f i n e da st h ewo r da t0400:0000.No t et h a tn e i t h e rt h en a men o rt h et y p ea r es e ti ns t o n e : y o uc o u l da s e a s i l yh a v ec a l l e di t“ BOO”a n de v e nu s e dah a l f wo r dp o i n t e r .Th ef u l l l i s to fr e g i s t e r#d e f i n e sc a nb ef o u n di nt o n c l i b ' sregs.h. file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 24/331 28-03-13 Tonc : GBA Programming in rot13 Fo rt h o s ewh oa r e n ' ta sf a mi l i a rwi t hp o i n t e r sa sy o us h o u l d( b o y , a r ey o ug o n n ab ei nt r o u b l e:P) , h e r ei st h es t r u c t u r eo ft h e REG_DI SPCNT#d e f i n e .I ' mu s i n gvu32a sat y p e d e ff o r‘ v o l a t i l eu 3 2 ’h e r e . #define REG_DISPCNT *((volatile u32*)(MEM_IO+0x0000)) code type description MEM_IO+0x0000 Ad d r e s s MEM_I O=0x04000000, s ot h i si sa d d r e s s0 4 0 0 : 0 0 0 0 (vu32*)0x04000000p o i n t e r Ap o i n t e rt oa nu n s i g n e di n to ft h ev o l a t i l ep e r s u a s i o n( i g n o r et h i sl a s tp a r tf o rn o w) * Bydereferencing t h ep o i n t e r( t h e‘ * ’u n a r yo p e r a t o r ) , wea c c e s st h ec o n t e n t so f ‘ v a r i a b l e ’ (vu32*)0x04000000 t h ep o i n t e r .I de s t , t h ewh o l et h i n gb e c o me su s a b l ea sav a r i a b l e . Sof o ra l l i n t e n t sa n dp u r p o s e s , REG_DI SPCNTi sav a r i a b l ej u s tl i k ea n yo t h e r .Yo uc a na s s i g nv a l u e st oi t , r e a di t sc o n t e n t s , p e r f o r m b i to p e r a t i o n so ni ta n ds of o r t h .Wh i c hi sg o o d , b e c a u s et h a t ' sj u s tt h ewa ywewa n tt ou s et h a tr e g i s t e r . As i mi l a rp r o c e d u r ei sc a r r i e do u tf o rVRAM, o n l yt h i si ss t i l l i ni t sp o i n t e rf o r mi nt h ee n d .Be c a u s eo ft h a t , vid_memwo r k sa sa n array, n o tav a r i a b l e .Ag a i n , t h i si se x a c t l yh o wwewa n tt h i n g s .Pl e a s eb ec a r e f u l wi t ht h ed e f i n i t i o n , t h o u g h : a l l t h ep a r e n t h e s e st h e r e a r erequired!Be c a u s eo ft h eo p e r a t o rp r e c e d e n c eb e t we e nc a s t sa n da r r a y s , l e a v i n go u tt h eo u t e rp a r e n t h e s e sp a i rg i v e sc o mp i l a t i o n e r r o r s . IO register and their bits Th eI Or e g i s t e r s( n o tt ob ec o n f u s e dwi t ht h eCPUr e g i s t e r s )a r eac o l l e c t i o no fs wi t c h e si nt h ef o r mo fb i t f i e l d st h a tc o n t r o l v a r i o u s o p e r a t i o n so ft h eGBA.Th eI Or e g i s t e r sc a nb ef o u n di nt h e0400:0000r a n g eo fme mo r y , a n da r eu s u a l l yc l u mp e di n t owo r d so r h a l f wo r d sa c c o r d i n gt op e r s o n a l p r e f e r e n c e .Tog e ta n y t h i n gd o n e , y o uh a v et os e ts p e c i f i cb i t so ft h eI Or e g i s t e r s .Wh i l ey o uc a nt r yt o r e me mb e ra l l t h en u me r i c a l v a l u e so ft h e s eb i t s , i t ' smo r ec o n v e n i e n tt ou s e#d e f i n e si n s t e a d . Th et o o l b o xh e a d e rl i s t san u mb e ro ft h e#d e f i n e sIu s ef o rREG_DI SPCNT.Th ef u l l l i s tc a nb ef o u n di nvid.ho ft o n c l i b , a n dt h e r e g i s t e ri t s e l fi sd e s c r i b e di nt h ev i d e oc h a p t e r .Fo rn o w, weo n l yn e e dDCNT_MODE3a n dDCNT_BG2 .Th ef o r me rs e t st h ev i d e o mo d et omo d e3 , wh i c hi ss i mp l e s to ft h e3a v a i l a b l eb i t ma pmo d e s , a n dt h el a t t e ra c t i v a t e sb a c k g r o u n d2 .Ou to fat o t a l o ff o u r , b g2i s t h eo n l yo n ea v a i l a b l ei nt h eb i t ma pmo d e sa n dy o uh a v et os wi t c hi to ni fy o uwa n ta n y t h i n gt os h o wu p .Yo uh a v et oa d mi tt h a tt h e s e n a me sa r eal o tmo r ed e s c r i p t i v et h a n0x0003a n d0x0400, r i g h t ? I ' v ea l s oa d d e dal i s to fu s e f u l c o l o rd e f i n e s , e v e nt h o u g hI ' mn o tu s i n gt h e mi nsecond.c.Th e yma yo rma yn o tb eu s e f u l i nt h e f u t u r e , t h o u g h , s oi t ' sg o o dt oh a v et h e ma r o u n d . Cr e a t i n gt h er e g i s t e r#d e f i n e si sp r o b a b l yt h eb i g g e s tp a r to fh e a d e rf i l e s .Asar o u g he s t i ma t e , t h e r ea r e1 0 0r e g i s t e r swi t h1 6b i t se a c h , s ot h a two u l db e1 6 0 0#d e f i n e s .Th a t ' sal o t .Th ee x a c tn u mb e rma yb es ma l l e r , b u ti ti ss t i l l l a r g e .Be c a u s et h en a me so ft h e#d e f i n e si n a n do ft h e ms e l v e sa r e n ' ti mp o r t a n t , y o uc a ne x p e c td i f f e r e n tn a mi n gs c h e me sf o rd i f f e r e n tp e o p l e .Ia mp a r t i a l t omyo wns e to fn a me s , o t h e ro l d e rGBAc o d e r sma yu s ePERN' sn a me sa n dmo r er e c e n to n e sma yu s el i b g b a ' s , wh i c hc o me swi t hd e v k i t ARM.Ta k ey o u r p i c k . Macros and inline functions Yo uc a na l s oc r e a t e#d e f i n e st h a two r kal i t t l el i k ef u n c t i o n s .Th e s ea r ec a l l e dmacros.I ' mn o tu s i n gt h e mh e r e , b u tt h e r ea r ep l e n t yt o b ef o u n di nt o n c l i b ' sh e a d e r s .Li k ea l l #d e f i n e s , ma c r o sa r ep a r to ft h ep r e p r o c e s s o r , n o tt h ec o mp i l e r , wh i c hme a n st h a tt h ed e b u g g e r n e v e rs e e st h e ma n dt h e yc a nh a v ema n yh i d d e ne r r o r si nt h e m.Fo rt h a tr e a s o n , t h e yh a v eb e e nd e p r e c i a t e di nf a v o ro finline functions.Th e yh a v ea l l t h eb e n e f i t so fma c r o s( i . e . , i n t e g r a t e di n t ot h ef u n c t i o n st h a tc a l l t h e ms ot h a tt h e y ' r ef a s t ) , b u ta r es t i l l f u n c t i o n l i k ei ns y n t a xa n dr e s o l v e da tc o mp i l et i me .Atl e a s tt h a t ' st h et h e o r y , i np r a c t i c et h e y ' r en o tquite a ss p e e d ya sma c r o s , b u t o f t e np r e f e r a b l ea n y wa y . On ei n l i n ef u n c t i o nI ' mu s i n gi sm3_plot(), wh i c h , a sy o uma yh a v eg u e s s e d , i su s e dt op l o tp i x e l si nmo d e3 .I nmo d e3 , VRAM i sj u s tama t r i xo f1 6 b i tc o l o r s , s oa l l weh a v et od ot op l o tap i x e l i se n t e rah a l f wo r di nt h er i g h ta r r a ye l e me n t .m3_plot()l o o k s e x a c t l yl i k ean o r ma l f u n c t i o n , b u tb e c a u s et h e‘ static inline’i nf r o n to fi tma k e si ta ni n l i n ef u n c t i o n .No t et h a ti n l i n i n gi so n l ya r e c o mme n d a t i o nt ot h ec o mp i l e r , n o tac o mma n d me n t , a n di to n l ywo r k si fo p t i mi z a t i o n sa r es wi t c h e do n . // Mode 3 plotter as macro ... #define M3_PLOT(x, y, clr) vid_mem[(y)*SCREEN_WIDTH+(x)]=(clr) // and as an inline function static inline void m3_plot(int x, int y, COLOR clr) { vid_mem[y*SCREEN_WIDTH+x]= clr; } Th es e c o n di n l i n ef u n c t i o ni sRGB15(), wh i c hc r e a t e sa1 6 b i tc o l o rf r o ma n yg i v e nr e d , g r e e na n db l u ev a l u e s .Th eGBAu s e s1 6 b i t c o l o r sf o ri t sg r a p h i c s–o ra c t u a l l y1 5 b i tc o l o r si na5 . 5 . 5BGRf o r ma t .Th a t ' s3 2s h a d e so fr e di nt h ef i r s t( l o we s t )5b i t s , 3 2g r e e n si n b i t s5t o9 , a n d3 2b l u e si n1 0 1 4 .Th eRGB15()i n l i n et a k e sc a r eo fa l l t h es h i f t i n ga n dma s k i n gt oma k et h a th a p p e n . 3.2.2. The working code Ma k i n gu s eo ft h ec o n t e n t so ftoolbox.hma k e st h ec o d eo ft h ed e momu c hmo r eu n d e r s t a n d a b l e . Th ef i r s tl i n ei nmain()s e t saf e wb i t si nt h ed i s p l a yc o n t r o l , c o mmo n l yk n o wna sREG_DI SPCNT.Iu s eDCNT_MODE3t os e t t h ev i d e omo d et omo d e3 , a n da c t i v a t eb a c k g r o u n d2wi t hDCNT_BG2.Th i st r a n s l a t e st o0x0403a sb e f o r e , b u tt h i sme t h o dg i v e sa file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 25/331 28-03-13 Tonc : GBA Programming in rot13 b e t t e ri n d i c a t i o no fwh a t ' sh a p p e n i n gt h a ne n t e r i n gt h er a wn u mb e r .Us i n gav a r i a b l e l i k e#d e f i n ei n s t e a do ft h er a wd e r e f e r e n c e d p o i n t e ri sa l s op r e f e r a b l e ;e s p e c i a l l ya st h el a t t e ri ss u r et owi go u tp e o p l en e wt oC. Soh o wd oIk n o wwh a tb i td o e swh a tt oc r e a t et h e#d e f i n e si nt h ef i r s tp l a c e ?Si mp l e , Il o o k e dt h e mu pi nGBATe k , t h ee s s e n t i a l r e f e r e n c et oGBAp r o g r a mmi n g .Fo re v e r yI Or e g i s t e rIu s ei nt h e s ep a g e sI ' l l g i v ead e s c r i p t i o no ft h eb i t sa n dal i s to f#d e f i n e sa s t h e y ' r ed e f i n e di nt o n c l i b .Th ef o r ma t sf o rt h e s ed e s c r i p t i o n swe r eg i v e ni nt h ep r e f a c e , a n dt h et a b l ef o rREG_DI SPCNTc a nb ef o u n d i nt h ev i d e oc h a p t e r . Ac t u a l l yp l o t t i n gt h ep i x e l si sn o wd o n ewi t ht h ei n l i n ef u n c t i o nm3_plot(), wh i c hi sf o r ma t t e dmu c ht h es a mewa ya se v e r yk i n d o fp i x e l p l o t t e ri ne x i s t e n c e : 2c o o r d i n a t e sa n dt h ec o l o r .Mu c hb e t t e rt h a nr a wme mo r ya c c e s s , e v e nt h o u g hi two r k se x a c t l yt h es a me wa y .Th ec o l o r st h e ms e l v e sa r en o wc r e a t e dwi t ha ni n l i n et o o : RGB15t a k e s3n u mb e r sf o rt h er e d , g r e e na n db l u ec o mp o n e n t sa n d t i e st h e mt o g e t h e rt of o r mav a l i d1 6 b i tc o l o r . Fi n a l l y , t h e r ei sa ne n d l e s sl o o pt op r e v e n tt h ep r o g r a mf r o me v e re n d i n g .Bu ta r e n ' te n d l e s sl o o p sb a d ?We l l u s u a l l yy e s , b u tn o t h e r e .Re me mb e rwh a th a p p e n swh e nPCp r o g r a mse n d : c o n t r o l i sk i c k e db a c kt ot h eo p e r a t i n gs y s t e m.We l l , wed o n ' thave a n o p e r a t i n gs y s t e m.Sowh a th a p p e n sa f t e rmain()r e t u r n si su n d e f i n e d .I ti sp o s s i b l et os e ewh a th a p p e n sb yl o o k i n ga taf i l ec a l l e d ctrs0.S, wh i c hc o me swi t hy o u rd e v k i t , b u tt h a t ' sn o tat h i n gf o rb e g i n n e r ss oa tt h emo me n tmya d v i c ei st os i mp l yn o tl e ti th a p p e n . Er g o , e n d l e s sl o o p .Fo rt h er e c o r d , t h e r ea r eb e t t e rwa y so fs t o p p i n gGBAp r o g r a ms , b u tt h i so n e ' st h ee a s i e s t .An dn o wwe ' v er e a c h e d t h ee n do ft h ed e mo . Better, no? An dt h a ti swh a tp r o p e rc o d el o o k sl i k e .Asab a s i cr u l e , t r yt oa v o i dr a wn u mb e r s : n o b o d ye x c e p ty o uwi l l k n o wwh a tt h e yme a n , a n d a f t e rawh i l ey o uma yf o r g e tt h e my o u r s e l fa swe l l .Ty p e d e f s( a n de n u msa n ds t r u c t s )c a nwo r kwo n d e r si ns t r u c t u r i n gc o d e , s oc a n s u b r o u t i n e s( f u n c t i o n s , i n l i n ef u n c t i o n sa n dma c r o s ) .Ba s i c a l l y , e v e r yt i mey o un o t i c ey o ua r er e p e a t i n gy o u r s e l f( c o p y &p a s t ec o d i n g ) , i t mi g h tb et i met ot h i n ka b o u tc r e a t i n gs o mes u b st or e p l a c et h a tc o d e . Th e s ea r ej u s taf e wb a s i cg u i d e l i n e s .I fy o un e e dmo r e , y o uc a nf i n ds o memo r eh e r e , f o re x a mp l e .Go o g l ei sy o u rf r i e n d .No w, i f y o u ' v ef o l l o we da n yc l a s s e so nC, y o us h o u l da l r e a d yk n o wt h e s et h i n g s .I fn o t , y o uh a v eb e e nc h e a t e d .Bo o k sa n dt u t o r i a l sma y s o me t i me ss k i pt h e s et o p i c s , s oi tma yb en e c e s s a r yt ob r o ws ea r o u n df o rmo r eg u i d e l i n e so np r o g r a mmi n gs t y l e s .Th a t ' sa l l t h e ya r e , b y t h ewa y : guidelines.Wh i l et h er u l e sa r eu s u a l l ys e n s i b l e , t h e r e ' sn on e e dt og e tf a s c i s ta b o u tt h e m.So me t i me st h er u l e swo n ' tq u i t e wo r kf o ry o u rs i t u a t i o n ;i nt h a tc a s ef e e l f r e et ob r e a kt h e m.Bu tp l e a s ek e e pi nmi n dt h a tt h e s eg u i d e l i n e sh a v eb e e nwr i t t e nf o ra r e a s o n : mo r eo f t e nt h a nn o ty o uwi l l b e n e f i tf r o mf o l l o wi n gt h e m. 3.2.3. First demo v3? Th e r ea r ema n ywa y st h a tl e a dt oRo me .Yo u ' v ea l r e a d ys e e nt wowa y so fc o d i n gt h a te s s e n t i a l l yd ot h es a met h i n g , t h o u g ho n ewa s e a s i l ys u p e r i o r .Bu ts o me t i me st h i n g sa r e n ' ts oc l e a rc u t .I nma n yc a s e s , t h e r ea r ean u mb e ro fe q u a l l yv a l i dwa y so fp r o g r a mmi n g .Th e o b v i o u se x a mp l ei st h en a me sy o ug i v ey o u ri d e n t i f i e r s .Noo n e ' sf o r c i n gy o ut oap a r t i c u l a rs e to fn a me sb e c a u s ei t ' sn o tt h en a me st h a t a r ei mp o r t a n t , i t ' swh a tt h e ys t a n df o r .An o t h e rp o i n to fc o n t e n t i o ni swh e t h e ry o uu s ema c r o s , f u n c t i o n s , a r r a y so rwh a tn o tf o rd e a l i n g wi t ht h eme mo r yma p .I nmo s tc a s e s , t h e r e ' sn od i f f e r e n c ei nt h ec o mp i l e dc o d e . Th ec o d eb e l o ws h o wsy e ta n o t h e rwa yo fp l o t t i n gt h e3p i x e l s .I nt h i sc a s e , Ia mu s i n gt h ec o l o r#d e f i n e sr a t h e rt h a nt h eRGB i n l i n e , b u tmo r ei mp o r t a n t l yI ' mu s i n ga na r r a yt y p e d e fM3LINEwi t hwh i c hIc a nma pVRAM a sama t r i xs ot h a te a c hp i x e l i s r e p r e s e n t e db yama t r i xe l e me n t .Ye s , y o uc a nd ot h a t , a n di ns o mewa yi t ' se v e nb e t t e rt h a nu s i n ga ni n l i n eo rma c r ob e c a u s ey o u ' r en o t l i mi t e dt oj u s ts e t t i n gp i x e l s ;g e t t i n g , ma s k i n ga n dwh a tn o ta r ea l l p e r f e c t l yp o s s i b l ewi t hama t r i x , b u ti fy o uwe r et og ot h es u b r o u t i n e wa y , y o u ' dh a v et oc r e a t emo r ef o re a c ht y p eo fa c t i o n . Asy o uc a ns e e , t h e r e ' sa l l k i n d so fwa y so fg e t t i n gs o me t h i n gd o n e , a n ds o mea r emo r ep r a c t i c a l t h a no t h e r s .Wh i c ho n ei s a p p r o p r i a t ef o ry o u rs i t u a t i o ni sp r e t t ymu c hu pt oy o u ;i t ' sj u s tp a r to fs o f t wa r ed e s i g n . #include "toolbox.h" // extra stuff, also in tonc_video.h #define M3_WIDTH SCREEN_WIDTH // typedef for a whole mode3 line typedef COLOR M3LINE[M3_WIDTH]; // m3_mem is a matrix; m3_mem[y][x] is pixel (x,y) #define m3_mem ((M3LINE*)MEM_VRAM) int main() { REG_DISPCNT= DCNT_MODE3 | DCNT_BG2; m3_mem[80][120]= CLR_RED; m3_mem[80][136]= CLR_LIME; m3_mem[96][120]= CLR_BLUE; while(1); return 0; } 3.3. General notes on GBA programming file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 26/331 28-03-13 Tonc : GBA Programming in rot13 Co n s o l ep r o g r a mmi n gi ss u b s t a n t i a l l yd i f f e r e n tf r o mPCp r o g r a mmi n g , e s p e c i a l l yf o rs o me t h i n gl i k et h eGBA.Th e r ei sn oo p e r a t i n g s y s t e m, n oc o mp l e xAPIt ol e a r n , i t ' sj u s ty o ua g a i n s tt h eme mo r y .Yo un e e dt oh a v ei n t i ma t ek n o wl e d g eo ft h eGBAme mo r ys e c t i o n s t oma k et h i n g swo r k , a n dg o o dp o i n t e ra n db i t o p e r a t i o ns k i l l sa r eamu s t .Al s or e me mb e rt h a ty o ud o n ' th a v ea2 GHzCPU, h a l fag i go f RAM a n daGPUt h a tc a nd oag a z i l l i o np o l y g o n sp e rs e c o n d .I t ' sj u s ta1 6MHzCPUwi t h9 6 k Bv i d e ome mo r y .An dno f l o a t i n gp o i n t s u p p o r to re v e nh a r d wa r ed i v i s i o n .Th e s ea r ea l l t h i n g sy o un e e dt ob ea wa r eo fwh e nd e s i g n i n gy o u rGBAs o f t wa r e . An o t h e rt h i n gt h a ty o un e e dt or e me mb e ri st h a tt h eGBAh a sat e n d e n c yt od ot h i n g sj u s tat i n yb i td i f f e r e n tt h a ny o uma ye x p e c t .Th e p r i ma r ye x a mp l eo ft h i si st h ema t r i xu s e df o ra f f i n et r a n s f o r ma t i o n sl i k er o t a t i o na n ds c a l i n g .Al l o ft h ep o p u l a rt u t o r i a l sg i v et h ewr o n g ma t r i xf o rar o t a t i o n s c a l et r a n s f o r ma t i o n , e v e nt h o u g ht h er e f e r e n c ed o c u me n t sg i v et h ec o r r e c td e s c r i p t i o no fe a c he l e me n t .Ot h e r g o o de x a mp l e sa r et h ee n dr e s u l to ft r y i n gt owr i t eas i n g l eb y t et oVRAM, t h ef a c tt h a tb i t sf o rk e y s t a t e sa r ea c t u a l l yset wh e nt h e b u t t o n ' su n p r e s s e di n s t e a do ft h eo t h e rwa ya r o u n d , o rwh a tt h et i me rr e g i s t e rREG_TMx Dreally d o e s . I ' v et r i e dt ob ec o mp l e t ei nmye x p l a n a t i o n so fa l l t h e s et h i n g s , b u tI ' mp o s i t i v eI ' v emi s s e dat h i n go rt wo .I fy o ue n c o u n t e rp r o b l e ms , y o u ' r ep r o b a b l yn o tt h ef i r s to n e .Th e r ea r ep l e n t yo fFAQsa n df o r u mswh e r ey o uc a nf i n dt h es o l u t i o n .I ft h a tf a i l s , i tn e v e rh u r t st o a s k .I fa n yo fmyi n f o r ma t i o ni si n c o r r e c to ri n c o mp l e t e , p l e a s ed o n ' th e s i t a t et ot e l l me . 3.3.1. GBA Good/bad practices Fo rs o mer e a s o n , t h e r ea r eal o to fb a dp r o g r a mmi n gp r a c t i c e sa mo n gt h eGBAd e v e l o p me n tc o mmu n i t y .Th ema i nr e a s o nf o rt h i si s p r o b a b l yt h a tp e o p l ej u s tc o p y p a s t ef r o mt u t o r i a l c o d e , mo s to fwh i c hu s et h e s ep r a c t i c e s .He r e ' sas h o r tl i s to ft h i n g st oa v o i d , a n d t h i n g st oa d o p t . Don't believe everything you read.Bo t t o ml i n e : p e o p l ema k emi s t a k e s .So me t i me s , t h ei n f o r ma t i o nt h a ti sg i v e ni s i n c o r r e c to ri n c o mp l e t e .So me t i me st h ec o d ed o e s n ' two r k ;s o me t i me si td o e s , b u ti t ' si n e f f i c i e n to ri n c o n s i s t e n to rj u s tc o n t a i n s p r a c t i c e st h a twi l l c o meb a c kt ob i t ey o ul a t e ro n .Th i si st r u ef o rmo s t( i fn o ta l l )o l d e rt u t o r i a l s .Do n ' ta u t o ma t i c a l l ya s s u me y o u ' r ed o i n gi twr o n g : t h e r ei sac h a n c ei t ' st h es o u r c ema t e r i a l . Un f o r t u n a t e l y , i fy o u ' r en e wt op r o g r a mmi n gy o umi g h tn o tr e c o g n i z et h eb a da n da d o p tt h es t a n d a r d se x h i b i t e db ys o me s o u r c e s .Do not learn C programming from GBA tutorials! I ' da l mo s te x t e n tt h a ts u g g e s t i o nt oo n l i n et u t o r i a l si ng e n e r a l , e s p e c i a l l ys h o r to n e s .Bo o k sa r eu s u a l l ymo r ea c c u r a t ea n dp r o v i d eab e t t e ri n s i g h ti n t ot h ema t e r i a l .( Bu ta g a i n , n o ta l wa y s . ) RTFAQ / RTFR.Re a dt h eg b a d e vf o r u mFAQ.Sh o u l dg owi t h o u ts a y i n g .I tc o v e r sal o to fc o mmo np r o b l e ms .Ad d i t i o n a l l y , r e a dt h ef u c k i nr e f e r e n c e , b ywh i c hIme a nGBATe k , wh i c hc o v e r sj u s ta b o u te v e r y t h i n g . Makefiles are good.Ma n yt u t o r i a l su s eb a t c h f i l e sf o rb u i l d i n gp r o j e c t s .Th i si sav e r ye a s yme t h o d , Ia g r e e , b u ti nt h el o n g r u n , i t ' sv e r yi n e f f i c i e n t , Wi n d o wso n l ya n di sp r o n et oma i n t a i n a b i l i t yp r o b l e ms .Ma k e f i l e sa r eb e t t e rf o rRe a l Wo r l dp r o j e c t s , e v e nt h o u g ht h e r ema yb eah u r d l es e t t i n gt h e mu pi n i t i a l l y .Fo r t u n a t e l y , y o ud o n ' th a v et owo r r ya b o u ti tt h a tmu c h , b e c a u s e De v k i t Pr oc o me swi t hatemplate makefile/project ( s e e${DEVKITPRO}/examples/gba/template)wh e r ea l l y o u n e e dt od oi ss a yi nwh i c hd i r e c t o r i e st h es o u r c e / h e a d e r / d a t af i l e sa r ek e p t .Th ema k e f i l e sIu s ef o rt h ea d v a n c e da n dl a b p r o j e c t sa r ea na d a p t a t i o no ft h e s e . Thumb-code is good.Th es t a n d a r ds e c t i o n sf o rc o d e( ROM a n dEWRAM)h a v e1 6 b i tb u s e s .ARM i n s t r u c t i o n swi l l c l o g t h eb u sa n dc a ns e r i o u s l ys l o wd o wnp e r f o r ma n c e .Th u mbi n s t r u c t i o n sf i tb e t t e rh e r e .Th u mbc o d ei so f t e ns ma l l e rt o o .No t e t h a tb e c a u s eo ft h e3 2 b i tb u so fI WRAM, t h e r ei sn op e n a l t yf o rARM c o d et h e r e . Enabling interworking, optimizations and warnings are good. I n t e r wo r k i n g( -mthumb-interwork)a l l o wsy o ut o u s es wi t c hb e t we e nARM a n dTh u mbc o d e ;y o uma ywa n tt h i si fy o uh a v eaf e wh i g h p e r f o r ma n c er o u t i n e si n ARM/ I WRAM t h a ty o uwa n tt oc a l l f r o mROM c o d e .Op t i mi z a t i o n s( -O#)ma k eGCCn o tb ea ni d i o twh e nc o mp i l i n gCi n t o ma c h i n ec o d e( I ' ms e r i o u s : wi t h o u tt h e mt h eo u t p u ti sa t t r o c i o u s l yb a di ne v e r ywa y ) .I tp r o d u c e sf a s t e rc o d e , a n du s u a l l y s ma l l e ra swe l l .Wa r n i n g s-Walls h o u l db ee n a b l e db e c a u s ey o uwill d os t u p i dt h i n g st h a twi l l p r o d u c ec o mp i l a b l eo u t p u t , b u t wo n ' td owh a ty o ue x p e c t e d .Wa r n i n g sa r er e mi n d e r st h a ts o me t h i n gf u n k yma yb eg o i n go n . 32bit variables are good.Ev e r yCPUh a sa‘ n a t i v e ’d a t a t y p e , a l s ok n o wna st h eword, o ri nCs p e a k , t h eint.Ge n e r a l l y s p e a k i n g , t h eCPUi sb e t t e re q u i p p e dt od e a l wi t ht h a td a t a t y p et h a na n yo t h e r .Th eGBAi sc a l l e da3 2 b i tma c h i n eb e c a u s e t h eCPU' sn a t i v ed a t a t y p ei s3 2 b i t .Th ei n s t r u c t i o ns e t sa r eo p t i mi s e df o rwo r d s i z e dc h u n k s .I tlikes wo r d s , s oy o u ' db e t t e r f e e di two r d su n l e s sy o uh a v en oo t h e rc h o i c e . I nav e r yr e a l wa y , t h e3 2 b i ti n t e g e ri st h eonly d a t a t y p et h eGBAh a s .Th er e s ta r ee s s e n t i a l l ye mu l a t e d , wh i c hc a r r i e sa s ma l l p e r f o r ma n c ep e n a t l y( 2e x t r as h i f ti n s t r u c t i o n sf o rb y t e sa n dh a l f wo r d s ) .Donot u s eu8o ru16f o rl o o p i n d i c e sf o r e x a mp l e , d o i n gs oc a nc u tt h es p e e do fal o o pin half!( Th eb e l i e ft h a tu s i n gs ma l l e rt y p e sme a n sl o we rme mo r y u s eo n l y h o l d sf o ra g g r e g a t e sa n dma y b eg l o b a l s ;f o rl o c a l v a r i a b l e si ta c t u a l l ycosts me mo r y ) .Li k e wi s e , i fy o uh a v eme mo r yt oc o p y o rf i l l , u s i n gwo r d sc a nb ea b o u tt wi c ea sf a s ta sh a l f wo r d s .J u s tb ec a r e f u l wh e nc a s t i n g , b e c a u s ea nARM CPUi sv e r y p i c k ywh e ni tc o me st oa l i g n me n t . Data in header files is bad, very bad.I ' l l g oi nal i t t l ed e t a i l a b o u ti twh e nt a l k i n ga b o u td a t a .An ds e ea l s oh e r ea n dh e r e . Th o s ea r ep o i n t swh e r eo t h e rGBAt u t o r i a l so f t e ne r r .I t ' sn o ta ne x c l u s i v el i s t , b u tt h ema i np o i n t sa r et h e r eIt h i n k .Th e r ea r ea l s oa f e wt h i n g so n( C)p r o g r a mmi n gi ng e n e r a l t h a tI ' dl i k et ome n t i o nh e r e . Know the language; know the system.I ts h o u l dg owi t h o u ts a y i n gt h a ti fy o u ' r ep r o g r a mmi n gi nac e r t a i nl a n g u a g eo ro na c e r t a i ns y s t e m, y o us h o u l dk n o wal i t t l e( a n dp r e f e r a b l yal o t )a b o u tb o t h .Ho we v e r , Ih a v es e e nag o o dd e a l o fc o d et h a twa s p r o b l e ma t i cs i mp l yb e c a u s et h ea u t h o ra p p a r e n t l yd i d n ' tk n o wmu c ha b o u te i t h e r .AsIs a i di nt h eb e g i n n i n go ft h i ss e c t i o n , t h e GBAh a saf e wi n t e r e s t i n gq u i r k st h a ty o un e e dt ok n o wa b o u twh e np r o g r a mmi n gf o ri t .Th a t , o fc o u r s e , i swh a tTo n ci sa l l a b o u t .So met h i n g ss t e mf r o ml a c ko fCs k i l l s–t h e‘ i n t ’ t h i n gi sa ne x a mp l eo ft h i s .An o t h e rvery c o mmo np r o b l e mi sc o r r e c t me mo r ya n dp o i n t e ru s e , s o me t h i n gt h a tIwi l l c o v e ral i t t l el a t e ra n da l s oi nt h es e c t i o no nd a t a .Wi t hC, y o uh a v ed i f f e r e n t file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 27/331 28-03-13 Tonc : GBA Programming in rot13 k i n d so fd a t a t y p e s , p o i n t e r s , t h ep r e p r o c e s s o ra n db i t o p e r a t o r sa ty o u rd i s p o s a l .Le a r nwh a tt h e yd oa n dh o wt ou s et h e m e f f e c t i v e l y . Think first, code later.Don't o p e nu pa ne d i t o r , t y p es o mec o d ea n dh o p ei two r k sc o r r e c t l y .I two n ' t .Ho wc a ni t , i fy o u h a v e n ' te v e nd e f i n e dwh a t‘ c o r r e c t l y ’me a n s ?Th i n ko fwh a ty o uwa n tt od of i r s t , t h e nwh a ty o un e e dt og e ti td o n ea n dthen t r yt oi mp l e me n ti t . Al o to fp r o g r a mmi n g( f o rmea n y wa y )i sn o td o n ei nat e x te d i t o ra ta l l .Fo re x a mp l e , f o ra n y t h i n gi n v o l v i n gma t h( wh i c h c a ni n c l u d eg r a p h i c sa swe l l ) , i t ' sb e t t e rt oma k ead i a g r a mo ft h es i t u a t i o n .Ih a v ep a g e so fd i a g r a msa n de q u a t i o n sf o rt h e a f f i n et r a n s f o r ma t i o na n dmo d e7 , j u s tt os e ewh a twh a tg o i n go n .Pe na n dp a p e ra r ey o u rf r i e n d sh e r e . Learn to generalize and simplify.Th i si sa c t u a l l yn o ta b o u tp r o g r a mmi n g , b u tp r o b l e ms o l v i n gi ng e n e r a l .Sp e c i f i cp r o b l e ms a r eo f t e ns p e c i a l c a s e so fmo r eg e n e r a l p r o b l e ms .Fo re x a mp l e , 2 Dma t hi sas u b s e to fmu l t i d i me n s i o n a l ma t h ;v e c t o ra n a l y s i s a n dt r a n s f o r ma t i o n ss u c ha sr o t a t i o n sa n ds c a l i n ga r ep a r t so fl i n e a ra l g e b r a .I fy o uk n o wt h eg e n e r a l s o l u t i o n , y o uc a na l wa y s ( we l l , often, a ta n yr a t e )wo r kd o wnt ot h es p e c i f i cc a s e .Ho we v e r , wh a ti so f t e nt a u g h t( i ns c h o o l , b u ti nu n i v e r s i t i e sa swe l l ) a r et h es p e c i f i cs o l u t i o n s , n o tt h eg e n e r a l o n e s .Wh i l eu s i n gt h es p e c i a l c a s es o l u t i o n sa r eo f t e nf a s t e ri nu s e , t h e ywo n ' two r k a ta l l i ft h ec a s ei sjust al i t t l ed i f f e r e n tt h a nt h ee x a mp l ei nt h eb o o k .I fy o u ' dl e a r n e dt h eg e n e r a l s o l u t i o n–b e t t e ry e t , h o wt o a r r i v ea tt h eg e n e r a l s o l u t i o n–y o u ' ds t a n damu c hb e t t e rc h a n g eo fs o l v i n gt h et a s ka th a n d . Ar e l a t e di s s u ei ss i mp l i f i c a t i o n .Fo re x a mp l e , i fy o uh a v el o n ge x p r e s s i o n si nas e to fe q u a t i o n s( o ra l g o r i t h ms ) , y o uc a n g r o u pt h e mt ot o g e t h e ru n d e ran e wn a me .Th i sme a n sl e s swr i t i n g , l e s swr i t i n ga n dal o we rr i s ko fma k i n gami s t a k e s o me wh e r e . Learn basic optimization strategies.Byt h i sId o n ' tme a nt h a ty o us h o u l dk n o we v e r yt r i c ki nt h eb o o k , b u tt h e r ea r eaf e w t h i n g st h a ty o uc a nu s ei nwr i t i n gc o d et h a tc a ns p e e dt h i n g su p( s o me t i me se v e ns i g n i f i c a n t l y )wi t h o u tc o s tt or e a d b i l i t ya n d ma i n t a i n a b i l i t y .I nf a c t , s o me t i me st h ec o d ea c t u a l l yb e c o me se a s i e rt or e a db e c a u s eo fi t .Af e we x a mp l e s : Use a better algorithm.Ok a y , s ot h i so n ema yn o ta l wa y sb es i mp l e , b u ti t ' ss t i l l v e r yt r u e . Use ints.Th einti sl o o s e l yd e f i n e da st h en a t i v ed a t a t y p e .Pr o c e s s o r st e n dt op e r f o r mb e t t e rwh e nt h e yd e a l wi t h t h e i rn a t i v ed a t a t y p e . Use temporary variables for common expressions.I fy o uu s eal o n ge x p r e s s i o nmo r et h a naf e wt i me s , c o n s i d e rd u mp i n gi ti nat e mp .Th i sme a n sl e s swr i t i n gf o ry o u , a n dl e s sr e a d i n gf o re v e r y o n e .I tc a na l s oma k ey o u r c o d ef a s t e rb e c a u s ey o ud o n ' tn e e dt oe v a l u a t et h ee n t i r ee x p r e s s i o na l l t h et i me .Th i si se s p e c i a l l yt r u ef o rg l o b a l v a r i a b l e s , wh i c hh a v et ob er e l o a d e da f t e re a c hf u n c t i o n c a l l b e c a u s et h ev a l u e sma yh a v ec h a n g e d . Use pointers.Po i n t e r sh a v et h er e p u t a t i o no fb e i n gd a n g e r o u s , b u tt h e y ' r eavery p o we r f u l t o o l i fu s e dc o r r e c t l y . Po i n t e ra r i t h me t i ci su s u a l l yf a s t e rt h a ni n d e x i n gb e c a u s ei t ' sc l o s e rt oh a r d wa r e , a n db ya s s i g n i n gt e mpp o i n t e r st o d e e p l yn e s t e ds t r u c t u r ee x p r e s s i o n s( s e ea b o v e ) , y o uc a ng r a t l yr e d u c et h el e n g t ho ft h ee x p r e s s i o n s , wh i c hma k e s t h i n g se a s i e ro nt h ec o mp i l e ra n dt h er e a d e ra l i k e . Precalculate. Th i si sr e l a t e dt ot h ep r e v i o u st wop o i n t s .I fy o uh a v eal o o pi nwh i c ht h i n g sd o n ' td e p e n do nt h el o o p v a r i a b l e , p r e c a l c u l a t et h a tp a r tb e f o r et h el o o p .Al s o , t oa v o i d( c o mp l e x )c a l c u l a t i o n s , y o uc o u l dd u mpt h ed a t ai na Lo o k u pTa b l ea n ds i mp l yg r a bav a l u ef r o mt h e r e . Avoid branches.Th i n g st h a tr e d i r e c tp r o g r a mf l o w( i f s , l o o p s , s wi t c h e s )g e n e r a l l yc o s tmo r et h a no t h e ro p e r a t i o n s s u c ha sa r i t h me t i c .So me t i me si t ' sp o s s i b l et oe f f e c t i v e l yd ot h eb r a n c hwi t ha r i t h me t i c( f o re x a mp l e , (int)x>>1 g i v e s−1o r0 , d e p e n d i n go nt h es i g no fx) Th e r ea r ema n ymo r eo p t i mi z a t i o nt e c h n i q u e s , o fc o u r s e .Wi k i p e d i ah a san i c eo v e r v i e w, a n dy o uc a nf i n dp a g e sd i s c u s s i n g p a r t i c u l a rt e c h n i q u e sh e r e [b0rked] a n dt h e r e .So meo ft h e s et e c h n i q u e swi l l b ed o n eb yt h ec o mp i l e ra n y wa y , b u tn o ta l wa y s . Learn to optimize later.Al s ok n o wna s“ p r e ma t u r eo p t i mi z a t i o ni st h er o o to fa l l e v i l ” .Op t i mi z a t i o ns h o u l db ed o n ei nt h e f i n a l s t a g e s , wh e nmo s tc o d ei si np a c ea n dy o uc a na c t u a l l yt e l l wh e r eo p t i mi z a t i o ni sn e c e s s a r y( i fi t ' sn e c e s s a r ya ta l l ) . Ho we v e r , t h i sd o e snot me a ny o us h o u l da c t u a l l ys t r i v ef o rt h es l o we s ts o l u t i o ni nt h ee a r l yp h a s e s .Of t e nt h e r ei sac l e a n e r a n d / o rf a s t e r( s o me t i me se v e nmuch f a s t e r )a l g o r i t h mt h e nt h et r i v i a l o n e , wh i c hwi l l c o met oy o uwi t hj u s tas ma l l a mo u n to f t h o u g h t .Th i si s n ' to p t i mi z a t i o n , i t ' ss i mp l yama t t e ro fn o tb e i n gs t u p i d .Af e wo ft h ep o i n t sme n t i o n e da b o v ef a l l i nt h i s c a t e g o r y . There are always exceptions.Th e r ei sn op r o g r a mmi n gg u i d e l i n et h a td o e s n ' th a v ei t se x c e p t i o n .Ex c e p tma y b et h i so n e . I ' l l l e a v ei ta tt h a tf o rn o w.En t i r eb o o k sh a v eb e e nwr i t t e no nh o wt oc o d ee f f i c i e n t l y .Re s o u r c e sa r ea v a i l a b l eo nt h ewe l l a swe l l : s e a r c hf o rt h i n g sl i k e“ o p t i mi z a t i o n ” , “ c o d i n gs t a n d a r d s ”o r“ c o d i n gg u i d e l i n e s ”s h o u l dg i v ey o umo r et h a ne n o u g h .Al s ol o o ku pDe s i g n Pa t t e r na n dAn t i p a t t e r n .Al s of u na r eb o o k sa n ds i t e st h a ts h o wh o wnot t oc o d e .So me t i me st h e s ea r ee v e nmo r eu s e f u l .Wo r s et h a n Fa i l u r ei so n eo ft h e s e( i np a r t i c u l a rt h ec o d e SODc a t e g o r y ) ;Th ep r o g r a mmi n gs e c t i o no fCo mp u t e rs t u p i d i t i e si sa l s on i c e .I fy o uwa n t t os e ewh yt h eu s eo fg l o b a l v a r i a b l e si sg e n e r a l l yd i s c o u r a g e d , d oas e a r c hf o r‘ a l p h a ’i nt h el a t t e rp a g e . 3.3.2. A few examples of good/bad practices He r ea r eaf e we x a mp l e so fc o d et h a t , wh i l ef u n c t i o n a l , c o u l db ei mp r o v e di nt e r mso fs p e e d , a mo u n to fc o d ea n d / o rma i n t a i n a b i l i t y . Ints versus non-ints Ab o v e , In o t e dt h a tu s eo fn o n i n t sc a nb ep r o b l e ma t i c .Be c a u s et h i sb a dh a b i ti sp a r t i c u l a r l yc o mmo nu n d e rGBAa n dNDSc o d e( b o t h h o me b r e wand c o mme r c i a l ) , I ' dl i k et os h o wy o ua ne x a mp l eo ft h i s . // Force a number into range [min, max> #define CLAMP(x, min, max) \ ( (x)>=(max) ? ((max)-1) : ( ((x)<(min)) ? (min) : (x) ) ) file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 28/331 28-03-13 Tonc : GBA Programming in rot13 // Change brightness of a palette (kinda) (70) void pal_brightness(u16 *pal, u16 size, s8 bright) { u16 ii; s8 r, g, b; for(ii=0; ii<size; ii++) { r= (pal[ii] )&31; g= (pal[ii] >>5)&31; b= (pal[ii]>>10)&31; r += bright; g += bright; b += bright; r= CLAMP(r, 0, 32); g= CLAMP(g, 0, 32); b= CLAMP(b, 0, 32); pal[ii]= r |(g<<5) | (b<<10); } } Th i sr o u t i n eb r i g h t e n so rd a r k e n sap a l e t t eb ya d d i n gab r i g h t n e s s f a c t o rt ot h ec o l o rc o mp o n e n t s , e a c ho fwh i c hi st h e nc l a mp e dt ot h e r a n g e[ 0 , 3 1 t oa v o i df u n k ye r r o r s .Th eb a s i ca l g o r i t h mi ss o u n d , e v e nt h ei mp l e me n t a t i o ni s , I MHO, p r e t t yg o o d .Wh a ti s n ' tg o o d , h o we v e ri st h ed a t a t y p e su s e d .Us i n gs8a n du16h e r ea d d sa ne x t r as h i f t p a i rp r a c t i c a l l ye v e r yt i mea n yv a r i a b l ei su s e d !Th el o o p i t s e l fc o mp i l e st oa b o u t9 0Th u mbi n s t r u c t i o n s .I nc o n t r a s t , wh e nu s i n gintsf o re v e r y t h i n ge x c e p tpalt h el o o pi so n l y4 5i n s t r u c t i o n s l o n g .Ofc o u r s et h ei n c r e a s ei ns i z eme a n sa ni n c r e a s ei nt i mea swe l l : t h ei n t o n l yv e r s i o ni s7 8 %f a s t e rt h a nt h eo n eg i v e na b o v e .To r e p e a tt h a t : the code has doubled in size and slowed down by 78% just by using the wrong datatype! I ' l l a d mi tt h a tt h i se x a mp l ei sp a r t i c u l a r l yn a s t yb e c a u s et h e r ei sal o to fa r i t h me t i ci ni t .Mo s tf u n c t i o n swo u l di n c u ras ma l l e rp e n a l t y . Ho we v e r , t h e r ei sn or e a s o nf o rl o s i n gt h a tp e r f o r ma n c ei nt h ef i r s tp l a c e .Th e r ei sn ob e n e f i to fu s i n gs8a n du16;i td o e sn o ti n c r e a s e r e d a b i l i t y–a l l i td o e si sc a u s eb l o a ta n ds l o wd o wn .Use 32-bit variables when you can, the others only when you have to. No w, b e f o r et h i sb e c o me sa n o t h e rg o t oi s s u e , n o n i n t sd oh a v et h e i rp l a c e .Va r i a b l e sc a nb ed i v i d e di n t ot wog r o u p s : wo r k e rv a r i a b l e s ( t h i n g si nr e g i s t e r s )a n dme mo r yv a r i a b l e s .Lo c a l v a r i a b l e sa n df u n c t i o np a r a me t e r sa r ewo r k e rv a r i a b l e s .Th e s es h o u l db e3 2 b i t .I t e ms t h a ta r ei nme mo r y( a r r a y s , g l o b a l s , s t r u c t s , a n dwh a tn o t )c o u l db e n e f i tf r o mb e i n ga ss ma l l a sp o s s i b l e .Ofc o u r s e , me mo r yv a r i a b l e s s t i l l h a v et ob el o a d e di n t or e g i s t e r sb e f o r ey o uc a nd oa n y t h i n gwi t ht h e m.Ane x p l i c i tl o c a l v a r i a b l ema yb eu s e f u l h e r e , b u ti td e p e n d s o nt h ec a s ea th a n d . Pointer problems On eo ft h emo s tc o mmo nmi s t a k e sGBAn e o p h y t e sma k ei smi x i n gu pa r r a y / p o i n t e rs i z e swh e nc o p y i n gd a t a .Da t ai sd a t a , b u ty o uc a n a c c e s si ti nd i f f e r e n twa y s .Fo re x a mp l e , h e r e ' sc o d et h a tc o p i e sb i t ma p d a t af r o ma na r r a yi n t oVRAM. // An array representing a 240x160@16 bitmap, converted // to an array by some graphics conversion tool. const u8 fooBitmap[240*160*2]= { // Maaaaany, many lines of data. } int main() { REG_DISPCNT= DCNT_MODE3 | DCNT_BG2; // Copy 240x160 pixels to VRAM (ORLY?) int ii; for(ii=0; ii<240*160; ii++) vid_mem[ii]= fooBitmap[ii]; return 0; } Th efooBitmapa r r a yr e p r e s e n t ss o meb i t ma p .I no r d e rt od i s p l a yt h a tb i t ma po nt h es c r e e n , y o un e e dt oc o p yi t sd a t ai n t oVRAM. Th a t ' ss i mp l ee n o u g h : weh a v evid_memf r o mb e f o r e , a n dwec a nc o p yf r o mfooBitmapt oVRAM b yc o p y i n ge l e me n t su s i n ga s i mp l ef o r l o o p . Ho we v e r , i t ' sn o tq u i t ea ss i mp l ea st h a t .vid_memi sa nu16a r r a y ;s od e f i n e db e c a u s ei nmo d e3e a c hp i x e l i sa n1 6 b i tc o l o r .Bu t fooBitmapi sab y t e a r r a y : two e l e me n t so ft h i sa r r a yr e p r e s e n tone c o l o r , a n dc o p y i n gb y t e s t o h a l f wo r d sl e a v e st h et o p b y t eo f e a c hp i x e l e mp t y , g i v i n gav e r ys k e we dr e s u l t .Su c has o u r c e d e s t i n a t i o ni si n c r e d i b l yc o mmo n , p a r t l yb e c a u s ep e o p l ed o n ' tk n o wh o w p o i n t e r sa n da r r a y sr e p r e s e n tme mo r y , b u ta l s ob e c a u s et h e yd o n ' tp a ya t t e n t i o nt ot h ed a t a t y p e . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 29/331 28-03-13 Tonc : GBA Programming in rot13 He r e ' sav e r s i o nt h a two u l dwo r k : // An array representing a 240x160@16 bitmap, converted // to an array by some graphics conversion tool. const u8 fooBitmap[240*160*2]= { // Maaaaany, many lines of data. } int main() { REG_DISPCNT= DCNT_MODE3 | DCNT_BG2; u16 *src= (u16*)fooBitmap; // Cast source to u16-array // Copy 240x160 pixels to VRAM (YARLY!) int ii; for(ii=0; ii<240*160; ii++) vid_mem[ii]= src[ii]; return 0; } Bye n s u r i n gt h es o u r c ea n dd e s t i n a t i o n sa r eo ft h es a met y p e , t h ec o p y i n gl e a v e sn og a p s .No t et h a tt h eu n d e r l y i n gd a t ah a s n ' tc h a n g e d –o n l yh o wi t ' su s e d .Th e r ea r ea c t u a l l yal o tmo r et h i n g sy o un e e dt ok n o wa b o u th o wt ou s ed a t aa n dme mo r y , wh i c hwi l l b ec o v e r e di n al a t e rc h a p t e r . Simplification Co n s i d e rt h ef o l l o wi n gf u n c t i o n( b a s i c a l l yt a k e nf r o mt h eRi n k wo r k ss i t eme n t i o n e de a r l i e r ) : int foo(int x) { switch(x) { case 1: return 1; case 2: return 2; case 3: return 3; case 4: return 4; case 5: return 5; case 6: return 6; case 7: return 7; } return 0; } Wh a tt h i sf u n c t i o nd o e si st h i s : i fx i sb e t we e n1a n d7 , r e t u r nt h a tn u mb e r , o t h e r wi s er e t u r n0 .Th et h i n gt on o t ei st h a tt h ec a s e v a l u e a n dt h er e t u r nc o d ea r et h es a me , s oi n s t e a do ft h es wi t c h b l o c ky o uc o u l dh a v ej u s tr e t u r n e dx. int foo(int x) { if(x >= 1 && x <= 7) return x; else return 0; } Si mp l i f i c a t i o n sl i k et h i so f t e np r e s e n tt h e ms e l v e si fy o uj u s tt h i n ka b o u twh a ty o u ' r ed o i n gf o ral i t t l ewh i l e , r a t h e rt h a nj u s te n t e r i n gc o d e . No w, t h i swo u l ds h o u l db er a t h e ro b v i o u s , b u tmo r ed i f f i c u l ts wi t c h b l o c k sc a no f t e nb er e p l a c e sb ys o me t h i n gl i k et h i sa swe l l .Fo r e x a mp l e , i ft h e r ei sas i mp l ema t h e ma t i c a l r e l a t i o nb e t we e nt h ei n p u ta n dt h er e t u r nv a l u e( s o mea d d i t i o no rmu l t i p l i c a t i o n , f o re x a mp l e ) , y o uc a nj u s tu s et h a tr e l a t i o n .Ev e ni ft h e r ei sn o ts u c has i mp l er e l a t i o n , t h e r ec a nb ep o s s i b i l i t i e s .I fy o u ' r er e t u r n i n gc o n s t a n t s , y o u c o u l dp u tt h o s ec o n s t a n t si nat a b l ea n du s ex a sa ni n d e x . Th ea b o v ei sas i mp l i f i c a t i o ni nt e r mso ft h ea l g o r i t h mu s e d .An o t h e rk i n do fs i mp l i f i c a t i o ni si nr e a d a b i l i t y .Ofc o u r s e , e v e r y b o d yh a s t h e i ro wni d e a sa b o u twh a t ' sr e a d a b l e .Pe r s o n a l l y , Ip r e f e rt ok e e pmys t a t e me n t ss h o r t , e s p e c i a l l yi nt h ep l a c ewh e r et h ea c t i o n h a p p e n s .Th en e x tf u n c t i o ni sa ne x a mp l eo fb o u n d i n gc i r c l ec o l l i s i o nd e t e c t i o n .Ba s i c a l l y , y o uh a v et woc i r c l e sa tp o i n t sp1=( x1, y1)a n d p2=( x2, y2)a n dr a d i i r1a n dr2.Th ed i s t a n c eb e t we e nt h e s et wop o i n t sc a nb ec a l c u l a t e dwi t ht h ePy t h a g o r e a nt h e o r e m.I ft h i sd i s t a n c e i ss ma l l e rt h a nt h es u mo ft h et wor a d i i , t h ec i r c l e so v e r l a p .Af u n c t i o nt h a tc h e c k swh e t h e rt h ep l a y e rs p r i t eh i t sa n yo ft h ee n e my s p r i t e sc o u l dl o o ks o me t h i n gl i k et h i s : file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 30/331 28-03-13 Tonc : GBA Programming in rot13 // Some basic structs and a sprite array. // #defines for sprite-indices and amounts omitted. typedef struct { int x, y; } POINT; typedef struct { POINT position; int radius; } TSprite; TSprite gSprites[SPRITE_MAX]; // Collision function. int player_collision() { int ii; for(ii=0; ii<ENEMY_MAX; ii++) { // Test for hit between player and enemy ii if( (gSprites[ENEMY_ID+ii].position.x - gSprites[PLAYER_ID].position.x) * (gSprites[ENEMY_ID+ii].position.x - gSprites[PLAYER_ID].position.x) + (gSprites[ENEMY_ID+ii].position.y - gSprites[PLAYER_ID].position.y) * (gSprites[ENEMY_ID+ii].position.y - gSprites[PLAYER_ID].position.y) < (gSprites[ENEMY_ID+ii].radius + gSprites[PLAYER_ID].radius) * (gSprites[ENEMY_ID+ii].radius + gSprites[PLAYER_ID].radius) ) { return 1; } } // Not hit return 0; } Pe r s o n a l l y , Ih a v eah a r dt i mer e a d i n gwh a ta c t u a l l yg o e so ni n s i d et h ei f s t a t e me n tt h e r e .Be c a u s et h ee x p r e s s i o ni s6l i n e sl o n g , I a c t u a l l yh a v et os i td o wna n dp a r s ewh a ti ta c t u a l l yd o e s , a n dh o p et h a tt h ep a r e n t h e s e sa r ea l l c o r r e c t , e t c .No w, n o t et h a tan u mb e ro f t h i n g sa r eu s e dmu l t i p l et i me sh e r e : t h egSpritesa c c e s s e s( 6 ×f o rt h ep l a y e r , 6 ×f o rt h ee n e my )a n dt h e nt h ep o s i t i o n sa swe l l .Th e s e c a na l l b ea c c e s s e dwi t hl e s sc o d eb yu s i n gp o i n t e r sa n do t h e rl o c a l v a r i a b l e s .Al s o , t h ep l a y e r ' sa t t r i b u t e sa r el o o pi n v a r i a n t( t h e yd o n ' t c h a n g ed u r i n gt h el o o p ) , s ot h e yc a nb el o a d e do u t s i d et h el o o p . int player_collision() { int ii; int r1= gSprites[PLAYER_ID].radius, r2, dx, dy; POINT *pt1= &gSprites[PLAYER_ID].position, *pt2; TSprite *enemy= &gSprites[ENEMY_ID]; for(ii=0; ii<ENEMY_MAX; ii++) { r2= enemy[ii].radius; pt2= &enemy[ii].position; dx= pt2->x - pt1->x; dy= pt2->y - pt1->y; // Test for hit between player and enemy ii if( dx*dx + dy*dy < (r1+r2)*(r1+r2) ) return 1; } // Not hit return 0; } Th e r ema yn o th a v eb e e nar e a l c h a n g ei nt h en u mb e ro fl i n e s , b u tt h el i n e st h e ms e l v e sa r es h o r t e ra n de a s i e rt or e a d .Al s o , i n s t e a do fa 6 l i n eife x p r e s s i o n , i tn o wf i t so nas i n g l el i n ea n dy o uc a na c t u a l l ys e ewh a ti td o e s .Pe r s o n a l l y , I ' dc a l l t h a tawi n . 3.4. Testing your code on a real GBA I fy o u ' r ej u s ts t a r t i n gGBAp r o g r a mmi n g , c h a n c e sa r ey o u ' r eu s i n gt h ee mu l a t o r st h a ta r eo u tt h e r e , a n dwi l l b ec o n t e n twi t ht h o s e . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 31/331 28-03-13 Tonc : GBA Programming in rot13 Ho we v e r , i fy o ul o o kt h r o u g ht h ef o r u msy o u ' l l s e ema n yp e o p l eu r g i n gy o ut ot e s to nh a r d wa r er e g u l a r l y .Th e ya r ea b s o l u t e l yr i g h t . No w, i ti s n ' tt h a tt h ee mu l a t o r sa r eb a d .Ont h ec o n t r a r y , i nf a c t ;t h emo s tp o p u l a re mu l a t o r sh a v et h i n g sl i k et i l e , ma pa n dme mo r y v i e we r st h a ta r ee s s e n t i a l t od e b u g g i n g .Ane mu l a t o rl i k eVBAi sv e r y , v e r yg o o d , b u tn o tq u i t ep e r f e c t .Ta k et h eTo n cd e mo s , f o r e x a mp l e : t h e yr u nt h es a meo nVBAa so nar e a l GBAi na l l c a s e s. . .mo s t l y .Fo ro n et h i n g , t i mi n gi sar e a l i s s u eo nmo s to ft h e m( t h e e x c e p t i o nh e r ei sn o $ g b a , wh i c hI ' v en e v e rs e e no f ft h ema r kb ymo r et h a n2 %, u s u a l l yal o tl e s s ) .Al s o , i naf e wr a r eo c c a s i o n s( l i k ei n cbb_demoa n dwin_demo)t h e r ewe r es ma l l b u ti mp o r t a n td i f f e r e n c e sb e t we e nGBAa n de mu l a t o ro u t p u t s , a n di fy o u ' v en e v e r t e s t e do nt h er e a l t h i n g , y o u ' dn e v e rk n o w. On eo t h e rt h i n gt h a ti sv e r yd i f f e r e n ti st h ec o l o r s .Si n c ei t ' sn o tb a c k l i tt h eGBAs c r e e ni smu c hd a r k e rt h a naPCmo n i t o r .Or ma y b et h a t ' sj u s tmyr o o m;).Al s o , o na ne mu l a t o ry o uh a v et h el u x u r yo fs c a l i n gy o u rv i e w;t h er e a l GBAi sa l wa y s3 "s c r e e n . Th e r e ' swo r l do fd i f f e r e n c e , t r u s tmeo nt h i s .Ta k et h a tfirst.gbae x a mp l eIs h o we da b o v e : t h ep i x e l sa r es ot i n yi t ' sa l mo s t i mp o s s i b l et os e eo nar e a l GBA!Ev e na n8 x 8t i l ei sp r e t t ys ma l l .Al s o , t h eu s eo fak e y b o a r di na ne mui snothing l i k eh o l d i n gar e a l GBAi ny o u rh a n d s . An d , o fc o u r s e , t h ewh o l ei d e ao fc r e a t i n gs o me t h i n gt h a two r k so nac o n s o l eh a sa na i ro fc o o l n e s st h a td e f i e sd e s c r i p t i o n .We l l , a l mo s t a n y wa y .Th ewo r di sp r o g a s m.Sa y si ta l l r e a l l y , d o e s n ' ti t ? 3.4.1. Multiboot & linkers OK, s on o wy o uk n o wy o us h o u l dt e s to nh a r d wa r e , h o wd oy o ud oi t ?Af t e ra l l , y o uc a n ' te x a c t l yp l u gaGBAi n t oy o u rPCl i k eaUSB me mo r ys t i c ko rap r i n t e r ?We l l , y e sy o uc a n. . .wi t ht h er i g h te q u i p me n t .Th et womo s tc o mmo nwa y sa r eamultiboot cable o raflash linker. Flash Card & Linker Af l a s hc a r di saGBAc a r twi t had i f f e r e n c e : i ti sc o mp l e t e l yr e wr i t a b l e .Th e r ea r ean u mb e ro fd i f f e r e n ts e t sa v a i l a b l e : d i f f e r e n ts i z e d c a r t s( 6 4 Mb i tt o1 0 2 4 Mb i t ) , USBo rPa r a l l e l p o r tv e r s i o n s ;s e t st h a tu s eas e p a r a t el i n k e r( wh e r ey o uh a v et ot a k et h ec a r to u to ft h e GBA, wr i t et oi t , a n dr e i n s e r t )o ro n e st h a twr i t ed i r e c t l yt ot h ec a r to rt r a n s f e rt h r o u g ht h emu l t i b o o tp o r t .I d e a l l yy o u ' du s eo n eo ft h e s e . Ho we v e r , t h e yc a nb er a t h e rp r i c y( $ 6 0-$ 2 0 0( a n du p ? ) )a n dg e n e r a l l yo n l ya v a i l a b l et h r o u g ho n l i n es t o r e s , wh i c ha l s ome a n ss h i p p i n g a n dt a x e sa n ds u c h . Multimedia cards As o l u t i o nt h a t ' sb e c o mi n gmo r ea n dmo r ep o p u l a ri su s i n gs t a n d a r dmu l t i me d i ac a r d s( e g .SD, Co mp a c t Fl a s h )a n da na d a p t e rl i k e GBAMPa n dSu p e r Ca r d .Me mo r yc a r d sc a nb ev e r yc h e a p( l i k e$ 1 0 )a n db o u g h ti nmo s te l e c t r o n i c ss t o r e s ;t h ea d a p t e r sa r eg e n e r e a l l y $ 2 5a n du p . Supercard vs waitstates. Th e r ei so n es ma l l t e c h n i c a l yp r o b l e mwi t hSu p e r c a r d s : t h e yu s es l o wme mo r yt h a td o e s n ' ts u p p o r twh a t3 / 1ROM wa i t s t a t e s .Th i si saf a s t e rs e t t i n gt h a nt h ed e f a u l t4 / 2a n da n y t h i n gt h a tu s e st h ef o r me rs i mp l ywo n ' tr u n .Th i ss h o u l d n ' tb ea p r o b l e mwi t hmo s th o me b r e wt h i n g s , b u tah a n d f u l o fb i n a r i e swi l l f a i l a n dy o uwo u l d n ' tb ea b l et oma k eu s eo ft h es p e e d u p y o u r s e l fe i t h e r . Multiboot cable Th eo t h e rwa yi samu l t i b o o tc a b l e .Th i si sac a b l et h a tp l u g si n t ot h eGBAmu l t i b o o tp o r t( l i k emu l t i p l a y e rg a me sd o )a n do n eo ft h ePC p o r t s , u s u a l l yt h ep a r a l l e l p o r t .Th e s ea r eal o tc h e a p e rt h a naf l a s hk i t .Yo uc a ne v e nb u i l do n ey o u r s e l f:)!Yo uc a nf i n dt h e i n s t r u c t i o n sa n dn e c e s s a r ys o f t wa r et ob u i l da nXb o oc o mmu n i c a t i o nc a b l eo nwww. d e v k i t p r o . o r g , wh i c hwo r k sl i k eac h a r m.Ba s i c a l l y a l l y o un e e dt od oi sc o n n e c to n ee n do ft h el i n kc a b l et oama l ep a r a l l e l p o r tc a b l e .I fy o us h o pa r o u n dy o us h o u l db ea b l et og e ta l l y o u n e e df o ra sl i t t l ea s$ 5 . Bu t , l i k ea l wa y s , t h e r e ' sn os u c ht h i n ga saf r e el u n c h .Wh a th a p p e n si namu l t i b o o tg a mei st h a tt h ec o d ei swr i t t e nt oEWRAM. Th a t ' sh o wy o uc a nu s eo n ec a r ti namu l t i p l a y e rg a me .Th emu l t i b o o tc a b l ei st h es a met h i n g , o n l ywi t ht h ePCa st h eh o s t .Th et r o u b l e i st h a tEWRAM i so n l y2 5 6 k bi ns i z e ;y o uwo n ' tb ea b l et of i ta ne n t i r eg a meo ni t .An d , o fc o u r s e , i tr u n sa l wa y st h r o u g hy o u r c o mp u t e r , s ou n l e s sy o uh a v eal a p t o p , f o r g e ta b o u tt a k i n gi to u t s i d et os h o wo f ft oy o u rf r i e n d s . Fig 3.2: e f af l a s hc a r d . Fig 3.3: Supe r Ca r d , c o mpa c tf l a s h v e r s i o n . Fig 3.4: x b o omu l t i b o o tc a b l e . 3.4.2. Compiling for real hardware Th i si sa l mo s tt h es a mea sf o re mu l a t o r s .Th eo n l yr e a l t h i n g sy o uh a v et owo r r ya b o u ta r ea )t h a ty o uc a no n l yu s et h eb i n a r ya f t e rt h e file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 32/331 28-03-13 Tonc : GBA Programming in rot13 objcopyt r e a t me n t , a n db )t h a ty o un e e dt oh a v eav a l i dGBAh e a d e r , wh i c hi tu s u a l l yd o e s n ' t .I ft h ei n t r os c r e e ns h o ws“ Ga me Bo y ” a sn o r ma l , b u tt h e“ Ni n t e n d o ”a tt h eb o t t o mi sg a r b l e d , y o uh a v eab a dh e a d e r .Tog e tav a l i dh e a d e r , u s eap r o g r a mc a l l e d gbafix.exe.Th i si so r i g i n a l l yb yd a r k f a d e r , b u ty o uc a na l s of i n di ta twww. d e v k i t p r o . o r g .Ia l r e a d yme n t i o n e dt h ee x t r as t e p sf o ra mu l t i b o o tg a mee a r l i e r . Fl a s hk i t su s u a l l yc o mewi t hs o f t wa r et h a tc a nt a k ec a r eo fa l l t h i ss t u f ff o ry o u( o rs oI ' mt o l d , Id o n ' th a v eo n e ) .Th eXb o oz i p f i l e a l s oh a sal i t t l ea p pt h a ts e n d sy o u rb i n a r yt ot h eGBA. file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 33/331 28-03-13 Tonc : GBA Programming in rot13 4. Introduction to GBA Graphics Ge n e r a l i n t r o d u c t i o n . Dr a wa n db l a n kp e r i o d s . Co l o r sa n dp a l e t t e s . Bi t ma p s , b a c k g r o u n d sa n ds p r i t e s . Di s p l a yr e g i s t e r s . Vs y n c i n gp a r tI , t h eb u s y wa i tl o o p . 4.1. General introduction Th eGBAh a sa nLCDs c r e e nt h a ti s2 4 0p i x e l swi d e , 1 6 0p i x e l sh i g ha n di sc a p a b l eo fd i s p l a y i n g3 2 7 6 8( 1 5b i t )c o l o r s .Th er e f r e s hr a t e i sj u s ts h yo f6 0f r a me sp e rs e c o n d( 5 9 . 7 3Hz ) .Th eGBAh a s5i n d e p e n d e n tl a y e r st h a tc a nc o n t a i ng r a p h i c s : 4backgrounds a n do n e sprite l a y e ra n di sc a p a b l eo fs o mes p e c i a l e f f e c t st h a ti n c l u d eb l e n d i n gt wol a y e r sa n dmo s a i ca n d , o fc o u r s e , r o t a t i o na n ds c a l i n g . Wh e r e a ss o u n da n dj o y p a df u n c t i o n a l i t yh a v et oma k ed owi t ho n l yaf e wme a s l yr e g i s t e r s , t h ev i d e os y s t e mh a sag r e a td e a l o f me mo r ya ti t sd i s p o s a l ( r e l a t i v e l ys p e a k i n g ) .Ap a r tf r o mamu l t i t u d eo fr e g i s t e r si nI / Ome mo r y , t h e r e ' st h e9 6 k bo fv i d e ome mo r y ( s t a r t i n ga t0600:0000h) , p a l e t t eme mo r y( 0500:0000h)a n dOAM me mo r y( 0700:0000h) . 4.2. Draw and blank periods Ass a i d , t h ee n t i r eGBAs c r e e ni sr e f r e s h e de v e r y6 0 t ho fas e c o n d , b u tt h e r e ' smo r et oi tt h a nt h a t .Af t e ras c a n l i n eh a sb e e nd r a wn ( t h eHDr a wp e r i o d , 2 4 0p i x e l s ) , t h e r ei sap a u s e( HBl a n k , 6 8p i x e l s )b e f o r ei ts t a r t sd r a wi n gt h en e x ts c a n l i n e .Li k e wi s e , a f t e rt h e1 6 0 s c a n l i n e s( VDr a w)i sa6 8s c a n l i n eb l a n k( VBl a n k )b e f o r ei ts t a r t so v e ra g a i n .Toa v o i dt e a r i n g , p o s i t i o n a l d a t ai su s u a l l yu p d a t e da tt h e VBl a n k .Th i si swh ymo s tg a me sr u na t6 0o r3 0f p s .( FYI , s y n c i n ga tt h eVBl a n ki sa l s owh ywei nPALc o u n t r i e so f t e nh a ds l o we r g a me s : PALTVsr u n( r a n )a t5 0 Hz , h e n c eo n l y5 0f p si n s t e a do f6 0 , h e n c ea1 7 %s l o we rg a mei fn o b o d yb o t h e r e dt oa c c o u n tf o ri t . Fe wc o mp a n i e se v e rd i d:() . Bo t ht h eCo wBi t eSp e ca n dGBATe kg i v ey o us o mei n t e r e s t i n gd e t a i l sa b o u tt h et i mi n g so ft h ed i s p l a y .Af u l l s c r e e nr e f r e s ht a k e s e x a c t l y2 8 0 8 9 6c y c l e s , d i v i d e db yt h ec l o c ks p e e dg i v e saf r a me r a t eo f5 9 . 7 3 .Fr o mt h eDr a w/ Bl a n kp e r i o d sg i v e na b o v ey o uc a ns e e t h a tt h e r ea r e4c y c l e sp e rp i x e l , a n d1 2 3 2c y c l e sp e rs c a n l i n e .Yo uc a nf i n das u mma r yo ft i mi n gd e t a i l si nt a b l e4 . 1 . subject length cycles p i x e l 1 4 HDr a w2 4 0 p x 9 6 0 HBl a n k6 8 p x 2 7 2 s c a n l i n eHd r a w+Hb l1 2 3 2 VDr a w1 6 0 * s c a n l i n e1 9 7 1 2 0 Fig 4.1: v d r a w, v b l a n ka n dh b l a n k pe r i o d s . VBl a n k6 8 * s c a n l i n e8 3 7 7 6 r e f r e s h VDr a w+Vb l2 8 0 8 9 6 Table 4.1: Di s pl a yt i mi n gd e t a i l s 4.3. Colors and palettes Th eGBAi sc a p a b l eo fd i s p l a y i n g1 6 b i tc o l o r si na5 . 5 . 5f o r ma t .Th a tme a n s5b i t sf o rr e d , 5f o rg r e e na n d5f o rb l u e ;t h el e f t o v e rb i ti s u n u s e d .Ba s i c a l l y , t h eb i t p a t t e r nl o o k sl i k et h i s : “xbbbbbgggggrrrrr” .Th e r ea r ean u mb e ro fd e f i n e sa n dma c r o si ncolor.h t h a twi l l ma k ed e a l i n gwi t hc o l o re a s i e r . No w, a sf o rp a l e t t e s . . . <rant> Gu y s , t h ewo r dh e r ei s“palette”!On e‘ l ’ , t wo‘ t ’ sa n da n‘ e ’a tt h ee n d .I ti sn o ta“pallet”, wh i c hi s“ al o w, p o r t a b l ep l a t f o r m, u s u a l l yd o u b l e f a c e d , o nwh i c hma t e r i a l sa r es t a c k e df o rs t o r a g eo rt r a n s p o r t a t i o n , a si nawa r e h o u s e ” , n o ri si ta“pallette”, me a n i n g“ a p l a t ep r o t e c t i n gt h ea r mp i t , i nas u i to fa r mo r ” .Th ewo r d“pallete”, i t smo s tc o mmo nv a r i a n t , i s n ' te v e ni nt h ed i c t i o n a r y , t h u sn o te v e n wo r t hc o n s i d e r i n g .I t ' s“ p a l e t t e ” , p e o p l e , “ p a l e t t e ” . </rant> An y h o o , t h eGBAh a st wop a l e t t e s , o n ef o rs p r i t e s( o b j e c t s )a n do n ef o rb a c k g r o u n d s .Bo t hp a l e t t e sc o n t a i n2 5 6e n t r i e so f1 6 b i t c o l o r s( 5 1 2b y t e s , e a c h ) .Th eb a c k g r o u n dp a l e t t es t a r t sa t0500:0000h, i mme d i a t e l yf o l l o we db yt h es p r i t ep a l e t t ea t0500:0200h. Sp r i t e sa n db a c k g r o u n d sc a nu s et h e s ep a l e t t e si nt wowa y s : a sas i n g l ep a l e t t ewi t h2 5 6c o l o r s( 8b i t sp e rp i x e l ) ;o ra s1 6s u b p a l e t t e so r palette banks o f1 6c o l o r s( 4b i t sp e rp i x e l ) . On ef i n a l t h i n ga b o u tp a l e t t e s : i n d e x0i st h etransparency index.I np a l e t t e dmo d e s , p i x e l swi t hav a l u eo f0wi l l b et r a n s p a r e n t . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 34/331 28-03-13 Tonc : GBA Programming in rot13 4.4. Bitmaps, backgrounds and sprites Al l t h i n g sc o n s i d e r e d , t h eGBAk n o ws3t y p e so fg r a p h i c sr e p r e s e n t a t i o n s : bitmaps, tiled backgrounds a n dsprites.Th eb i t ma pa n d t i l e db a c k g r o u n d( a l s os i mp l yk n o wna sb a c k g r o u n d )t y p e sa f f e c th o wt h ewh o l es c r e e ni sb u i l tu pa n da ss u c hc a n n o tb o t hb ea c t i v a t e d a tt h es a met i me . I nb i t ma pmo d e , v i d e ome mo r ywo r k sj u s tl i k eaw×h b i t ma p .Top l o tap i x e l a tl o c a t i o n( x,y) , g ot ol o c a t i o ny*w+x a n df i l l i nt h ec o l o r . No t et h a ty o uc a n n o tb u i l du pas c r e e n f u l l o fi n d i v i d u a l p i x e l se a c hf r a meo nt h eGBA, t h e r ea r es i mp l yt o oma n yo ft h e m. Ti l e db a c k g r o u n d swo r kc o mp l e t e l yd i f f e r e n t .Fi r s t , y o us t o r e8 x 8p i x e l tilesi no n ep a r to fv i d e ome mo r y .Th e n , i na n o t h e rp a r t , y o u b u i l du pat i l e ma p , wh i c hc o n t a i n si n d i c e st h a tt e l l st h eGBAwh i c ht i l e sg oi n t ot h ei ma g ey o us e eo nt h es c r e e n .Tob u i l das c r e e n y o u ' do n l yn e e da3 0 x 2 0ma po fn u mb e r sa n dt h eh a r d wa r et a k e sc a r eo fd r a wi n gt h et i l e st h a tt h e s en u mb e r sp o i n tt o .Th i swa y , y o u can u p d a t ea ne n t i r es c r e e ne a c hf r a me .Th e r ea r ev e r yf e wg a me st h a td on o tr e l yo nt h i sg r a p h i c st y p e . Fi n a l l y , weh a v es p r i t e s .Sp r i t e sa r es ma l l ( 8 x 8t o6 4 x 6 4p i x e l s )g r a p h i c a l o b j e c t st h a tc a nb et r a n s f o r me di n d e p e n d e n t l yf r o me a c h o t h e ra n dc a nb eu s e di nc o n j u n c t i o nwi t he i t h e rb i t ma po rb a c k g r o u n dt y p e s . Prefer tile modes over bitmap modes I na l mo s ta l l t y p e so fg a me s , t h et i l emo d e swi l l b emo r es u i t a b l e .Mo s to t h e rt u t o r i a l sf o c u so nb i t ma pmo d e s , b u tt h a t ' so n l y b e c a u s et h e ya r ee a s i e ro nb e g i n n e r s , n o tb e c a u s eo ft h e i rp r a c t i c a l v a l u ef o rg a me s .Th ev a s tma j o r i t yo fc o mme r c i a l g a me s u s et i l emo d e s ;t h a ts h o u l dt e l l y o us o me t h i n g . Th o s ea r et h et h r e eb a s i cg r a p h i c a l t y p e s , t h o u g ho t h e rc l a s s i f i c a t i o n sa l s os p r i n gt omi n d .Fo re x a mp l e , t h eb i t ma pa n dt i l e db a c k g r o u n d s t y p e s , s i n c et h e y ' r emu t u a l l ye x c l u s i v ea n du s et h ee n t i r es c r e e n , c o n s t i t u t et h ebackgroundt y p e s .Al s o , i ts oh a p p e n st h a tt h et i l e so f t i l e db a c k g r o u n d sa n dt h es p r i t e sh a v et h es a meme mo r yl a y o u t( n a me l y , i ng r o u p so f8 x 8p i x e l t i l e s ) .Th i sma k e st i l e db a c k g r o u n d sa n d s p r i t e st h et i l e d t y p e s . 4.5. Display registers: REG_DISPCNT, REG_DISPSTAT and REG_VCOUNT Th e r ea r et h r e eI / Or e g i s t e r st h a ty o uwi l l e n c o u n t e rwh e nd o i n ga n y t h i n gg r a p h i c a l : t h ed i s p l a yc o n t r o l REG_DISPCNT (0400:0000h), t h ed i s p l a ys t a t u sREG_DISPSTAT (0400:0004h)a n dt h es c a n l i n ec o u n t e rREG_VCOUNT (0400:0006h).Th o s en a me sa r es i mp l yd e f i n e st ot h eme mo r yl o c a t i o n sa n dc a n , i np r i n c i p l e , b ec h o s e na twi l l .Ho we v e r , wewi l l u s et h en a me sa st h e ya p p e a ri nt h ePe r nPr o j e c t , wh i c ha r et h emo s tc o mmo n . Th eREG_DI SPCNTr e g i s t e ri st h ep r i ma r yc o n t r o l o ft h es c r e e n .Th eb i t l a y o u to ft h i sr e g i s t e ra n dt h e i rme a n i n g sc a nb ef o u n di nt h e f o l l o wi n gt a b l e .Th i si st h eg e n e r a l f o r ma tIwi l l u s ef o rr e g i s t e r so rr e g i s t e r l i k es e c t i o n s .Th ed e t a i l so ft h ef o r ma th a v ea l r e a d yb e e n e x p l a i n e di nt h ep r e f a c e . REG_DISPCNT @ 0400:0000h F E D C B A 9 8 7 6 5 4 3 210 OW W1 W0 Obj BG3 BG2 BG1 BG0 FB OM HB PS GB Mode bits name define description 0 2 Mo d e DCNT_MODEx . Se t sv i d e omo d e .0 , 1 , 2a r et i l e dmo d e s ;3 , 4 , 5a r eb i t ma pmo d e s . DCNT_MODE# 3 GB DCNT_GB I ss e ti fc a r t r i d g ei saGBCg a me .Re a d o n l y . 4 PS DCNT_PAGE Pa g es e l e c t .Mo d e s4a n d5c a nu s ep a g ef l i p p i n gf o rs mo o t h e ra n i ma t i o n .Th i sb i t s e l e c t st h ed i s p l a y e dp a g e( a n da l l o wi n gt h eo t h e ro n et ob ed r a wno nwi t h o u t a r t i f a c t s ) . 5 HB DCNT_OAM_HBLAl l o wsa c c e s st oOAM i na nHBl a n k .OAM i sn o r ma l l yl o c k e di nVDr a w.Wi l l r e d u c et h ea mo u n to fs p r i t ep i x e l sr e n d e r e dp e rl i n e . 6 OM DCNT_OBJ _1 D Ob j e c tma p p i n gmo d e .Ti l eme mo r yc a nb es e e na sa3 2 x 3 2ma t r i xo ft i l e s .Wh e n s p r i t e sa r ec o mp o s e do fmu l t i p l et i l e sh i g h , t h i sb i tt e l l swh e t h e rt h en e x tr o wo f t i l e sl i e sb e n e a t ht h ep r e v i o u s , i nc o r r e s p o n d e n c ewi t ht h ema t r i xs t r u c t u r e( 2 D ma p p i n g , OM=0 ) , o rr i g h tn e x tt oi t , s ot h a tme mo r yi sa r r a n g e da sa na r r a yo f s p r i t e s( 1 Dma p p i n gOM=1 ) .Mo r eo nt h i si nt h es p r i t ec h a p t e r . 7 FB DCNT_BLANK Fo r c eas c r e e nb l a n k . 8 B BG0 BG3 , Ob j DCNT_BGx , DCNT_OBJ . DCNT_LAYER# En a b l e sr e n d e r i n go ft h ec o r r e s p o n d i n gb a c k g r o u n da n ds p r i t e s . DF W0 OW DCNT_WI Nx , En a b l e st h eu s eo fwi n d o ws0 , 1a n dOb j e c twi n d o w, r e s p e c t i v e l y .Wi n d o wsc a n DCNT_WI NOBJ b eu s e dt oma s ko u tc e r t a i na r e a s( l i k et h el a mpd i di nZe l d a : LTTP) . Se t t i n gt h ed i s p l a yc o n t r o l i sp r o b a b l yt h ef i r s tt h i n gy o u ' l l b ed o i n g .Fo rs i mp l ed e mo s , y o uc a nj u s ts e ti to n c ea n dl e a v ei ta tt h a t , t h o u g h s wi t c h i n gb e t we e nt h ev i d e o mo d e sc a nh a v es o mei n t e r e s t i n gr e s u l t s . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 35/331 28-03-13 Tonc : GBA Programming in rot13 No wt h eo t h e rt wor e g i s t e r sIme n t i o n e d , REG_DISPSTATa n dREG_VCOUNT.Th el a t t e rt e l l sy o ut h es c a n l i n et h a ti sc u r r e n t l yb e i n g wo r k e do n .No t et h a tt h i sc o u n t e rk e e p sg o i n gi n t ot h eVBl a n ka swe l l , s oi tc o u n t st o2 2 7b e f o r es t a r t i n ga t0a g a i n .Th ef o r me rg i v e s y o ui n f o r ma t i o na b o u tt h eDr a w/ Bl a n ks t a t u sa n di su s e dt os e td i s p l a yi n t e r r u p t s .Yo uc a na l s od os o mer e a l l yc o o l s t u f fwi t ht h e i n t e r r u p t st h a ty o uc a ne n a b l eh e r e .Fo ro n et h i n g , t h eHBl a n ki n t e r r u p ti su s e di nc r e a t i n gMo d e7g r a p h i c s , a n dy o uwa n tt ok n o wh o w t h a two r k s , d o n ' ty o u ? REG_DISPSTAT @ 0400:0004h FEDCBA98 76 VcT 5 4 3 2 1 0 - VcI HbI VbI VcS HbS VbS bits name define description 0 Vb S DSTAT_I N_VBL VBl a n ks t a t u s , r e a do n l y .Wi l l b es e ti n s i d eVBl a n k , c l e a ri nVDr a w. 1 Hb S DSTAT_I N_HBL HBl a n ks t a t u s , r e a do n l y .Wi l l b es e ti n s i d eHBl a n k . 2 Vc S DSTAT_I N_VCT VCo u n tt r i g g e rs t a t u s .Se ti ft h ec u r r e n ts c a n l i n ema t c h e st h es c a n l i n et r i g g e r( REG_VCOUNT==REG_DISPSTAT{ 8 F}) 3 Vb I DSTAT_VBL_I RQVBl a n ki n t e r r u p tr e q u e s t .I fs e t , a ni n t e r r u p twi l l b ef i r e da tVBl a n k . 4 Hb I DSTAT_HBL_I RQHBl a n ki n t e r r u p tr e q u e s t . 5 Vc I DSTAT_VCT_I RQVCo u n ti n t e r r u p tr e q u e s t .Fi r e si n t e r r u p ti fc u r r e n ts c a n l i n ema t c h e st r i g g e rv a l u e . 8 F Vc T DSTAT_VCT# VCo u n tt r i g g e rv a l u e .I ft h ec u r r e n ts c a n l i n ei sa tt h i sv a l u e , b i t2i ss e ta n da n i n t e r r u p ti sf i r e di fr e q u e s t e d . REG_VCOUNT @ 0400:0006h (readonly) FEDCBA98 76543210 - bits Vc name description 0 7 Vc Ve r t i c a l c o u n t .Ra n g ei s[ 0 , 2 2 7 ] 4.6. Vsyncing part I, the busy-wait loop Ass a i d , u s et h eVBl a n ka sat i mi n gme c h a n i s ma n dt ou p d a t et h eg a med a t a .Th i si sc a l l e dvsync ( ve r t i c a l syncr o n i s a t i o n ) .Th e r ea r ea n u mb e ro fwa y st ov s y n c .Th et womo s tc o mmo nme t h o d su s eawh i l el o o pa n dc h e c kREG_VCOUNTo rREG_DISPSTAT.Fo r e x a mp l e , s i n c et h eVBl a n ks t a r t sa ts c a n l i n e1 6 0 , y o uc o u l ds e ewh e nREG_VCOUNTg o e sb e y o n dt h i sv a l u e . #define REG_VCOUNT *(u16*)0x04000006 void vid_vsync() { while(REG_VCOUNT < 160); } Un f o r t u n a t e l y , t h e r ea r eaf e wp r o b l e mswi t ht h i sc o d e .Fi r s to fa l l , i fy o u ' r es i mp l yd o i n ga ne mp t ywhilel o o pt owa i tf o r1 6 0 , t h e c o mp i l e rma yt r yt og e ts ma r t , n o t i c et h a tt h el o o pd o e s n ' tc h a n g eREG_VCOUNTa n dp u ti t sv a l u ei nar e g i s t e rf o re a s yr e f e r e n c e . Si n c et h e r ei sag o o dc h a n c et h a tt h a tv a l u ewi l l b eb e l o w1 6 0a ts o mep o i n t , y o uh a v ean i c el i t t l ei n f i n i t el o o po ny o u rh a n d .Top r e v e n t t h i s , u s et h ek e y wo r dvolatile( s e eregs.h) .Se c o n d , i ns ma l l d e mo ss i mp l ywa i t i n gf o rt h eVBl a n ki s n ' te n o u g h ;y o uma ys t i l l b e i nt h a tVBl a n kwh e ny o uc a l l vid_sync()a g a i n , wh i c hwi l l b eb l a z e dt h r o u g hi mme d i a t e l y .Th a td o e sn o ts y n ct o6 0f p s .Tod ot h i s , y o uf i r s th a v et owa i tu n t i l t h enext VDr a w.Th i sma k e so u rvid_syncl o o kal i t t l el i k et h i s : #define REG_VCOUNT *(vu16*)0x04000006 void vid_vsync() { while(REG_VCOUNT >= 160); while(REG_VCOUNT < 160); } // wait till VDraw // wait till VBlank Th i swi l l a l wa y swa i tu n t i l t h es t a r to ft h en e x tVBl a n ko c c u r s .An dREG_VCOUNTi sn o wvolatile( t h e“ vu16”i stypedefe da s av o l a t i l eu n s i g n e d( 1 6 b i t )s h o r t .I ' l l b eu s i n gal o to ft h i sk i n do fs h o r t h a n d , s og e tu s e dt oi t ) .Th a t ' so n ewa yt od oi t .An o t h e ri s c h e c k i n gt h el a s tb i ti nt h ed i s p l a ys t a t u sr e g i s t e r , REG_DISPSTAT{ 0 } . Sowe ' r ed o n eh e r e , r i g h t ?Er r m. . .n o , n o te x a c t l y .Wh i l ei t ' st r u et h a ty o un o wh a v ea ne a s ywa yt ov s y n c , i t ' sa l s oav e r yp o o ro n e . Wh i l ey o u ' r ei nt h ewh i l el o o p , y o u ' r es t i l l b u r n i n gCPUc y c l e s .Wh i c h , o fc o u r s e , c o s t sb a t t e r yp o we r .An ds i n c ey o u ' r ed o i n ga b s o l u t e l y n o t h i n gi n s i d et h a twh i l e l o o p , y o u ' r en o tj u s tu s i n gi t , y o u ' r ea c t u a l l ywa s t i n gb a t t e r yp o we r .Mo r e o v e r , s i n c ey o uwi l l p r o b a b l yma k eo n l y s ma l l g a me sa tf i r s t , y o u ' l l b ewa s t i n gaLOT o fb a t t e r yp o we r .Th er e c o mme n d e dwa yt ov s y n ci sp u t t i n gt h eCPUi nl o wp o we rmo d e file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 36/331 28-03-13 Tonc : GBA Programming in rot13 wh e ny o u ' r ed o n ea n dt h e nu s ei n t e r r u p t st ob r i n gi tb a c kt ol i f ea g a i n .Yo uc a nr e a da b o u tt h ep r o c e d u r eh e r e , b u ts i n c ey o uh a v et o k n o wh o wt ou s ei n t e r r u p t sa n dBI OSc a l l s , y o umi g h twa n tt owa i tawh i l e . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 37/331 28-03-13 Tonc : GBA Programming in rot13 5. The Bitmap modes (mode 3, 4, 5) I n t r o d u c t i o n . GBAb i t ma pmo d e s , f o rr e a l Pa g ef l i p p i n g . Ond a t aa n dh o wt ou s ei t . Co n c l u s i o n s . 5.1. Introduction I nt h i sc h a p t e r , we ' l l l o o ka tt h eb i t ma pmo d e s .Bi t ma pmo d e sa r eag o o dp l a c et os t a r tb e c a u s et h e r ei sao n et oo n er e l a t i o nb e t we e n t h ec o n t e n t so fme mo r ya n dt h ep i x e l so nt h es c r e e n .Th ee s s e n t i a l so fa l l t h eb i t ma pmo d e swi l l b ed i s c u s s e db r i e f l y , wi t hac l o s e rl o o k a twh a ty o uc a nd oi nmo d e3a sa ne x a mp l e .We ' l l a l s os e eab i to fp a g ef l i p p i n g( mo d e4 ) , wh i c ha l l o wsf o rs mo o t h e ra n i ma t i o n . Th ec h a p t e rwi l l c l o s ewi t has e c t i o no nh o wt od e a l wi t hd a t aa n dc o mp u t e rme mo r yi ng e n e r a l .Be c a u s eGBAp r o g r a mmi n gi sv e r y c l o s et ot h eh a r d wa r e , y o uneed t ok n o wt h e s et h i n g s .I fy o u ' v eb e e np r o g r a mmi n g( i nCo ra s s e mb l y )f o ral o n gt i mea n dh a v ea l r e a d y g a i n e dag o o du n d e r s t a n d i n go nd a t a , d a t a t y p e sa n dme mo r yy o uc a np r o b a b l ys k i pi t ;f o rt h er e s to fy o u , Iwo u l ds t r o n g l yu r g et or e a di t , b e c a u s ei ti sv e r yi mp o r t a n tf o ra l l t h ec h a p t e r st oc o me . 5.1.1. Bitmap 101 I nf i g5 . 1y o uc a nf i n dab i t ma po fo n eo ft h eg a mec h a r a c t e r st h a tma d eNi n t e n d og r e a t .Th i si sp r o b a b l yh o wmo s t p e o p l et h i n ko fb i t ma p s : ag r i do fc o l o r e dp i x e l s .I no r d e rt ou s eb i t ma p si nap r o g r a mwen e e dt ok n o wh o wt h e y ' r e a r r a n g e di nme mo r y .Fo rt h a tweu s ef i g5 . 2( b e l o w) ;t h i si saz o o me do u tv e r s i o no ff i g5 . 1 , wi t hap i x e l g r i d i mp o s e do v e ri ta n ds o men u mb e r s . Ab i t ma pi sl i t t l emo r et h a naw×h ma t r i xo fc o l o r s( o rc o l o r i n d i c e s ) , wh e r ew i st h en u mb e ro fc o l u mn s( t h e Fig 5.1:Li nk wi d t h )a n dh t h en u mb e ro fr o ws( t h eh e i g h t ) .Ap a r t i c u l a rp i x e l c a nb er e f e r r e dt owi t hac o o r d i n a t ep a i r : ( x, y) .By ( 2 4 x 24b i t ma p) . t h ewa y , t h ey a x i so ft h eGBAp o i n t sdown, n o tu p .Sop i x e l ( 0 , 0 )i si nt h et o p l e f tc o r n e r .I nme mo r y , t h el i n e so f t h eb i t ma pa r el a i do u ts e q u e n t i a l l y , s ot h a tt h ef o l l o wi n gr u l eh o l d s : i naw×h b i t ma p , t h ep i x e l ( x, y)i st h e( w×y + x) t hp i x e l .Th i si s t r u ef o ra l l Cma t r i c e s , b yt h ewa y . Fi g5 . 2s h o wsh o wt h i swo r k s .Th i si saw=2 4b yh=2 4b i t ma p , a t8 b p p( 8Bi t sPe rPi x e l ( =1b y t e ) ) .Th en u mb e r si ny e l l o wi n d i c a t e t h eme mo r yl o c a t i o n s ;y o uc a nc o u n tt h e mf o ry o u r s e l fi fy o ud o n ' tb e l i e v eme .Th ef i r s tp i x e l , ( 0 , 0 ) , c a nb ef o u n da tl o c a t i o n0 .Th elast p i x e l o ft h efirst r o w( 2 3 , 0 )i sa tw−1( =2 3i nt h i sc a s e ) .Th ef i r s tp i x e l o ft h es e c o n dr o w( 0 , 1 )i sa tw ( =2 4 )e t c , e t c , t i l l t h el a s tp i x e l a t w×h−1 . Fig 5.2a: z o o mo u to ff i g5 . 1 , wi t hpi x e l o f f s e t s . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm Fig 5.2b: z o o mo u to ff i g5 . 1 , wi t hpi x e l v a l u e s . Ze r oo mi t t e df o r c l a r i t y. Pa l e t t eo nt h el e f t h a n ds i d e . 38/331 28-03-13 Tonc : GBA Programming in rot13 No t e , h o we v e r , t h a twh e ny o uu s ea n o t h e rb i t d e p t h , t h ea d d r e s s e sc h a n g et o o .Fo re x a mp l e , a t1 6 b p p( 2b y t e sp e rp i x e l ) , y o u ' dn e e dt o mu l t i p l yt h ep i x e l n u mb e rb y2 .Oru s ea n o t h e rd a t a t y p ef o ry o u ra r r a y .Th eg e n e r a l f o r mu l ai sl e f ta sa ne x e r c i s ef o rt h er e a d e r . Us u a l l yi t ' sn o ta c t u a l l yt h ewi d t h( i . e . , t h en u mb e ro fp i x e l si nar o w)t h a t ' si mp o r t a n t , b u tt h epitch.Th ep i t c hi sd e f i n e da st h en u mb e r o fb y t e si nas c a n l i n e .Fo r8 b p pi ma g e st h ep i t c ha n dwi d t hwi l l u s u a l l yb et h es a me , b u tf o r , s a y , 1 6 b p pi ma g e s( 2b y t e sp e rp i x e l )t h e p i t c hi st h ewi d t ht i me st wo .Th e r e ' sa n o t h e rc a t c h : me mo r ya l i g n me n t .Al i g n me n twi l l b ec o v e r e di nal a t e rs e c t i o n , b u tt h eu p s h o ti st h a t s y s t e msg e n e r a l l yh a v ea‘ p r e f e r r e d ’t y p es i z ea n dc a nb e t t e rd e a l wi t hd a t ai ft h ea d d r e s s e sa r eamu l t i p l eo ft h a tt y p es i z e .Th i si swh y wi n d o wsBMPs ' s c a n l i n e sa r ea l wa y sa l i g n e dt o3 2 b i tb o u n d a r i e s . 5.2. The GBA bitmap modes Vi d e omo d e s3 , 4a n d5a r et h eb i t ma pmo d e s .Tou s et h e m, p u t3 , 4o r5i nt h el o we s tb i t so fREG_DI SPCNTa n de n a b l eBG2.Yo u ma ywo n d e rwh ywes t a r twi t hmo d e3 , r a t h e rt h a nmo d e0 .Th er e a s o nf o rt h i si st h a tb i t ma p sa r eal o te a s i e rt oc o met ot e r mswi t h t h a nt i l e ma p s .An dt h i si st h eonly r e a s o n .Th et r u t ho ft h ema t t e ri st h a tt h eb i t ma pmo d e sa r ej u s tt o os l o wt ob eu s e df o rmo s t c o n v e n t i o n a l GBAg a me s .Ic a n ' tg i v ea ne x a c tf i g u r e , b u ti fs o me o n et o l dme9 0 %o rmo r eo fGBAg a me su s e dt i l e mo d e sa n dn o t b i t ma pmo d e s , Iwo u l d n ' tb es u r p r i s e d .Th eo n l yt i mewh e nb i t ma pmo d e swo u l db eb e n e f i c i a l wo u l db ee i t h e rf o rv e r ys t a t i cs c r e e n s ( i n t r o d u c t o r yd e mo s )o rv e r yd y n a mi cs c r e e n s( 3 Dg a me sl i k eSt a r f o xo rDo o m) . Th eb i t ma pmo d e sh a v et h ef o l l o wi n gc h a r a c t e r i s t i c s : mode width height bpp size page-flip 3 2 4 0 1 6 0 1 6 1 x1 2 C0 0 hNo 4 2 4 0 1 6 0 8 2 x9 6 0 0 h Ye s 5 1 6 0 1 2 8 1 6 2 xA0 0 0 h Ye s Table 5.1: Bi t ma pmo d ec h a r a c t e r i s t i c s Wh a twi d t h , h e i g h ta n db p pme a ns h o u l db ec l e a rb yn o w;t h es i z et h a tt h eb i t ma pr e q u i r e si ss i mp l ywidth × height × bpp/8.Pa g e f l i p p i n gma yn e e ds o memo r ee x p l a n a t i o n , b u tf i r s twe ' l l l o o ka ts o mee x a mp l e so fmo d e3g r a p h i c s . 5.2.1. Drawing primitives in mode 3 We ' v ea l r e a d ys e e nh o wt op l o tp i x e l s , n o wi t ' st i mef o rs o mel i n e sa n dr e c t a n g l e s .Ho r i z o n t a l l i n e sa r ep i s s e a s y : b e c a u s et h ep i x e l sa r e i na d j a c e n tme mo r y , a l l y o un e e di sas i mp l el o o pf r o mt h es t a r t i n gx t ot h ef i n a l x.Ve r t i c a l l i n e sa r en e a r l ya se a s y : wh i l et h ep i x e l s a r e n ' tr i g h tn e x tt oe a c ho t h e r , t h e yd oh a v eaf i x e do f f s e tb e t we e nt h e m, n a me l yt h ep i t c h .Soa g a i nas i mp l el o o pi sa l l y o un e e d . Re c t a n g l e sa r ee s s e n t i a l l ymu l t i p l eh o r i z o n t a l l i n e s , s ot h o s ea r ee a s ya swe l l . Di a g o n a l l i n e sa r eal i t t l et r i c k i e r , f o ran u mb e ro fr e a s o n s .Di a g o n a l l i n e sh a v eas l o p et h a ti n d i c a t e sh o wma n yh o r i z o n t a l s t e p sy o u n e e dt ot a k eb e f o r emo v i n gt ot h en e x ts c a n l i n e .Th a two u l do n l ywo r ki ft h ea b s o l u t ev a l u ewe r el o we rt h a no n e , o t h e r wi s ey o u ' dg e t g a p sb e t we e np i x e l s .Fo rh i g h e rs l o p e s , y o un e e dt oi n c r e me n tv e r t i c a l l y , a n dp l o th o r i z o n t a l l y . An o t h e rp o i n ti sh o wt oma k et h er o u t i n ef a s te n o u g ht ob eo fr e a l u s e .Fo r t u n a t e l y , t h e s et h i n g sh a v ea l l b e e nf i g u r e do u ti nt h ep a s t a l r e a d y , s owe ' l l j u s tu s et h er e s u l t sh e r e .I nt h i sc a s e , we ' l l u s eaBr e s e n h a mMi d p o i n ta l g o r i t h mf o rt h el i n ed r a wi n g , mo d i f i e dt od e a l wi t hh o r i z o n t a l a n dv e r t i c a l l i n e ss e p a r a t e l y .Wh i l eIc o u l de x p l a i nwh a tt h er o u t i n ed o e se x a c t l y , i ti so u to ft h es c o p eo ft h ec h a p t e r , r e a l l y . Twop o i n t sIh a v ei g n o r e dh e r ea r en o r ma l i z a t i o na n dc l i p p i n g .Normalization me a n sma k i n gs u r et h er o u t i n er u n si nt h er i g h t d i r e c t i o n .Fo re x a mp l e , wh e ni mp l e me n t i n gal i n ed r a wi n gr o u t i n et h a tr u n sf r o mx1t ox2v i aa ni n c r e me n t i n gf o rl o o p , y o u ' db e s tb e s u r et h a tx2i sa c t u a l l yh i g h e rt h a nx1i nt h ef i r s tp l a c e .Clipping me a n sc u t t i n gt h ep r i mi t i v ed o wnt of i ti n s i d et h ev i e wp o r t .Wh i l et h i s i sag o o dt h i n gt od o , wewi l l o mi ti tb e c a u s ei tc a ng e tr e a l l yh a i r yt od oi twe l l . Th ec o d eb e l o wi sa ne x c e r p tf r o mtoolbox.cf r o mt h em3_demoa n dc o n t a i n sf u n c t i o n sf o rd r a wi n gl i n e s , r e c t a n g l e sa n df r a me s o na1 6 b p pc a n v a s , l i k ei nmo d e3a n dmo d e5 .dstBasei st h eb a s e p o i n t e rt ot h ec a n v a sa n ddstPitchi st h ep i t c h .Th er e s to ft h e p a r a me t e r ss h o u l db eo b v i o u s . #include "toolbox.h" //! Draw a line on a 16bpp canvas void bmp16_line(int x1, int y1, int x2, int y2, u32 clr, void *dstBase, uint dstPitch) { int ii, dx, dy, xstep, ystep, dd; u16 *dst= (u16*)(dstBase + y1*dstPitch + x1*2); dstPitch /= 2; file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 39/331 28-03-13 Tonc : GBA Programming in rot13 // --- Normalization --if(x1>x2) { xstep= -1; dx= x1-x2; } else { xstep= +1; dx= x2-x1; } if(y1>y2) { ystep= -dstPitch; else { ystep= +dstPitch; dy= y1-y2; } dy= y2-y1; } // --- Drawing --if(dy == 0) // Horizontal { for(ii=0; ii<=dx; ii++) dst[ii*xstep]= clr; } else if(dx == 0) // Vertical { for(ii=0; ii<=dy; ii++) dst[ii*ystep]= clr; } else if(dx>=dy) // Diagonal, slope <= 1 { dd= 2*dy - dx; for(ii=0; ii<=dx; ii++) { *dst= clr; if(dd >= 0) { dd -= 2*dx; dst += ystep; } dd += 2*dy; dst += xstep; } } else // Diagonal, slope > 1 { dd= 2*dx - dy; for(ii=0; ii<=dy; ii++) { *dst= clr; if(dd >= 0) { dd -= 2*dy; dst += xstep; } dd += 2*dx; dst += ystep; } } } //! Draw a rectangle on a 16bpp canvas void bmp16_rect(int left, int top, int right, int bottom, u32 clr, void *dstBase, uint dstPitch) { int ix, iy; uint width= right-left, height= bottom-top; u16 *dst= (u16*)(dstBase+top*dstPitch + left*2); dstPitch /= 2; // --- Draw --for(iy=0; iy<height; iy++) for(ix=0; ix<width; ix++) dst[iy*dstPitch + ix]= clr; } //! Draw a frame on a 16bpp canvas void bmp16_frame(int left, int top, int right, int bottom, u32 clr, void *dstBase, uint dstPitch) file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 40/331 28-03-13 Tonc : GBA Programming in rot13 { // Frame is RB exclusive right--; bottom--; bmp16_line(left, top, right, top, clr, dstBase, dstPitch); bmp16_line(left, bottom, right, bottom, clr, dstBase, dstPitch); bmp16_line(left, top, bmp16_line(right, top, left, bottom, clr, dstBase, dstPitch); right, bottom, clr, dstBase, dstPitch); } Th e s ef u n c t i o n sa r ev e r yg e n e r a l : t h e ywi l l wo r kf o ra n y t h i n gt h a th a s1 6 b i tc o l o r s .Th a ts a i d , i tma yb ea n n o y i n gt oh a v et oa d dt h e c a n v a sp o i n t e ra n dp i t c ha l l t h et i me , s oy o uc o u l dc r e a t ea ninterface layer s p e c i f i c a l l yf o rmo d e3a n dmo d e5 .Th eo n e sf o rmo d e3 wo u l dl o o ks o me t h i n gl i k et h i s : typedef u16 COLOR; #define vid_mem ((COLOR*)MEM_VRAM) #define M3_WIDTH 240 // === PROTOTYPES ===================================================== INLINE void m3_plot(int x, int y, COLOR clr); INLINE void m3_line(int x1, int y1, int x2, int y2, COLOR clr); INLINE void m3_rect(int left, int top, int right, int bottom, COLOR clr); INLINE void m3_frame(int left, int top, int right, int bottom, COLOR clr); // === INLINES ======================================================== //! Plot a single \a clr colored pixel in mode 3 at (\a x, \a y). INLINE void m3_plot(int x, int y, COLOR clr) { vid_mem[y*M3_WIDTH+x]= clr; } //! Draw a \a clr colored line in mode 3. INLINE void m3_line(int x1, int y1, int x2, int y2, COLOR clr) { bmp16_line(x1, y1, x2, y2, clr, vid_mem, M3_WIDTH*2); } //! Draw a \a clr colored rectangle in mode 3. INLINE void m3_rect(int left, int top, int right, int bottom, COLOR clr) { bmp16_rect(left, top, right, bottom, clr, vid_mem, M3_WIDTH*2); } //! Draw a \a clr colored frame in mode 3. INLINE void m3_frame(int left, int top, int right, int bottom, COLOR clr) { bmp16_frame(left, top, right, bottom, clr, vid_mem, M3_WIDTH*2); } Fi n a l l y , t h e r ei sam3_fill()f u n c t i o n , t h a tf i l l st h ee n t i r emo d e3c a n v a swi t has i n g l ec o l o r . //! Fill the mode 3 background with color \a clr. void m3_fill(COLOR clr) { int ii; u32 *dst= (u32*)vid_mem; u32 wd= (clr<<16) | clr; for(ii=0; ii<M3_SIZE/4; ii++) *dst++= wd; } No w, n o t ewh a tI ' md o i n gh e r e : i n s t e a do ft r e a t i n gVRAM a sa na r r a yo f1 6 b i tv a l u e swh i c ha r ea p p r o p r i a t ef o r1 6 b p pc o l o r s , I ' mu s i n g a3 2 b i tp o i n t e ra n df i l l i n gVRAM wi t ha3 2 b i tv a r i a b l ec o n t a i n i n gt woc o l o r s .Wh e nf i l l i n gl a r g ec h u n k so fme mo r y , i tma k e sn o file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 41/331 28-03-13 Tonc : GBA Programming in rot13 d i f f e r e n c ei fIf i l l i ti nN 1 6 b i tc h u n k s , o r½N 3 2 b i tc h u n k s .Ho we v e r , b e c a u s ey o uo n l yu s eh a l ft h en u mb e ro fi t e r a t i o n si nt h el a t t e r c a s e , i t ' sr o u g h l yt wi c ea sf a s t .I nC, i t ' sp e r f e c t l yl e g a l t od os o me t h i n gl i k et h i sa n do f t e n a c t u a l l yu s e f u l .Th i si swh yi t ' si mp o r t a n tt ok n o wt h ep r i n c i p l e so fd a t aa n dme mo r y .Al s o n o t et h a tI ' mu s i n gp o i n t e ra r i t h me t i ch e r ei n s t e a do fa r r a yi n d i c e s .Wh i l et h ec o mp i l e r g e n e r a l l yma k et h ec o n v e r s i o ni t s e l f , d o i n gi tma n u a l l yi ss t i l l o f t e nal i t t l ef a s t e r . Wh i l et h i sme t h o di sa l r e a d yt wi c ea sf a s ta st h e‘ n o r ma l ’me t h o d , t h e r ea r ea c t u a l l y mu c hf a s t e rme t h o d sa swe l l .Wewi l l me e tt h e s el a t e r , wh e nwes t o pu s i n gs e p a r a t e toolkitf i l e sa n ds t a r tu s i n gtonclib, t h ec o d el i b r a r yf o rt o n c .To n c l i bc o n t a i n st h e f u n c t i o n sd e s c r i b e da b o v e( o n l yf a s t e r ) , a swe l l a s8 b p pv a r i a t i o n so ft h ebmp16_r o u t i n e s a n di n t e r f a c e sf o rmo d e4a n dmo d e5 . Be l o wy o uc a nf i n dt h ema i nc o d ef o rm3_demo, wh i c hu s e st h em3_f u n c t i o n st od r a w s o mei t e mso nt h es c r e e n .Te c h n i c a l l y , i t ' sb a df o r mt ou s et h i sma n yma g i cn u mb e r s , b u t f o rd e mo n s t r a t i o np u r p o s e si ts h o u l db eo k a y .Th er e s u l tc a nb es e e ni nf i g5 . 3 . Fig 5.3a: d r a wi n gi nmo d e3 . #include "toolbox.h" int main() { int ii, jj; REG_DISPCNT= DCNT_MODE3 | DCNT_BG2; // Fill screen with grey color m3_fill(RGB15(12, 12, 14)); // Rectangles: m3_rect( 12, 8, 108, 72, CLR_RED); m3_rect(108, 72, 132, 88, CLR_LIME); m3_rect(132, 88, 228, 152, CLR_BLUE); // Rectangle frames m3_frame(132, 8, 228, 72, CLR_CYAN); m3_frame(109, 73, 131, 87, CLR_BLACK); m3_frame( 12, 88, 108, 152, CLR_YELLOW); // Lines in top right frame for(ii=0; ii<=8; ii++) { jj= 3*ii+7; m3_line(132+11*ii, 9, 226, 12+7*ii, RGB15(jj, 0, jj)); m3_line(226-11*ii,70, 133, 69-7*ii, RGB15(jj, 0, jj)); } // Lines in bottom left frame for(ii=0; ii<=8; ii++) { jj= 3*ii+7; m3_line(15+11*ii, 88, 104-11*ii, 150, RGB15(0, jj, jj)); } while(1); return 0; } A dash of mode 4 Mo d e4i sa n o t h e rb i t ma pmo d e .I ta l s oh a sa2 4 0 ×1 6 0f r a me b u f f e r , b u ti n s t e a do f1 6 b p pp i x e l si tu s e s8 b p pp i x e l s .Th e s e8b i t sa r ea palette index t ot h eb a c k g r o u n dp a l e t t el o c a t e da t0500:0000.Th ec o l o ry o u ' l l s e eo ns c r e e ni st h ec o l o rf o u n di nt h ep a l e t t ea tt h a t l o c a t i o n . Pi x e l so fab i t d e p t ho f8me a ny o uc a no n l yh a v e2 5 6c o l o r sa tat i me( i n s t e a do f3 2 6 7 8i nt h ec a s eo f1 5 b p p ) , b u tt h e r ea r eb e n e f i t s a swe l l .Fo ro n e , y o uc a nma n i p u l a t et h ec o l o r so fma n yp i x e l sb ys i mp l yc h a n g i n gt h ec o l o ri nt h ep a l e t t e .An8 b p pf r a me b u f f e ra l s o t a k e su ph a l fa smu c hme mo r ya sa1 6 b p pb u f f e r .No to n l yi si tf a s t e rt of i l l ( we l l , i np r i n c i p l ea n y wa y ) , b u tt h e r ei sn o wa l s or o o mf o ra s e c o n db u f f e rt oa l l o wp a g ef l i p p i n g .Wh yt h a t ' su s e f u l wi l l b ec o v e r e di nami n u t e . Th e r ei s , h o we v e r , o n ema j o rd o wn s i z et ou s i n gmo d e4 , wh i c hs t e msf r o mah a r d wa r el i mi t a t i o n .Wi t h8 b i tp i x e l s , i t ' dma k es e n s et o ma pVRAM a sa na r r a yo fb y t e s .Th i swo u l db ef i n ei fi twe r e n ' tf o rt h er a t h e ra n n o y i n gf a c tt h a tVRAM d o e sn o ta l l o wb y t e wr i t e s ! No w, b e c a u s et h i si sav e r yi mp o r t a n tp o i n t , l e tmer e p e a tt h a t : You cannot write to VRAM in byte-sized chunks!!!.By t e r e a d sa r e file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 42/331 28-03-13 Tonc : GBA Programming in rot13 o k , b u twr i t e sh a v et ob ed o n ei n1 6 b i to r3 2 b i tb i tc h u n k s .I fy o udo wr i t ei nb y t e st oVRAM, t h eh a l f wo r dy o u ' r ea c c e s s i n gwi l l e n d u pwi t ht h a tb y t ei nb o t hi t sb y t e s : y o u ' r es e t t i n gt wop i x e l sa to n c e .No t et h a tt h i sn o b y t e wr i t er u l ea l s oe x t e n d st oPALRAM a n d OAM, b u tt h e r ei td o e s n ' tc a u s et r o u b l eb e c a u s ey o uwo n ' tb eu s i n gt h a ta sb y t e sa n y wa y . Soh o wt op l o ts i n g l e p i x e l st h e n ?We l l , y o uh a v et or e a dt h ewh o l eh a l f wo r dy o u ' r et r y i n gt oa c c e s s , ma s ko f ft h eb i t sy o ud o n ' t wa n tt oo v e r wr i t e , i n s e r ty o u rp i x e l sa n dt h e nwr i t ei tb a c k .I nc o d e : #define M4_WIDTH 240 u16 *vid_page= vid_mem; // Width in mode 4 // Point to current frame buffer INLINE void m4_plot(int x, int y, u8 clrid) { u16 *dst= &vid_page[(y*M4_WIDTH+x)/2]; // Division by 2 due to u8/u16 pointer mismatch! if(x&1) *dst= (*dst& 0xFF) | (clrid<<8); // odd pixel else *dst= (*dst&~0xFF) | clrid; // even pixel } Asy o uc a ns e e , i t ' sal i t t l emo r ec o mp l i c a t e dt h a nm3_plot().I tt a k e sal o tl o n g e rt or u na swe l l .St i l l , o n c ey o uh a v eap i x e l p l o t t e r , y o uc a nc r e a t eo t h e rr e n d e r i n gr o u t i n e swi t he a s e .Th eb a s i cc o d ef o rd r a wi n gl i n e s , r e c t a n g l e s , c i r c l e sa n dt h el i k ea r ep r e t t ymu c h i n d e p e n d e n to fh o wp i x e l sa r ef o r ma t t e d .Fo re x a mp l e , d r a wi n gar e c t a n g l ei sb a s i c a l l yp l o t t i n gp i x e l si nad o u b l el o o p . void generic_rect(int left, int top, int right, int bottom, COLOR clr) { int ix, iy; for(iy=top; iy<bottom; iy++) for(ix=left; ix<right; ix++) generic_plot(ix, iy, clr); } Th i si st h eg e n e r i ct e mp l a t ef o rar e c t a n g l ed r a wi n gr o u t i n e .Asl o n ga sy o uh a v eaf u n c t i o n a l p i x e l p l o t t e r , y o u ' r ei nb u s i n e s s .Ho we v e r , b u s i n e s swi l l b every s l o wi nmo d e4 , b e c a u s eo ft h ec o mp l i c a t e df o r mo ft h ep l o t t e r .I na l l l i k e l i h o o d , i t ' l l b es os l o wt oma k ei tu s e l e s s f o rg a me s .Th e r ei sawa yo u t , t h o u g h .Th er e a s o nm4_plot()i ss l o wi sb e c a u s ey o uh a v et ot a k ec a r en o tt oo v e r wr i t et h eo t h e r p i x e l .Ho we v e r , wh e ny o u ' r ed r a wi n gah o r i z o n t a l l i n e( b a s i c a l l yt h eixl o o ph e r e ) , c h a n c e sa r et h a ty o u ' l l h a v et og i v et h a to t h e rp i x e l t h es a mec o l o ra n y wa y , s oy o un e e d n ' tb o t h e rwi t hr e a d ma s k wr i t es t u f fe x c e p ta tt h ee d g e s .Th ei mp l e me n t a t i o no ft h i sf a s t e r( much f a s t e r )l i n ea l g o r i t h ma n ds u b s e q u e n t l yr e c t a n g l ed r a we ri sl e f ta sa ne x e r c i s ef o rt h er e a d e r .Ory o uc a ns e e ko u ttonc_bmp8.ci n t o n c l i b . VRAM vs byte writes Yo uc a n n o twr i t ei n d i v i d u a l b y t e si n t oVRAM ( o rPALRAM o rOAM f o rt h a tma t t e r ) .Ha l f wo r d so rwo r d so n l y , p l e a s e .I f y o uwa n tt owr i t es i n g l eb y t e s , y o uh a v et or e a dt h ef u l l ( h a l f ) wo r d , i n s e r tt h eb y t e , a n dp u ti tb a c k . Pl e a s ed o n ' ts k i pt h i sn o t e , a n dma k ey o u r s e l fa wa r eo ft h ef u l l r a mi f i c a t i o n so ft h i s .Er r o r sd u et op o i n t e r t y p emi s ma t c h e s a r ev e r ye a s yt oma k e , a n dy o uma yb ewr i t i n gt oVRAM a sb y t e smo r eo f t e nt h a ny o ut h i n k . Generic vs specific rendering routines Ev e r yk i n do fg r a p h i c ss u r f a c en e e d si t so wnp i x e l p l o t t e t .I np r i n c i p l e , mo r ec o mp l i c a t e d( mu l t i p i x e l )s h a p e sa r es u r f a c e i n d e p e n d e n t .Fo re x a mp l e , al i n er o u t i n ef o l l o wst h es a mea l g o r i t h m, b u ts i mp l yu s e sad i f f e r e n tp l o t t e rf o rd r a wi n gp i x e l s . Th e s eg e n e r i cf o r msa r eg r e a ti nt e r mso fr e u s a b i l i t ya n dma i n t a i n a b i l i t y , b u tc a nb edisastrous wh e ni tc o me st os p e e d . Cr e a t i n gs u r f a c e s p e c i f i cr e n d e r e r sma yb ee x t r awo r k , b u tc a no no c c a s i o ns a v ey o uu pt oaf a c t o r1 0 0i ns p e e d . 5.2.3. Complications of bitmap modes Wh i l eIc o u l dg oo nt od i s c u s smo r ec o mp l i c a t e dma t t e r s , s u c ha sd r a wi n gr e c t a n g l e sa n db l i t sa n dt e x t , t h e r e ' sv e r yl i t t l er e a s o nt od os o a tt h i sj u n c t i o n .AsIs a i db e f o r e , t h eb i t ma pmo d e sa r eu s e f u l t ol e a r ns o meb a s i cf u n c t i o n a l i t y , b u tf o rmo s tp r a c t i c a l p u r p o s e s , y o u ' r e b e t t e ro f fwi t ht i l e dmo d e s . Th ep r i ma r yi s s u ei ss p e e d .Ev e ns i mp l ep r i mi t i v e ss u c ha st h eo n e ss h o wnh e r ec a nt a k eal o to ft i me , e s p e c i a l l yi fy o u ' r en o t c a r e f u l i ny o u ri mp l e me n t a t i o n .Fo re x a mp l e , af u l l mo d e3s c r e e n wi p ewo u l dt a k ea b o u t6 0 %o faVBl a n kat best!I nb a d i mp l e me n t a t i o n so fas c r e e n wi p e , l i k ed o i n gi twi t har e c t a n g l ed r a we rt h a tc a l l san o n i n l i n ep i x e l p l o t t i n gf u n c t i o n , c o u l dt a k ea smu c h a s1 0f r a me s .An dthen y o us t i l l h a v et od r a wa l l y o u rb a c k g r o u n d sa n ds p r i t e sa n dd ot h eg a mel o g i c .Th ep h r a s e‘ c r a wl i n gh o r r o r ’ s o me h o ws p r i n g st omi n da tt h et h o u g h to ft h i s . As i d ef r o mt h a t , b i t ma pmo d e sc a nu s eo n l yo n eb a c k g r o u n da n dh a v en oh a r d wa r es c r o l l i n gt os p e a ko f .Al s o , t h o u g ht h i si sj u mp i n g t h eg u nab i t , i to v e r l a p st h eme mo r yt h a tc o n t a i n st h es p r i t et i l e s , wh i c hs t a r t sa t0601:0000h.Fo rt h a tr e a s o n , y o uwi l l o n l yb ea b l e t ou s es p r i t e t i l e s5 1 2t o1 0 2 3wh e ni nmo d e s3 5 . Pa g ef l i p p i n gc a na l l e v i a t es o meo ft h e s ei t e ms , b u tt h a t ' sn o ta v a i l a b l ei nmo d e3 .I ti si nmo d e5 , b u tt h a tu s e so n l yas ma l l p o r t i o no f file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 43/331 28-03-13 Tonc : GBA Programming in rot13 t h es c r e e n , s og a mi n gwi t ho n l yt h a tl o o k sa wk wa r d .Asf o rmo d e4 , we l l , t h a t ' so n eo ft h o s ep l a c e swh e r ey o uwi l l really s e ewh a t p r o g r a mmi n gc l o s et ot h eh a r d wa r eme a n s : i td o e s n ' ta l l o wy o ut owr i t et oVRAM i nb y t e s i z e dc h u n k s !Th eo n l ywa yt oh a v eas i n g l e p i x e l r e s o l u t i o ni st oc o mb i n e2a d j a c e n tp i x e l sa n dwr i t et h o s e , wh i c hc o s t sal o to fe x t r at i me . Sob a s i c a l l y , u s et h eb i t ma pmo d e sf o rt e s t i n ga n d / o rs t a t i ci ma g e s , b u tn o tmu c he l s eu n l e s sy o uk n o wt h et i l e mo d e sc a n ' td owh a t y o uwa n t . Bitmap modes are not for gaming Don o tg e tt o oc o mf o r t a b l ewi t hb i t ma pmo d e s .Th e y ' r en i c ef o rg b a d e vi n t r o d u c t o r ys e c t i o n sb e c a u s et h e ya r ee a s i e rt o wo r kwi t ht h a nt i l emo d e s , t h e ya r enot s u i t a b l ef o rmo s tt y p e so fg a me sb e c a u s et h eGBAs i mp l yc a n ' tp u s hp i x e l sf a s t e n o u g h .Ti n k e rwi t ht h e mt og e taf e e l f o rI Or e g i s t e r sa n dt h el i k e , t h e nmo v eo n . 5.3. Page flipping Pa g ef l i p p i n gi sat e c h n i q u et h a te l i mi n a t e sn a s t ya r t i f a c t sl i k et e a r i n gi na n i ma t i o n .Th e r ea r e t wot h i n g sg o i n go na tt h es a met i mei na na n i ma t i o n : p l a c i n gt h ep i x e l so nb i t ma p( wr i t i n g ) , a n d d r a wi n gt h eb i t ma po ns c r e e n( d i s p l a y i n g ) .So f t wa r et a k e sc a r eo fwr i t i n g , u p d a t i n gt h e p o s i t i o n so fc h a r a c t e r se t c ;h a r d wa r ed o e st h ed i s p l a y i n g : i ts i mp l yt a k e st h eb i t ma pa n dc o p i e s i tt ot h es c r e e n .Th ep r o b l e mi st h a tb o t ht h e s ep r o c e s s e st a k et i me .Wh a t ' swo r s e , t h e yh a p p e n a tt h es a met i me .An dwh e nt h eg a mes t a t ec h a n g e si nmi dd r a w, t h eb o t t o ms e c t i o nwi l l b eo f Fig 5.4:Pagef l i ppi n gpr o c e d ur e .Noda t ai s e d , o n l yt h e‘ d i s pl a y’a n d‘ wr i t e ’ t h ec u r r e n ts t a t e , wh i l et h et o ps e c t i o nwi l l r e p r e s e n tt h ep r e v i o u ss t a t e .Ne e d l e s st os a y , t h i si s copi po i n t e r sa r es wa ppe d . b a d . En t e rp a g ef l i p p i n g .I n s t e a do fu s i n go n es i n g l eb i t ma pt owr i t ea n dd i s p l a y , y o uu s et wo . Wh i l eo n eb i t ma pi sd i s p l a y e d , y o uwr i t ea l l y o un e e do n t oas e c o n db i t ma p( t h eb a c k b u f f e r ) .Th e n , wh e ny o u ' r ef i n i s h e d , y o ut e l l t h e h a r d wa r et od i s p l a yt h a ts e c o n db i t ma pa n dy o uc a np r e p a r et h en e x tf r a meo nt h ef i r s t .Noa r t i f a c t swh a t s o e v e r . Wh i l et h ep r o c e d u r ewo r k sg r e a t , t h e r ea r es o mes n a r e s .Fo rt h ef i r s t , c o n s i d e rt h i s .Gi v e na r et h ep o i n t e r st ot h et wop a g e spage1 a n dpage2.Ri g h tn o w, page1i sd i s p l a y e da n dpage2i sb e i n gp r e p a r e d ;s of a rs og o o d .Bu twh e ny o us wi t c ht ot h es e c o n dp a g e , t h i so n l yma k e spage2t h ed i s p l a y p a g e ;y o uh a v et oma k epage1t h ewr i t e p a g ey o u r s e l f !Th es o l u t i o nt ot h i sp r o b l e mi ss i mp l e : u s e awr i t e b u f f e rp o i n t e r , b u ti tc a nc a t c hy o uo f f g u a r di fy o u ' r en e wt ot h i ss t u f f . Th es e c o n dp r o b l e mc o n c e r n sal i t t l en a s t yi nt h ea g e o l dme t h o do fa n i ma t i o n .Th ec a n o n i c a l a n i ma t i o nd o e st h i s .Fr a me 1 : d r a w o b j e c t .Fr a me 2 : e r a s eo l do b j e c t , d r a wo b j e c ti nn e ws t a t e .Th i sd o e s n ' two r kf o rp a g ef l i p p i n gs i n c eFr a me 2i swr i t t e no na ne n t i r e l y d i f f e r e n tb i t ma pt h a nFr a me 1 , s ot r y i n gt oe r a s eFr a me 1 ' so l do b j e c td o e s n ' t .Wh a ty o un e e dt oe r a s ei st h eo b j e c tf r o m2f r a me sa g o . Ag a i n , e a s ys o l u t i o n , b u ty o uh a v eb ea wa r eo ft h ep r o b l e m.( Ofc o u r s e , e r a s i n gt h ee n t i r ef r a mee a c ht i mewo u l dwo r kt o o , b u twh o ' s g o tt h et i me ? ) Pageflipping, not double buffering An o t h e rme t h o do fs mo o t h e ra n i ma t i o ni sd o u b l eb u f f e r i n g : d r a wo nas e c o n d a r yb u f f e r( t h eb a c k b u f f e r )a n dc o p yi tt ot h e s c r e e nwh e nf i n i s h e d .Th i si saf u n d a me n t a l l yd i f f e r e n tt e c h n i q u et h a np a g ef l i p p i n g !Ev e nt h o u g hb o t hu s et wob u f f e r s , i n p a g ef l i p p i n gy o ud o n ' tc o p yt h eb a c k b u f f e rt ot h ed i s p l a yb u f f e r , y o umake b a c k b u f f e rt h ed i s p l a yb u f f e r . Wh a tt h eGBAd o e si sp a g ef l i p p i n g , s or e f e rt oi ta ss u c h . 5.3.1. GBA page flipping Th es e c o n dp a g eo ft h eGBAi sl o c a t e da tl o c a t i o n0600:A000h.I fy o ul o o ka tt h es i z er e q u i r e df o rmo d e3 , y o u ' l l s e ewh yi td o e s n ' t h a v ep a g e f l i p p i n gc a p a b i l i t i e s : t h e r e ' sn or o o mf o ras e c o n dp a g e .Tos e tt h eGBAt od i s p l a yt h es e c o n dp a g e , s e tREG_DISPCNT{ 4 } . Myp a g ef l i p p i n gf u n c t i o nl o o k sal i t t l el i k et h i s : u16 *vid_flip() { // toggle the write_buffer's page vid_page= (u16*)((u32)vid_page ^ VID_FLIP); REG_DISPCNT ^= DCNT_PAGE; // update control register return vid_page; } Th ec o d ei sr e l a t i v e l ys t r a i g h t f o r wa r d .vid_pagei st h ep o i n t e rt h a ta l wa y sp o i n t st ot h ewr i t e p a g e .Ih a dt op u l l al i t t l ec a s t i n g t r i c k e r yt og e tt h eXORt owo r k( Cd o e s n ' tl i k ei twh e ny o ut r yi to np o i n t e r s ) .Ont h eGBA, t h es t e p sf o rp a g ef l i p p i n ga r ep e r f e c t l y x o r r a b l eo p e r a t i o n s .Su r e , y o ucould j u s tp u tt h ee q u i v a l e n ti na nif-elseb l o c k , b u twh e r e ' st h ef u ni nt h a t:P? 5.3.2. Page flipping demo Wh a tf o l l o wsi st h ec o d e( s a n sd a t a )f o rt h epageflipd e mo .Th ea c t u a l p a r tc o n c e r n e dwi t hp a g ef l i p p i n gi sv e r ys ma l l .I nf a c t , t h e a c t u a l f l i pi sme r e l yac a l l t ovid_flip()o n c ee v e r y6 0f r a me s=1s e c o n d( p o i n t3 ) .We ' l l a l s oh a v et os e tt h ev i d e omo d et o s o me t h i n gt h a ta c t u a l l yh a sp a g e st of l i p , wh i c hi nt h i sc a s ei smo d e4 . Wh a twe ' l l h a v et od oa swe l l i sl o a dt h ed a t at h a twi l l b ed i s p l a y e do nt h e s et wop a g e s .I ' mu s i n gt h es t a n d a r dCr o u t i n e memcpy()f o rt h ec o p y , b e c a u s et h a t ' st h es t a n d a r dwa yo fc o p y i n gt h i n g si nC.Wh i l ei t ' sf a s t e rt h a nma n u a l l o o p s , i td o e sc o mewi t h file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 44/331 28-03-13 Tonc : GBA Programming in rot13 af e ws n a r e st h a ty o un e e dt ob ea wa r eo fb e f o r eu s i n gi te v e r y wh e r e .To n c l i bc o me swi t hf a s t e ra n ds a f e rr o u t i n e s , b u twe ' l l g e tt o t h o s ewh e ni t ' st i me . Lo a d i n gab i t ma pi sv e r ys i mp l ei nt h e o r y , b u tt h eb i t ma p ( s )I ' mu s i n ga r eo n l y1 4 4 x 1 6i ns i z e , wh i l et h eVRAM p a g e ' sp i t c hi s2 4 0 p i x e l swi d e .Th i sme a n st h a twe ' l l h a v et oc o p ye a c hs c a n l i n es e p a r a t e l y , wh i c hi sd o n ea tp o i n t( 1 ) .No t et h a tI ' mc o p y i n g frontBitmapt ovid_mem_fronta n dbackBitmapt ovid_mem_back, b e c a u s et h o s ea r et h es t a r t i n gl o c a t i o n so ft h et wo p a g e s . Si n c et h e s ea r emo d e4b i t ma p s , t h e y ' l l a l s on e e dap a l e t t e .Bo t hp a l e t t e su s efrontPal, b u ti n s t e a do fu s i n gmemcpy()t oc o p y i tt ot h eb a c k g r o u n dp a l e t t eme mo r y , I ' mu s i n gau 3 2 a r r a yb e c a u s e… we l l , j u s tb e c a u s eIg u e s s . La s t l y , y o uc a np a u s ea n du n p a u s et h ed e mob yh o l d i n gd o wns t a r t . #include <string.h> #include <toolbox.h> #include "page_pic.h" void load_gfx() { int ii; // (1) Because my bitmaps here don't fit the screen size, // I'll have to load them one scanlline at a time for(ii=0; ii<16; ii++) { memcpy(&vid_mem_front[ii*120], &frontBitmap[ii*144/4], 144); memcpy(&vid_mem_back[ii*120], &backBitmap[ii*144/4], 144); } // (2) You don't have to do everything with memcpy. // In fact, for small blocks it might be better if you didn't. // Just mind your types, though. No sense in copying from a 32bit // array to a 16bit one. u32 *dst= (u32*)pal_bg_mem; for(ii=0; ii<8; ii++) dst[ii]= frontPal[ii]; } int main() { int ii=0; load_gfx(); // Set video mode to 4 (8bpp, 2 pages) REG_DISPCNT= DCNT_MODE4 | DCNT_BG2; while(1) { while(KEY_DOWN_NOW(KEY_START)); // pause with start vid_vsync(); // (3) Count 60 frames, then flip pages if(++ii == 60) { ii=0; vid_flip(); } } return 0; } Fig 5.5: t h epa g ef l i ppi n gd e mos wi t c h e sb e t we e nt h e s et wob l o c k s . 5.4. On data and how to use it Th i ss e c t i o ni sal i t t l eb o r i n g( o k , v e r yb o r i n g )b u ti tn e e d st ob es a i d .Wh i l eb o o k sa n dt u t o r i a l so nCma yu s ed a t af o rwh a t e v e rp u r p o s e , t h e yo f t e ng l o s so v e rwh a td a t aa c t u a l l yis a tt h el o we s tl e v e l , a n dh o wt od e a l wi t hi tc o r r e c t l y .Asy o u ' l l b ed e a l i n gd i r e c t l ywi t h h a r d wa r ea n dme mo r yh e r e , i ti si mp o r t a n tt h a ty o ua r ea wa r eo ft h e s ei t e ms , p r e f e r a b l ye v e nu n d e r s t a n dt h e m, s ot h a tt h e yd o n ' tb i t e file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 45/331 28-03-13 Tonc : GBA Programming in rot13 y o ui nt h ea s sa ts o mel a t e rp o i n t . Th ef i r s tt wos u b s e c t i o n sa r ea b o u th o wt og e tg r a p h i c si n t oy o u rg a me , s o me t h i n gt h a ty o u ' l l r e a l l yn e e dt ok n o w.Af t e rt h a tI ' l l d i s c u s saf e wn a s t ya n dh i g h l yt e c h n i c a l t h i n g st h a tma yo rma yn o tc a u s ep r o b l e msl a t e ro n .Th e s ea r eo p t i o n a l a n dy o uc a ns k i pt ot h e d a t a l o a d i n g / i n t e r p r e t i n gd e moa ta n yt i me .Th a ts a i d , Iu r g ey o ut or e a dt h e ma n y wa yb e c a u s et h e yma ys a v ey o ual o to fd e b u g g i n g t i me . Relax, it's only 1s and 0s Wh e ny o ug e tr i g h td o wnt oi t , e v e r y t h i n go nc o mp u t e r si sme r e l yab i gme s so fb i t swi t h o u ta n yp u r p o s eb yi t s e l f .I ti st h e i n t e r a c t i o nb e t we e nh a r d wa r ea n ds o f t wa r et h a tma k e ss e q u e n c e so fb i t sa p p e a ra sv a l i de x e c u t a b l ec o d e , ab i t ma p , mu s i co r wh a t e v e r . 5.4.1. Yes, we have no files Th i sma yb eag o o dp o i n tt os a yaf e wwo r d so nd a t a .St r i c t l ys p e a k i n g , everything i sd a t a , b u ti nt h i sc a s eI ' mr e f e r r i n gt od a t at h a to n PCg a me swo u l db es e p a r a t ef r o mt h ee x e c u t a b l e : g r a p h i c s , mu s i c , ma y b es c r i p t sa n dt e x t f i l e sa n dwh a tn o t .Th i sa l l wo r k sf i n eo na PC, b u tn o ts of i n eo nt h eGBAb e c a u s et h e r eis no file system.Th i sme a n st h a ty o uc a n n o tu s et h es t a n d a r df i l eI / Or o u t i n e s ( fscanf(), fread(), e t c )t or e a dt h ed a t a , b e c a u s et h e r ea r en of i l e st or e a dt h e mf r o m. Al l t h eg a me ' sd a t ah a st ob ea d d e dd i r e c t l yt ot h eb i n a r y .Th e r ea r ean u mb e ro fwa y st od ot h i s .Th emo s tc o mmo nwa yi st o c o n v e r tt h er a wb i n a r yf i l e st oCa r r a y s , t h e nc o mp i l et h o s ea n dl i n kt h e mt ot h ep r o j e c t .We l l , t h emo s tc o mmo na mo n gh o me b r e we r si s p r o b a b l yc o n v e r t i n gt oCa r r a y sa n d#i n c l u d i n gt h e m, b u tt h a t ' ss o me t h i n gt h a ty o us h o u l dnever d o .Al s op o p u l a ra r ea s s e mb l ya r r a y s . Th e s ea r eau s e f u l a l t e r n a t i v et oCa r r a y sb e c a u s ea )t h e ycan't b e#i n c l u d e da n db )b e c a u s et h e yb y p a s st h ec o mp i l a t i o ns t e pa n d c o mp i l a t i o no fa r r a y si sv e r yi n t e n s i v e .Ofc o u r s e , y o uwo u l dh a v et ok n o wh o wt owo r kwi t ht h ea s s e mb l e r .An o t h e rn i c et h i n ga b o u t t h ea s s e mb l e ri st h a ty o uc a ni n c l u d eb i n a r yf i l e sd i r e c t l yi n t ot h e m, e l i mi n a t i n gt h en e e df o rac o n v e r t e r .La s t l y , wh i l et h eGBAd o e s n ' t h a v ean a t i v ef i l es y s t e m, y o uc a na l wa y swr i t ey o u ro wn .Ac o mmo no n ei sGBFSb yt h eg b a d e vf o r u mFAQma i n t a i n e r , t e p p l e s . Us i n gaf i l es y s t e mi sa c t u a l l yt h er e c o mme n d e dme t h o d , b u tf o rn o w, I ' l l s t i c kt oCa r r a y sb e c a u s et h e ya r et h ee a s i e s tt ou s e . Ahem. Actually, we do have files Th e r ewere n of i l e si nt h ep a s t , b u ti nJ u l yo f2 0 0 6 , Ch i s h mg a v eu slibfat, wh i c hi saFATl i k ef i l es y s t e mf o rGBAa n d NDS.I ti sd i s t r i b u t e dv i aDKPa swe l l , s oc h a n c e sa r ey o uh a v ei ta l r e a d y . Where do my arrays go? Byd e f a u l t , a r r a y sg oi n t oI WRAM.Yo uk n o w, t h eo n et h a t ' so n l y3 2k bl o n g .No w, amo d e3b i t ma pi s2 4 0 x 1 6 0 x 2=7 7k b .Ob v i o u s l y , t r y i n gt op u ta7 7k bo b j e c ti n t oa3 2 k bs e c t i o nwo u l df i tn i c e l yi n t ot h eb a dt h i n g sc a t e g o r y .Toa v o i dt h i s , p u ti ti nt h er e a d o n l ys e c t i o n ( ROM) , wh i c hi smu c hl a r g e r .Al l y o uh a v et od of o rt h i si sa d dt h e‘ const’k e y wo r dt ot h ed e f i n i t i o ni fy o u ' r eu s i n gC, o rt h e ‘ .rodata’d i r e c t i v ei na s s e mb l y .No t et h a tf o rmu l t i b o o tp r o g r a msROM a c t u a l l yme a n sEWRAM, wh i c hi so n l y2 5 6k bl o n g .Th e l a t t e rwo u l df i tt h r e emo d e3b i t ma p s ;mo r ewo u l da g a i nb eb a du n l e s sy o uu s ec o mp r e s s i o n . No t et h a twh a tIs a i da b o u ta r r a y si st r u ef o rall a r r a y s , n o tj u s td a t aa r r a y s : i fy o uwa n ta n yk i n do fl a r g ea r r a y( l i k eab a c k b u f f e r f o rmo d e3 ) , i two u l da l s od e f a u l tt oa n dkill I WRAM.Bu ty o uc a n ' tma k ei tc o n s tb e c a u s et h e ny o u ' dn o tb ea b l et owr i t eo ni t .GCC h a sa t t r i b u t e st h a tl e t sy o uc h o o s ewh e r et h i n g sa r ep u t–i nEWRAM f o ri n s t a n c e .He r ea r et h ec o mmo n l ys e e n#d e f i n e sf o rt h e a t t r i b u t e st h a tc a nb eu s e df o rs p e c i f i cs e c t i o np l a c e me n t . #define EWRAM_DATA __attribute__((section(".ewram"))) #define IWRAM_DATA __attribute__((section(".iwram"))) #define EWRAM_BSS __attribute__((section(".sbss"))) #define EWRAM_CODE __attribute__((section(".ewram"), long_call)) #define IWRAM_CODE __attribute__((section(".iwram"), long_call)) Const is good Da t at h a ty o ud o n ' te x p e c tt oc h a n g ei ny o u rg a mes h o u l db ed e f i n e da sc o n s t a n td a t au s i n gt h e‘ const’k e y wo r d , l e s ti t t r a s h e sy o u rI WRAM. Converted and const arrays in C++ Th e r ea r et wol i t t l es n a g st h a ty o uc a nt r i po ni fy o u ' r eu s i n g( c o n v e r t e d )d a t aa r r a y si nC++.Th ef i r s ti st h a tt o o l st h a tg e n e r a t et h e a r r a y swi l l o u t p u tCf i l e s , n o tC++f i l e s .Th i si sn o tap r o b l e mi ni t s e l fb e c a u s et h o s ef i l e swi l l b ec o mp i l e dj u s tt h es a me .Wh a tis a p r o b l e mi st h a tC++u s e ss o me t h i n gk n o wna sNa mema n g l i n gt oa l l o wo v e r l o a d i n ga n ds t u f fl i k et h a t .Cd o e s n ' ta n da sar e s u l t , t h e n a met h a tt h eC++f i l el o o k sf o ri s n ' tt h es a meo n ea si nt h eCf i l ea n dy o ug e tu n d e f i n e dr e f e r e n c e s .Tof i xt h i s , u s eèxtern "C"' i n f r o n to ra r o u n dt h ed e c l a r a t i o n so ft h es t u f fi nt h eCf i l e s . // This: file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 46/331 28-03-13 Tonc : GBA Programming in rot13 extern "C" const unsigned char C_array[]; // Or this: extern "C" { const unsigned char C_array1[]; const unsigned char C_array2[]; } An o t h e rp r o b l e mwi t hC++i st h a tc o n s t a r r a y sa r ec o n s i d e r e ds t a t i c( l o c a l t ot h ef i l et h a tc o n t a i n si t )u n l e s sy o ua d da ne x t e r n a l d e c l a r a t i o nt oi t .Soi fy o uj u s th a v ec̀onst u8 foo[]= { etc }' i naf i l e , t h ea r r a ywi l l b ei n v i s i b l et oo t h e rf i l e s . Th es o l u t i o n h e r ewo u l db et oa d dt h ed e c l a r a t i o ni n s i d et h ef i l ei t s e l fa swe l l . // foo.cpp. Always have an external declaration // inside the file as well. extern const unsigned char foo[]; const unsigned char foo[]= { // data }; 5.4.2. Data conversion I t ' sr a t h e re a s yt owr i t eat o o l t h a tc o n v e r t sab i n a r yf i l et oaCo ra s ma r r a y .I nf a c t , d e v k i t ARM c o me swi t ht wot h a td oj u s tt h a t : raw2c.exea n dbin2s.exe.I ta l s oc o me swi t ht h eb a s i ct o o l sf o rg b f sb yt h ewa y .Bu tb e i n ga b l et oa t t a c hb i n a r yf i l e st oy o u r g a mei so n l yp a r to ft h es t o r y .Co n s i d e rab i t ma p , f o re x a mp l e .I np r i n c i p l e , ab i t ma pi sab i n a r yf i l ej u s tl i k ea n yo t h e r .Th e r e ' sn o t h i n g i n h e r e n t l yg r a p h i c a l a b o u ti t , a n di td o e s n ' tma g i c a l l ya p p e a ra sab i t ma pwh e n e v e ry o uu s ei tb yi t s e l f .Ye s , wh e ny o ud o u b l e c l i c ko ni t , a ni ma g ev i e we rma yp o pu pa n dd i s p l a yi t , b u tt h a t ' so n l yb e c a u s et h e r e ' ss o mes e r i o u swo r kb yt h eOSg o i n go nu n d e r n e a t h .Wh i c hwe d o n ' th a v eh e r e . Mo s tf i l e swi l l f o l l o wac e r t a i nf o r ma tt ot e l l i twh a ti ti s , a n dh o wt ou s ei t .Fo rb i t ma p s , t h a tu s u a l l yme a n swi d t h , h e i g h t , b i t d e p t h s a n daf e wo t h e rf i e l d sa swe l l .Th ep o i n ti st h a tt h e y ' r en o td i r e c t l yu s a b l e .Yo uc a n ' tj u s ta t t a c h , s a y , aBMPf i l et oy o u rp r o j e c ta n d c o p yi tt oVRAM a n dt h i n kt h a te v e r y t h i n gwi l l wo r ko u t .No , y o uh a v et oconvert i tt oaGBAu s a b l ef o r ma t .No w, y o uc a nd ot h i s i n t e r n a l l y( o nt h eGBAi t s e l f ) , o re x t e r n a l l y( o nt h ePCa n da t t a c ht h ec o n v e r t e dd a t at ot h ep r o j e c t ) .Be c a u s et h el a t t e ri samu c hmo r e e f f i c i e n tu s eo fGBAr e s o u r c e s , t h a ti st h eu s u a l p r o c e d u r e . Th e r ea r ema n yc o n v e r s i o nt o o l s , o n emi g h ta l mo s ts a yt o oma n y .So mea r eo n e t r i c kp o n i e s : as i n g l ef i l e t y p et oas i n g l eg r a p h i c s mo d ef o re x a mp l e .So mea r ev e r yp o we r f u l a n dc a nh a n d l emu l t i p l ef i l e t y p e s , mu l t i p l ef i l e s , d i f f e r e n tc o n v e r s i o nmo d e swi t hl o t so f o p t i o n so nt h es i d e , a n dc o mp r e s s i o n .I ts h o u l db eo b v i o u swh i c ha r eo ft h emo s tv a l u e . Ag o o do n ei sg f x 2 g b a .Th i si sac o mma n d l i n et o o l s ot h a ti tc a nb eu s e di nama k e f i l e , b u tt h e r ei saGUIf r o n t e n df o ri ta swe l l . Th i st o o l h a st h eGo o dTh i n g sIme n t i o n e de a r l i e r , p l u ss o mema p e x p o r t i n go p t i o n sa n dp a l e t t eme r g i n g , b u tt h ei n p u tf i l emu s tb e8 b i t a n dIh e a rt h a twh i l ei td o e sc o mp r e s sd a t a , t h ea r r a y s i z ei ss t i l l g i v e na si t su n c o mp r e s s e ds i z ef o rs o meu n f o r t u n a t er e a s o n .Th i st o o l c o me swi t ht h eHAM i n s t a l l a t i o n , a n di sq u i t ec o mmo n , s od e f i n i t e l yr e c o mme n d e d .Un f o r t u n a t e l y , t h e r es e e mst ob ea n o t h e rt o o l wi t h t h es a men a me .Yo u ' l l wa n tt h ev 0 . 1 3v e r s i o nb yMa r k u s , n o tt h eo t h e ro n e . Pe r s o n a l l y , Iu s eUs e n t i , b u tt h e nIp r e t t ymu c hh a v et ob e c a u s ei t smyo wnt o o l .Th i si sa c t u a l l yab i t ma pe d i t o rwi t he x p o r t i n g o p t i o n st h r o wni n .I ta l l o wsd i f f e r e n tf i l e t y p e s , d i f f e r e n tb i t d e p t h s , d i f f e r e n to u t p u tf i l e s , a l l mo d e s , s o mema p e x p o r t i n gs t u f f , me t a t i l i n g , c o mp r e s s i o na n daf e wo t h e r s .I tma yn o tb ea sp o we r f u l a sb i gp h o t o e d i t i n gt o o l sa sPh o t o Sh o p , Gi mpa n dt h el i k e , b u ti tg e t st h ej o b d o n e .I fy o u ' r es t i l l d r a wi n gy o u rg r a p h i c swi t hMSPa i n t , p l e a s es t o pt h a ta n du s et h i so n ei n s t e a d .Th ee x p o r t e ri sa l s oa v a i l a b l e s e p a r a t e l yi nt h ef o r mo ft h eo p e ns o u r c ep r o j e c tc a l l e d( wi n ) g r i t , wh i c hc o me si nac o mma n d l i n ei n t e r f a c e( g r i t )a n daGUI( wi n g r i t ) . Aso fJ a n u a r y2 0 0 7 , i ti sa l s op a r to ft h ed e v k i t Pr od i s t r i b u t i o n . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 47/331 28-03-13 Tonc : GBA Programming in rot13 Bitmap conversion via CLI Th e r ea r ema n yc o mma n d l i n ei n t e r f a c e sa v a i l a b l ef o rg r a p h i c sc o n v e r s i o n , b u tt oma k et h e mf u n c t i o ny o un e e dt h ec o r r e c t f l a g s .He r ea r ee x a mp l e sf o rg f x 2 g b aa n dg r i t , c o n v e r t i n gab i t ma pfoo.bmpt oaCa r r a yf o rmo d e s3 , 4a n d5 .Th i si sj u s t a ne x a mp l e , b e c a u s et h i si sn o tt h ep l a c ef o raf u l l d i s c u s s i o no nt h e m.Lo o ki nt h e i rr e s p e c t i v er e a d me ' sf o rmo r ed e t a i l s . # gfx2gba # mode 3, 5 (C array; u16 foo_Bitmap[]; foo.raw.c) gfx2gba -fsrc -c32k foo.bmp # mode 4 (C array u8 foo_Bitmap[], u16 master_Palette[]; foo.raw.c, mastel.pal.c) gfx2gba -fsrc -c256 foo.bmp # grit # mode 3, 5 (C array; u32 fooBitmap[]; foo.c foo.h) grit foo.bmp -gb -gB16 # mode 4 (C array; u32 fooBitmap[], u16 fooPal[]; foo.c foo.h) grit foo.bmp -gb -gB8 Be l o w, y o uc a ns e eap a r t i a l l i s t i n go fmodes.c, wh i c hc o n t a i n st h eb i t ma pa n dt h ep a l e t t e 0 x 0 1 0 2 0 3 0 4 u s e di nt h ebm_modesd e mod i s c u s s e da tt h ee n do ft h i ss e c t i o n , a se x p o r t e db yUs e n t i .I ti s big u32 o n l yav e r ys ma l l p a r to ft h ef i l eb e c a u s ea to v e r2 7 0 0l i n e si ti swa yt o ol o n gt od i s p l a yh e r e , 0 x 0 1 0 2 0 x 0 3 0 4 big u16 wh i c hwo u l d n ' ts e r v emu c ho fap u r p o s ea n y wa y .No t et h a tb o t ha r eu 3 2 a r r a y s , r a t h e rt h a n u8 0x01 0x02 0x03 0x04 t h eu 8o ru 1 6 a r r a y sy o umi g h te n c o u n t e re l s e wh e r e .Wh a ty o un e e dt or e me mb e ri st h a tit x 0 2 0 1 0 x 0 4 0 3 doesn't matter i nwh a tk i n do fa na r r a yy o up u tt h ed a t a : i nme mo r yi t ' l l c o meo u tt h es a me little u16 0 a n y wa y . 0 x 0 4 0 3 0 2 0 1 little u32 We l l , t h a t ' sn o tquite t r u e .On l ywi t hu 3 2 a r r a y si sp r o p e rd a t aa l i g n me n tg u a r a n t e e d , Table 5.2: B i g e n d i a n v s l i t t l e e n di a n wh i c hi sag o o dt h i n g .Mo r ei mp o r t a n t l y , y o uh a v et ob ec a r e f u l wi t ht h eb y t e o r d e ro fmu l t i i n t e r p r e t a t i o n o f b y t e s e q u e n c e 0 1 h ,0 2 h, b y t et y p e s .Th i si sc a l l e dt h ee n d i a n n e s so ft y p e s .I nalittle endian s c h e me , l e a s ts i g n i f i c a n t 0 3 h , 0 4 h b y t e swi l l g of i r s ta n di nabig endian, mo s ts i g n i f i c a n tb y t e swi l l g of i r s t .Se et a b l e2f o ra n e x a mp l eu s i n g0x01, 0x02, 0x03a n d0x04.Th eGBAi sal i t t l ee n d i a nma c h i n e , s ot h ef i r s t wo r do ft h emodesBitmapa r r a y , 0x7FE003E0i st h eh a l f wo r d s0x03E0( g r e e n )f o l l o we db y0x7FE0( c y a n ) .I fy o uwa n tmo r e e x a mp l e so ft h i s , o p e nu pVBA' sme mo r yv i e we ra n dp l a ya r o u n dwi t ht h e8 b i t , 1 6 b i ta n d3 2 b i ts e t t i n g s . Th ek e yp o i n th e r e : t h ed a t ai t s e l fd o e s n ' tc h a n g ewh e ny o uu s ed i f f e r e n td a t a t y p e sf o rt h ea r r a y s , o n l yt h ewa yy o urepresent i t d o e s .Th a twa sa l s ot h ep o i n to ft h ebm_modesd e mo : i t ' st h es a med a t ai nVRAM a l l t h et i me ;i t ' sj u s tu s e di nad i f f e r e n twa y . //====================================================================== // // modes, 240x160@16, // + bitmap not compressed // Total size: 76800 = 76800 // // Time-stamp: 2005-12-24, 18:13:22 // Exported by Cearn's Usenti v1.7.1 // (comments, kudos, flames to "[email protected]") // //====================================================================== const unsigned int modesBitmap[19200]= { 0x7FE003E0,0x7FE07FE0,0x7FE07FE0,0x7FE07FE0,0x7FE07FE0,0x7FE07FE0,0x7FE07FE0,0x7FE07FE0, 0x080F080F,0x080F080F,0x080F080F,0x080F080F,0x080F080F,0x080F080F,0x080F080F,0x080F080F, 0x080F080F,0x080F080F,0x080F080F,0x080F080F,0x080F080F,0x080F080F,0x080F080F,0x080F080F, 0x080F080F,0x080F080F,0x080F080F,0x080F080F,0x080F080F,0x080F080F,0x080F080F,0x080F080F, // ... // over 2500 more lines like this // ... 0x080F080F,0x080F080F,0x080F080F,0x080F080F,0x080F080F,0x080F080F,0x080F080F,0x080F080F, 0x080F080F,0x080F080F,0x080F080F,0x080F080F,0x080F080F,0x080F080F,0x080F080F,0x080F080F, 0x080F080F,0x080F080F,0x080F080F,0x080F080F,0x080F080F,0x080F080F,0x080F080F,0x080F080F, 0x7FE07FE0,0x7FE07FE0,0x7FE07FE0,0x7FE07FE0,0x7FE07FE0,0x7FE07FE0,0x7FE07FE0,0x7FE07FE0, }; const unsigned int modesPal[8]= { 0x7FE07C1F,0x03FF0505,0x03E00505,0x7C000000,0x0000080F,0x00000000,0x00000000,0x080F0000, }; file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 48/331 28-03-13 Tonc : GBA Programming in rot13 Th o s e2 7 0 0l i n e sr e p r e s e n ta7 7 k bb i t ma p .On esingle b i t ma p .I na l l l i k e l i h o o d , y o u ' l l n e e da tl e a s tac o u p l eo ft h e mt oma k ea n y t h i n g wo r t h wh i l e .Mo s tg a me sh a v el o t so fd a t ai nt h e m, n o to n l yg r a p h i c sb u tma p sa n ds o u n da n dmu s i ca swe l l .Al l t h i sa d d su pt oah u g e a mo u n to fd a t a , c e r t a i n l yt o omu c hf o rj u s tEWRAM a n dma y b ee v e nf o raf u l l c a r t .Th a ti swh ycompression i sa l s oi mp o r t a n t .Th e GBABI OSh a sd e c o mp r e s s i o nr o u t i n e sf o rb i t p a c k i n g , r u n l e n g t he n c o d i n g , LZ7 7a n dHu f f ma n .Co n v e r t e r ss o me t i me sh a v et h e a p p r o p r i a t ec o mp r e s s o r sf o rt h e s er o u t i n e s , wh i c hc a nd r a s t i c a l l ys h r i n kt h ea mo u n to fme mo r yu s e d .Us e n t i a n d( wi n ) g r i ts u p p o r tt h e s e c o mp r e s s o r s .Sod o e sg f x 2 g b a , wh i c he v e nh a ss o memo r e .At o o l t h a tj u s td o e sc o mp r e s s i o no nb i n a r yf i l e s( b u td o e si tv e r ywe l l )i s GBACr u s h e r .Iwo n ' tg oi n t oc o mp r e s s i o nt h a tmu c h( o ra ta l l ) , b u ty o uc a nr e a du po nt h es u b j e c th e r e . Understanding data I ti sv i t a l t h a ty o uu n d e r s t a n dwh a td a t ai s , h o wt h ed i f f e r e n td a t a t y p e swo r k .Pr e f e r a b l ye n d i a n n e s sa n da l i g n me n tt o o . Emu l a t o r sa n dh e xe d i t o r sc a nh e l py o uwi t ht h i s .On c ey o uh a v ec o mp i l a t i o nwo r k i n g , j u s tma k eaf e wr a n d o ma r r a y sa n d s e ewh a tt h e yl o o kl i k ei nt h eVBAme mo r yv i e we rf o rawh i l e . 5.4.3. #including code or data considered harmful Mo s tn o n t r i v i a l p r o j e c t swi l l h a v emu l t i p l ef i l e swi t hc o d ea n dd a t a .Th es t a n d a r dwa yo f d e a l i n gwi t ht h e s ei st oc o mp i l et h e s es e p a r a t e l ya n dt h e nl i n kt h er e s u l t st ot h ef i n a l b i n a r y . Th i si st h er e c o mme n d e ds t r a t e g y .Ho we v e r , mo s to t h e rt u t o r i a l sa n dma n yo ft h ee x a mp l e c o d ey o uc a nf i n do nt h ewe bd os o me t h i n ge l s e : t h e y#i n c l u d ee v e r y t h i n gi n t ot h ema i n s o u r c ef i l ea n dc o mp i l et h a t .Th i si snot ar e c o mme n d e dp r a c t i c ea n ds h o u l db ea v o i d e d . ” Bu twh yn o t ?I ts e e mst owo r kf i n e , a n di t ' ss oe a s y ! ” Ye s , i ti se a s y ;a n di td o e ss e e mt owo r k .Th ema i np r o b l e mi st h a ti ti s n ' ts c a l a b l e .Fo r s ma l l p r o j e c t s( ah a n d f u l o ff i l e s )y o up r o b a b l ywo n ' tn o t i c e , b u ta sp r o j e c t sg r o wt o h u n d r e d sa n dp e r h a p st h o u s a n d so ff i l e s , y o uwi l l r u ni n t os o mev e r ya n n o y i n gp r o b l e ms . Th ema i ni s s u ei swh a t#i n c l u d ea c t u a l l yd o e s .I tc o p i e st h ewh o l ei n c l u d e df i l ei n t ot h e Fig 5.6: e v e nBa r tk n o ws… i n c l u d e rt of o r mas i n g l el a r g e rf i l e .Th i sl e a d st ot h ef o l l o wi n gi s s u e s . Massive files to compile.So , #i n c l u d i n gc r e a t e so n eb i gf i l e .I fy o uh a v eal o to fs t u f f , y o u ' l l h a v eo n every b i gf i l e .Th i swi l l c o s tl a r g ea mo u n t so fme mo r ya n ds l o wsd o wnc o mp i l a t i o n .Ast h ep r o j e c tg r o ws , wh a ts t a r t sa sac o mp i l et i meo fas e c o n d c a ng r o wt os e v e r a l , t h e nmi n u t e sa n dp e r h a p se v e nh o u r s . Ats o mep o i n t , t h e r ewa sa l s ot h ep r o b l e mt h a tt h ec o mp i l e rc o u l d n ' th a n d l ef i l e se x c e e d i n g4 MB, p u t t i n gal i mi to nh o w mu c hy o uc o u l d#i n c l u d ei naCf i l e .I ' mn o ts u r ei ft h i si ss t i l l a ni s s u e . Recompiling the world.Th ema i np r o b l e mi st h a twh e ny o u#i n c l u d ee v e r y t h i n g , y o un e e dt or e c o mp i l ee v e r y t h i n ga swe l l . I fy o uma k eo n ec h a n g eanywhere, n oma t t e rh o ws ma l l , c a u s e severything t ob ec o mp i l e d .Fo rs ma l l p r o j e c t s( s a y , ah a n d f u l o ff i l e s ) , af u l l r e b u i l dwo u l dt a k eaf e ws e c o n d ss oi t ' sn o tap r o b l e m.Bu tl a r g e rp r o j e c t sc a nh a v eh u n d r e d so rt h o u s a n d so f f i l e s , a n dt h et i mei sn o tme a s u r e di ns e c o n d s , b u ti nmi n u t e so rp e r h a p sh o u r s .Su r ei t ' sag o o de x c u s et og os wo r df i g h t i n g , b u tt e r r i b l ya n n o y i n gi fy o uwa n tt od os o me t h i n gp r o d u c t i v e . Bloat.Ev e ni fy o u ro wnc o d ea n dd a t aa r er e l a t i v e l ys ma l l i nn u mb e r , y o u ' r ep r o b a b l yu s i n gs o mec o d el i b r a r yf o rAPI f u n c t i o n s .No r ma l l y , t h e s ea r ep r e c o mp i l e da n do n l yt h ef u n c t i o n su s e da r el i n k e di n t oy o u rb i n a r y .Bu ti ft h o s ewo r k e db y #i n c l u d ea swe l l ( i no t h e rwo r d s , i ft h e i rc r e a t o r sh a df o l l o we dt h ep r a c t i c eI ' mwa r n i n ga g a i n s t ) , e v e r yf u n c t i o ni nt h a tl i b r a r y wo u l db ei n c l u d e da swe l l , i n c l u d i n gt h eo n e sy o u ' r en o tu s i n g .Th i si n c r e a s e st h ef i l e s i z e , and i n c r e a s e st h ep r o b l e ms me n t i o n e da b o v e . Undeclared identifiers, multiple definitions and circular dependencies.I nan u t s h e l l , Cr e q u i r e st h a ty o ud e c l a r ea n i d e n t i f i e rb e f o r ei t ' sr e f e r e n c e d , a n di tc a no n l yb ed e f i n e do n c e .Th ef i r s tp o i n tme a n st h a tt h eo r d e ro fi n c l u s i o n ss t a r t st o ma t t e r : i f , s a y , fileB.cn e e d ss o me t h i n gf r o mfileA.c, t h el a t t e rn e e d st ob ei n c l u d e db e f o r et h ef o r me rt og e tac o mp i l e . Th es e c o n dme a n st h a ty o uc o u l do n l y#i n c l u d eaf i l eo n c ei nt h ewh o l ep r o j e c t : i ffileB.ca n dfileC.cb o t hn e e ds t u f f f r o mfileA.c, y o uc a n ' t#i n c l u d ei ti nt h e mb o t hb e c a u s ewh e nt h e y ' r e#i n c l u d e di nmain.c, fileA.ci se f f e c t i v e l y #i n c l u d e dt wi c ea n dt h ec o mp i l e rwi l l b a l k . Th e s ep o i n t sc a nt e c h n i c a l l yb eo v e r c o meb yb e i n gc a r e f u l .Bu t , a g a i n , wh e np r o j e c t sg r o w, t h i n g sc a ng e ti n c r e a s i n g l y mo r ed i f f i c u l tt ok e e pt r a c ko fwh i c hc o me sb e f o r ewh a ta n dwh y .Th e r ei s , h o we v e r , o n ep o i n ta twh i c hi twill g owr o n g , n a me l ywh e nt h e r ea r ec i r c u l a rd e p e n d e n c i e s : fileB.cn e e d sfileA.ca n dv i c ev e r s a .Ea c hf i l ewo u l dr e q u i r et h eo t h e r t og of i r s t , wh i c hs i mp l yi s n ' tp o s s i b l eb e c a u s ei t ' dc a u s emu l t i p l ed e f i n i t i o n s . Data alignment.I ' l l g e tt owh a tt h i sme a n si nami n u t e , b u tr i g h tn o wk n o wt h a tc o p yr o u t i n e swo r kb e t t e ri ft h ed a t ai s a l i g n e dt o3 2 b i tb o u n d a r i e s( e v e nf o rb y t ea n dh a l f wo r da r r a y s ) .So meo ft h e mwo n ' te v e nwo r kp r o p e r l yi ft h i si s n ' tt h ec a s e . Th i si su s u a l l yg u a r a n t e e di fy o uc o mp i l es e p a r a t e l y , b u ti ft h ea r r a y sa r e#i n c l u d e da n dn os t e p sh a v eb e e nt a k e nt of o r c e a l i g n me n t , y o us i mp l yn e v e rk n o w. I t ' sn o tmu c ho fap r o b l e mn o wa d a y sb e c a u s emo s tg r a p h i c sc o n v e r t e r sf o r c ed a t a a l i g n me n t , b u ty o us t i l l n e e dt ok n o w a b o u ti t .Be c a u s ed a t aa l i g n me n ti saf a i r l ye s o t h e r i cc o n c e p t , i t ' sn e x tt oi mp o s s i b l et ot r a c kd o wnu n l e s sy o u ' r ea wa r eo ft h e p r o b l e msi tc a nb r i n g . Sop l e a s e , d oy o u r s e l faf a v o ra n dd on o t#i n c l u d ee v e r yf i l ey o uh a v ei n t omain.co ri t smo r a l e q u i v a l e n t .Pu tf u n c t i o na n dv a r i a b l e d e f i n i t i o n si ns e p a r a t es o u r c ef i l e st ob ec o mp i l e ds e p a r a t e l ya n dl i n k e dl a t e r .Th e#i n c l u d ed i r e c t i v ei so n l yt ob eu s e df o rf i l e swi t h p r e p r o c e s s o rd i r e c t i v e sa n dd e c l a r a c t i o n sa n dt y p ed e f i n i t i o n s .Oh , a n di n l i n ef u n c t i o n s .Th o s ea r eo k a yt h e r et o o . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 49/331 28-03-13 Tonc : GBA Programming in rot13 5.4.4. Proper build procedure Separate compilation Sowh a td oy o ud oi n s t e a d ?We l l , f o rs t a r t e r sk e e pa l l t h ec o d ea n dd a t ai ns e p a r a t es o u r c ef i l e s .Co mp i l et h e s es e p a r a t e l yb yi n v o k i n g g c co ne a c hf i l e .Th i sg i v e sy o ual i s to fo b j e c tf i l e s .Th e s ey o ut h e nl i n kt o g e t h e r .I nb a t c hf i l e s , y o u ' dn e e dt oa d de x t r ac o mma n d sf o r e a c hf i l e , b u tap r o p e r l ys e t u pma k e f i l eu s e sal i s to fo b j e c tf i l e s , a n dt h ema k e f i l e ' sr u l e swi l l t h e nt a k ec a r eo ft h er e s ta u t o ma t i c a l l y . Us i n gt h ema k e f i l eo ft h es e c o n dd e moa sar e f e r e n c e , y o u ' l l g e ts o me t h i n gl i k et h i s : # partial makefile for using multiple source files # some steps omitted for clarity # 3 targets for compilation OBJS := foo.o bar.o boo.o # link step: .o -> .elf $(PROJ).elf : $(OBJS) $(LD) $^ $(LDFLAGS) -o $@ # compile step .c -> .o $(OBJS) : %.o : %.c $(CC) -c $< $(CFLAGS) -o $@ Th eOBJSv a r i a b l ec o n t a i n st h en a me so ft h r e eo b j e c tf i l e s , wh i c hwo u l db et h et a r g e t so fc o mp i l i n gfoo.c, bar.ca n dboo.c. Re me mb e r , ma k e f i l e sl i s tr u l e sb yt a r g e t , n o tb yp r e r e q u i s i t e .Th ec o mp i l a t i o ns t e pu s e sas t a t i cp a t t e r nr u l e , wh i c hf o re a c h‘ . o ’f i l ei n OBJSc o mp i l e st h e‘ . c ’f i l ewi t ht h es a met i t l e .Th i si swh a tr u n st h ec o mp i l e rf o ro u rt h r e es o u r c ef i l e s .I nt h el i n k i n gs t e pt h ea u t o ma t i c v a r i a b l e$^e x p a n d st ot h ep r e r e q u i s i t e so ft h er u l e , wh i c hi st h el i s to fa l l o b j e c tf i l e s , a n dt h i si sh o wt h ef i l e sa r ea l l l i n k e dt o g e t h e r .I f y o un e e dmo r ef i l e s , a d dt h e mt ot h eOBJSl i s t . No t et h a tt h ed e v k i t Pr oa n dt o n ct e mp l a t ef i l e st a k ec a r eo ft h e s et h i n g sa u t o ma t i c a l l y .J u s tp u tt h es o u r c ef i l e si n t ot h er i g h t d i r e c t o r ya n dy o u ' r eg o o dt og o . Symbols, declarations and definitions I fy o uh a v eb e e nd o i n ge v e r y t h i n gv i a#i n c l u d e , y o us h o u l dc o n s i d e rr e f a c t o r i n ga l l o fy o u rs t u f ft os e p a r a t es o u r c ef i l e s .No , l e tme r e p h r a s et h a t , y o uneed t od ot h i sb e c a u s ey o u ' l l b e n e f i tf r o mi ti nt h ee n d .I fy o u ' r ea l r e a d ywe l l i ny o u rp r o j e c t , t h i si sg o i n gt os u c k b e c a u s ei t ' sb o r i n ga n dt i mec o n s u mi n ga n dmo s tl i k e l yi t ' sn o te v e ng o i n gt owork p r o p e r l ywh e ny o ut r yt h ef i r s tb u i l da f t e r wa r d s .I e x p e c ty o u ' l l g e tawh o l es l e wo fe r r o r s , p a r t i c u l a r l yt h e s et h r e e : f̀ o o ' u n d e c l a r e d r e d e f i n i t i o no ff̀ o o ' mu l t i p l ed e f i n i t i o no ff̀ o o ' Tou n d e r s t a n dwh a tt h e s eme a n , y o un e e dt ok n o wal i t t l eb i tmo r ea b o u th o wC( a n di n d e e dp r o g r a ms )a c t u a l l ywo r k s . AsIs a i db e f o r e , t h e r ea r e n ' tr e a l l yt h i n g sl i k ep r o g r a ms , b i t ma p s , s o u n do nc o mp u t e r s ;i t ' sa l l j u s tb i t s .Bi t s , b i t sa n dmo r eb i t s .Wh a t ma k e sas e q u e n c eo fb i t swo r ka sap r o g r a mi st h ewa yi ti sf e dt ot h eCPU, VRAM, a n do t h e rs e c t i o n s .So me wh e r ei nt h eb u i l d p r o c e s s , t h e r eh a st ob eat r a n s l a t i o no fa l l t h eCc o d et od a t aa n dma c h i n ei n s t r u c t i o n s .Th i s , o fc o u r s e , i st h ec o mp i l e r ' sj o b . Bu twa i t , t h e r e ' smo r e .Ca l l o wsy o ut oc o mp i l ee a c hf i l es e p a r a t e l y , a n dt h e nl i n kt h e ml a t e ri n t ot h ea c t u a l p r o g r a m.Th i si sag o o d i d e a , b e c a u s ei ta l l o wsy o ut os a v et i meb yo n l yc o mp i l i n gt h ef i l e st h a ty o uh a v er e c e n t l ymo d i f i e d , a swe l l a st h eu s eo fc o d elibraries, wh i c ha r el i t t l emo r et h a nab u n c ho fp r e c o mp i l e ds o u r c ef i l e s .I fy o u ' r en o tc o n v i n c e dt h a tt h i si sag o o di d e a , c o n s i d e rwh a ti two u l d t a k ewi t h o u ti t .Yo u ' dh a v et oh a v eall t h es o u r c ec o d et h a ty o uwa n t e dt ou s e( i n c l u d i n gf o rt h i n g sl i k eprintf()a n da l l t h eAPI c o d e ) , a n dc o mp i l ea l l t h o s eme g a b y t e so fs o u r c ef i l e se a c ht i me .So u n d sl i k ef u n ?No , Id i d n ' tt h i n ks oe i t h e r . Ho we v e r , y o un e e dal i t t l emo r eb o o k k e e p i n gt oma k et h i sa l l wo r k .Be c a u s ee v e r y t h i n gi sj u s tb i t s , y o u ' dn e e dawa yt of i n do u t wh e r et h ef u n c t i o no rd a t ay o uwa n tt ou s ea c t u a l l yi s .Th ec o n t e n t so ft h ec o mp i l e df i l e s( t h eo b j e c tf i l e s )i s n ' tj u s tr a wb i n a r y , i t c o n t a i n ssymbols.Th i si sj u s tawo r df o rt h eg r o u po ft h i n g st h a th a v ea c t u a l b i n a r yi n f o r ma t i o na t t a c h e dt ot h e m.Amo n go t h e rt h i n g s , t h eo b j e c tf i l ek e e p st r a c ko ft h es y mb o l ' sn a me , s e c t i o n , s i z e , a n dwh e r ei t sc o n t e n ti si nt h eo b j e c tf i l e .Af u n c t i o ni sas y mb o l , b e c a u s e i tc o n t a i n si n s t r u c t i o n s .Av a r i a b l ei sa l s oas y mb o l , a si sd a t af o rb i t ma p s , s o u n d , ma p se tc e t e r a .Pr e p r o c e s s o r#d e f i n e s , t y p e d e f sa n d s t r u c t / c l a s sd e c l a r a t i o n sa r enot s y mb o l s , b e c a u s et h e yo n l yd o n ' th a v ea c t u a l c o n t e n ti nt h e m, b u ta l l o wy o ut os t r u c t u r ey o u rc o d e b e t t e r . Th eo t h e rb o o k k e e p i n gn o t ei st h a te a c hs o u r c e / o b j e c tf i l ei sas e p a r a t ee n t i t y .I np r i n c i p l e , i tk n o wsn o t h i n ga b o u tt h eo u t s i d ewo r l d . Th i sma k e ss e n s eb e c a u s ei tl i mi t st h ed e p e n d e n c yo no t h e rf i l e s , b u ti td o e sc r e a t eal i t t l ep r o b l e mwh e ny o uwa n tt oma k ef i l e swo r k t o g e t h e r .Th i si swh e r edeclarations c o mei n . Yo uma yh a v en o t i c e dt h a tCi sp r e t t ys t r i c twh e ni tc o me st on a me so fs t u f f .Be f o r ey o uc a nu s ea n y t h i n g , i tr e q u i r e sy o ut o me n t i o nwh a ti ti sb e f o r e h a n d .Fo re x a mp l e , i fy o uu s eaf u n c t i o nfoo()i ny o u rc o d ea n dy o un e v e rd e f i n e di t sc o d e , o re v e ni fy o up u t i ta f t e rt h ec a l l t ofoo(), t h ec o mp i l e rwi l l c o mp l a i nt h a ti td o e s n ' tk n o wwh a ty o u ' r et a l k i n ga b o u t .Th a ti s , i twi l l s a yt h a t‘f̀ o o ' i s u n d e c l a r e d ’ .Yo uh a v et oa d mi ti th a sar i g h tt os t o pt h e r e : h o wwo u l di tk n o wh o wt ou s et h et h i n gi fy o un e v e rt o l di twh a ti twa s ? Th ec o d es n i p p e tb e l o wg i v e sa ne x a mp l eo fwh e nar e f e r e n c ei sa n di sn o td e c l a r e d , a n dwh yi t ' si mp o r t a n tt oh a v ead e c l a r a t i o n . Fu n c t i o na()c a l l sfoo(), wh i c hi sn o tk n o wna tt h et i me , s oa ne r r o ri sp r o d u c e d .Fu n c t i o nb()a l s oc a l l sfoo(), wh i c his k n o wna t t h a tt i me , b u ts t i l l g i v e sa ne r r o rb e c a u s efoo()j u s th a p p e n st or e q u i r ea ni n t e g e ra sa na r g u me n t .I ft h ed e c l a r a t i o nwa s n ' tma n d a t o r y file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 50/331 28-03-13 Tonc : GBA Programming in rot13 a n dt h ec a l l i na()wa sa l l o we d , foo()wo u l dh a v eb e e np r o c e s s i n gt h ewr o n gk i n do fi n f o r ma t i o na tr u n t i me .Th e r ea r ewa y sa r o u n d s u c hp r o b l e ms , o fc o u r s e , l a n g u a g e sl i k ePHP, VBa n do t h e r swo r kf i n ewi t h o u tma n d a t o r yd e c l a r a t i o n s , b u tt h ec o s tf o rt h a ti ss p e e d a n dp o s s i b l yal o tmo r er u n t i mee r r o r s . //# C requires identifiers to be declared or defined before first use. // ERROR: `foo' is undefined. void a() { foo(); } // Definition of foo(). Now the system 'knows' what foo is. void foo(int x) { // code } // foo is known and used correctly: no errors. void b() { foo(42); } // foo is known but used incorrectly. Compiler issues error. void c() { foo(); } No wb a c kt oo u rs e p a r a t ef i l e s , a n dt h ed i f f e r e n c eb e t we e nd e c l a r a t i o n sa n dd e f i n i t i o n so fs y mb o l s .Adefinition i ss o me t h i n go fa c t u a l c o n t e n t : i ti swh a ta c t u a l l yf o r mst h es y mb o l .Ex a mp l e sa r et h ev a l u e ( s )i nv a r i a b l e s , a n dt h ec o d ei nf u n c t i o n s .Adeclaration i sj u s ta n e mp t yr e f e r e n c e .I tj u s ts a y st h a tt h e r ei ssomething i nt h ep r o j e c twi t hap a r t i c u l a rn a me , a n di n d i c a t e sh o wt h a ts o me t h i n gi ss u p p o s e d t ob eu s e d : wh e t h e ri t ' saf u n c t i o no rv a r i a b l e , wh a td a t a t y p e , wh i c ha r g u me n t s , t h a ts o r to ft h i n g s .Th i si sh o wy o uc a nu s es y mb o l s f r o mo t h e ro b j e c tf i l e s . Yo us h o u l db ef a mi l i a rwi t hwh a tad e f i n i t i o nl o o k sl i k e .Ad e c l a r a t i o nl o o k sv e r ys i mi l a r .Th eb a s i cv a r i a b l ed e c l a r a t i o ni st h e v a r i a b l en a mea n da t t r i b u t e s( t y p e , c o n s t , s e c t i o n )p r e c e d e db yextern.Fo rf u n c t i o n s , r e p l a c et h ec o d eb l o c kb yas e mi c o l o n .Yo u c a na l s oa d dexternt h e r e , b u ti t ' sn o tr e q u i r e d . // -------------------------------------------------------------------// DECLARATIONS. Put these in source (.c) or header (.h) files. // -------------------------------------------------------------------extern int var; extern const unsigned int data[256]; void foo(int x); // -------------------------------------------------------------------// DEFINITIONS. Put these in source (.c) only. // -------------------------------------------------------------------// uninitialized definition int var; // initialized definition const unsigned int data[256]= { // data }; void foo(int x) { // code } No w, ad e f i n i t i o ni sa l s oad e c l a r a t i o n , b u tt h i sd o e snot wo r kt h eo t h e rwa y .Ho wc a ni t , t h ed e c l a r a t i o ni ss u p p o s e dt ob ee mp t y .Th e d i s t i n c t i o ni ss u b t l e , b u ti t ' st h er e a s o ny o umi g h tg e tmu l t i p l ed e f i n i t i o ne r r o r swh e nl i n k i n gt h ef i l e st o g e t h e r .Th i n ko fwh a two u l d h a p p e ni fy o uh a v et h ed e f i n i t i o no ff u n c t i o nfoo()i nmu l t i p l ef i l e s .Ea c hf i l ei t s e l fwo u l dk n o wwh a tfoo()i sb e c a u s ed e f i n i t i o n sa r e a l s od e c l a r a t i o n s , s oi two u l dp a s st h ec o mp i l a t i o ns t a g e .Son o wy o uh a v emu l t i p l eo b j e c tf i l e s , e a c hc o n t a i n i n gas y mb o l c a l l e dfoo. Bu tt h e ny o ut r yt ol i n kt h e mi n t oo n ef i l e .Th el i n k e rs e e sd i f f e r e n tv e r s i o n so ffoo, a n ds t o p sb e c a u s ei td o e s n ' tk n o wwh i c ho n ey o u file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 51/331 28-03-13 Tonc : GBA Programming in rot13 a r ea c t u a l l yt r y i n gt ou s e .Th emo r a l h e r ei st h a ty o uc a nh a v ea sma n yd e c l a r a t i o n sa sy o uwa n t , b u tt h e r ec a nb eo n l yone d e f i n i t i o n . An o t h e rp o i n tIs h o u l dr a i s ei st h a tt h ed e c l a r a t i o nd e f i n e sh o was y mb o l i st ob ed e a l twi t h , a si ti st h eo n l yp o i n to fr e f e r e n c ei ft h e d e f i n i t i o ni si na n o t h e rf i l e .Th i sme a n st h a t , i nt h e o r y , y o uc o u l dh a v eav a r i a b l evard e f i n e da sa ni n t , b u td e c l a r e da sas h o r t , o re v e na f u n c t i o n !Wh i l en o te x a c t l yr e c o mme n d e d , b u ti ti sa ni n t e r e s t i n gi t e m. La s t l y : t h ed i s t i n c t i o no fwh a ts h o u l dg oi ns o u r c ef i l e s , a n dwh a ti nh e a d e r s .So u r c ef i l e sc a na c t u a l l yc o n t a i na n y t h i n g , s ot h a t ' sa n e a s yo n e .Re me mb e rt h a tt h e ywi l l c o n t a i ne v e r y t h i n ga f t e rt h ep r e p r o c e s s o rs t e pa n y wa y , b e c a u s et h a t ' swh a t#i n c l u d er e a l l yd o e s .So wh a tma t t e r si swh a ty o up u ti nh e a d e r s .Th ep u r p o s eo fh e a d e rf i l e si st oh a v eap l a c ef o ra l l t h enon-symbol s t u f ft h a ty o uwa n tt o u s ei nd i f f e r e n ts o u r c ef i l e s .Th a tme a n sd e c l a r a t i o n s , #d e f i n e s , ma c r o s , t y p e d e f s , s t r u c t / c l a s sd e s c r i p t i o n s .I ta l s ome a n s( s t a t i c )i n l i n e f u n c t i o n s , b e c a u s et h e s ed o n ' tf o r ms y mb o l se i t h e r , b u ta r ei n t e g r a t e di nt h ef u n c t i o n st h a tc a l l t h e m. Summary Al l t h i ss t u f fa b o u ts e p a r a t ec o mp i l a t i o n , d e c l a r a t i o n s , a n dd e f i n i t i o n si sr a t h e ri mp o r t a n tf o rCp r o g r a mmi n g , b u tt h ep r e c e d i n gt e x tma y b eal i t t l emu c ht ot a k ei na to n c e .Soh e r ei sas u mma r yo ft h emo s ti mp o r t a n tp o i n t s . Symbols.Sy mb o l sa r et h o s ep a r t so ft h ec o d et h a tf o r ma c t u a l b i n a r yc o n t e n ti nt h ef i n a l p r o g r a m.Th i si n c l u d e sf u n c t i o n s , v a r i a b l e s , d a t a , b u tn o tp r e p r o c e s s o ro rt y p ed e s c r i p t i o ns t u f f . Declarations/definitions.Ad e f i n i t i o no fas y mb o l i swh e r et h ea c t u a l c o n t e n ti s .Ad e c l a r a t i o nj u s ts a y st h a ts o me t h i n go fa c e r t a i nn a mee x i s t s , b u twi l l b ea d d e dt ot h ep r o j e c tl a t e r .Mu l t i p l e( i d e n t i c a l )d e c l a r a t i o n sma ye x i s t , b u tt h e r ec a nb eo n l yo n e d e f i n i t i o ni nt h ep r o j e c t .De f i n i t i o n sa r ea l s od e c l a r a t i o n s . Source/object files are selfcontained entities.Th e yc o n t a i nt h ed e f i n i t i o n so ft h es y mb o l st h a ta r ei nt h ec o d e , a n dal i s to f r e f e r e n c e st oo u t s i d es y mb o l s , a si n d i c a t e db yt h ed e c l a r a t i o n s . Header files contain meta-data, not symbols.He a d e rf i l e sc a n n o tb ec o mp i l e d , b u ta r ei n t e n d e dc o n t a i nt h e‘ g l u e ’t h a t a l l o wd i f f e r e n c es o u r c e st owo r kt o g e t h e r( i . e . , d e c l a r a t i o n s )a n ds t u f ft h a tma k e swr i t i n gt h es o u r c e se a s i e r( l i k e#d e f i n e sa n d ma c r o s ) .Th e ya r eme a n tt ob ei n c l u d e di nmu l t i p l ef i l e s , s ot h e yc a n n o tc r e a t es y mb o l sb e c a u s et h a two u l dl e a dt omu l t i p l e d e f i n i t i o n s . Po t e n t i a l p r o b l e msd u r i n gc o mp i l a t i o no rl i n k i n g : `foo' undeclared.Co mp i l e re r r o r .Th ei d e n t i f i e rf̀ o o ' i sn o tk n o wna tt h i sp o i n t .Ch e c kt h es p e l l i n g , o ra d dt h ea p p r o p r i a t e d e c l a r a t i o no rh e a d e rf i l ec o n t a i n i n gt h ed e c l a r a t i o n . redefinition of `foo'.Co mp i l e re r r o r .Th ei d e n t i f i e ra sap r e v i o u sd e c l a r a t i o no rd e f i n i t i o nc o n f l i c t i n gwi t ht h ec u r r e n to n ei n t h es a mef i l eo ri n c l u d e dh e a d e r s .Us u a l l ya c c o mp a n i e db yame s s a g eo ft h ep r e v i o u sd e f i n i t i o n . multiple definition of 'foo'.Li n k e re r r o r .Th es y mb o l n a mef̀ o o ' i ss h a r e db ymu l t i p l eo b j e c tf i l e s .Re p l a c ea l l b u to n e d e f i n i t i o n so ff̀ o o ' i nt h es o u r c ef i l e swi t ht h ea p p r o p r i a t ed e c l a r a t i o n s .Us u a l l ya c c o mp a n i e dwi t hame s s a g ei n d i c a t i n gt h e o b j e c tf i l ewi t ht h eo t h e rd e f i n i t i o n ( s ) . 5.4.5. Data alignment Da t aa l i g n me n ti sa b o u tt h e‘ n a t u r a l ’me mo r ya d d r e s s e so fv a r i a b l e s .I ti so f t e nb e n e f i c i a l t oh a v eav a r i a b l eo fac e r t a i nl e n g t ht os t a r t a ta na d d r e s sd i v i s i b l eb yt h a tl e n g t h .Fo re x a mp l e , a3 2 b i tv a r i a b l el i k e st ob ep u ta ta d d r e s s e st h a ta r eamu l t i p l eo f4 .Pr o c e s s o r s t h e ms e l v e sa l s oh a v ec e r t a i np r e f e r r e da l i g n me n t s .Ad d r e s s i n gwi l l wo r kf a s t e ri fy o us t i c kt ot h e i rn a t i v et y p e sa n da l i g n me n t( s a y , 3 2 b i te v e r y t h i n gf o r3 2 b i tCPUs ) .Fo rPCsi ti sn o tr e q u i r e dt od oa n yo ft h i s , i t ' l l j u s tr u ns l o we r .Fo rRI SCs y s t e ms , h o we v e r , t h i n g smust b ea l i g n e dp r o p e r l yo rd a t ag e t sma n g l e d . I nmo s tc a s e s , t h ec o mp i l e rwi l l a l i g nt h i n g sf o ry o u .I twi l l p u ta l l h a l f wo r d so ne v e nb o u n d a r i e sa n dwo r d so nq u a d b y t eb o u n d a r i e s . Asl o n ga sy o us t i c kt ot h en o r ma l p r o g r a mmi n gr u l e s , y o uc a nr e ma i nc o mp l e t e l yo b l i v i o u st ot h i sa l i g n me n ts t u f f .Ex c e p tt h a ty o uwon't a l wa y ss t i c kt ot h er u l e s .I nf a c t , Ci sal a n g u a g et h a ta l l o wsy o ut ob r e a kt h er u l e swh e n e v e ry o uf e e l l i k ei t .I tt r u s t sy o ut ok n o wwh a t y o u ' r ed o i n g .Wh e t h e rt h a tt r u s ti sa l wa y sj u s t i f i e di sa n o t h e rma t t e r:P Th eb e s te x a mp l eo fb r e a k i n gt h er u l e si sp o i n t e rc a s t i n g .Fo re x a mp l e , mo s tg r a p h i c sc o n v e r t e r swi l l o u t p u tt h ed a t aa su 1 6 a r r a y s , s oy o uc a nc o p yi tt oVRAM wi t has i mp l ef o r l o o p .Yo uc a ns p e e du pc o p y i n gb yr o u g h l y1 6 0 %i fy o uc o p yb ywo r d s( 3 2 b i t )r a t h e r t h a nh a l f wo r d s( 1 6 b i t ) .Ru nt h et x t _s e 2d e moa n ds e ef o ry o u r s e l f .Al l y o uh a v et od of o rt h i si so n eo rt wop o i n t e rc a s t s , a ss h o wn h e r e . #define fooSize ... const u16 fooData[]= { ... }; // copy via u16 array (the de facto standard) u16 *dst= (u16*)vid_mem, *src= (u16*)fooData; for(ii=0; ii<fooSize/2; ii++) dst[ii]= src[ii]; // copy via u32 array (mooch faster) u32 *dst= (u32*)vid_mem, *src= (u32*)fooData; for(ii=0; ii<fooSize/4; ii++) dst[ii]= src[ii]; Bo t ht h e s er o u t i n e sc o p yfooSizeb y t e sf r o mfooDatat oVRAM.On l yt h es e c o n dv e r s i o ni smu c hf a s t e rb e c a u s et h e r ea r eh a l fa s ma n yl o o pi t e r a t i o n sa n da l s ob e c a u s et h eARM CPUi sj u s tb e t t e ra td e a l i n gwi t h3 2 b i tc h u n k s .Th eo n l yd a n g e rh e r ei st h a twh i l e fooDatawi l l b eh a l f wo r da l i g n e d , i tn e e dnot b ewo r da l i g n e d , wh i c hi sar e q u i r e me n tf o rt h es e c o n dv e r s i o n .Fo rt h o s er e a d e r st h a t file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 52/331 28-03-13 Tonc : GBA Programming in rot13 t h i n kc a s t sl i k et h i sa n dmi s a l i g n me n to n l yh a p p e nt oo t h e rp e o p l e , t h i n ka g a i n : t h ef a s t e rc o p yr o u t i n e s( memcpy(), CpuFastSet(), a n dDMAt o o )c a s tt owo r d p o i n t e r si mp l i c i t l y .Us et h e m( a n dy o us h o u l d )a n dy o ur u nt h er i s ko fmi s a l i g n me n t . Th e r ea r ema n ywa y so fe n s u r i n gp r o p e ra l i g n me n t .Th ee a s i e s twa yi st on o tmi xc o n v e r t e dd a t awi t ht h er e s to fy o u rs t u f f .Th a ti s , d o n ' t#i n c l u d ed a t a f i l e s .Th i ss h o u l ds u f f i c e .An o t h e rme t h o di st oc o n v e r tt ou 3 2 a r r a y si nt h ef i r s tp l a c e .I na s s e mb l yf i l e s , y o uc a n nb c o n t r o l a l i g n me n tb yu s i n gt h e‘ . a l i g nn’d i r e c t i v e , wh e r en a l i g n st o2 y t e s .Ci t s e l fd o e s n ' ta l l o wma n u a l a l i g n me n t , b u tt h e r ei sa n e x t e n s i o nf o rt h i si nGCC: ‘ __attribute__(( aligned(4) ))’ .Ad dt h a tt ot h ed e f i n i t i o na n di t ' l l b ewo r da l i g n e d .Th i si s o f t e n#d e f i n e da sALIGN4i ns o meh e a d e r s .Fi l e si nGBFSa r ea l s oa l wa y sc o r r e c t l ya l i g n e d . Struct alignment On eo t h e ra r e awh e r ea l i g n me n tc a nc a u s ep r o b l e msi si ns t r u c td e f i n i t i o n s .Lo o ka tt h ef o l l o wi n gc o d e .He r eweh a v eas t r u c tn a me d FOOc o n s i s t i n go fo n eb y t e , b, o n ewo r dw a n do n eh a l f wo r dh.Sot h a t ' s1 +4 +2 =7b y t e sf o rt h es t r u c tr i g h t ?Wr o n g .Be c a u s eo ft h e a l i g n me n tr e q u i r e me n t , wd o e s n ' ti mme d i a t e l yf o l l o wb b u tl e a v e s3b y t e so fp a d d i n g .Wh e nd e f i n i n ga r r a y so ft h i st y p e , y o u ' l l a l s os e e t h a tt h e r ea r ea l s ot wop a d d i n gb y t e sa f t e rh, b e c a u s eo t h e r wi s el a t e ra r r a y e n t r i e swo u l dr u ni n t ot r o u b l e . // one byte, one word, one halfword. 7 byte struct? // Well let's see ... struct FOO { u8 b; u32 w; u16 h; }; // Define a FOO array struct FOO foos[4]= { { 0x10, 0x14131211, 0x1615 }, { 0x20, 0x24232221, 0x2625 }, { 0x30, 0x34333231, 0x3635 }, { 0x40, 0x44434241, 0x4645 }, }; // In memory. 4x12 bytes. // 10 00 00 00 | 11 12 13 14 | 15 16 00 00 // 20 00 00 00 | 21 22 23 24 | 25 26 00 00 // 30 00 00 00 | 31 32 33 34 | 35 36 00 00 // 40 00 00 00 | 41 42 43 44 | 45 46 00 00 Th ereal s i z ei sa c t u a l l y1 2b y t e s .No to n l yi st h i sa l mo s tt wi c et h es i z e , i fy o ue v e rt r yt oc o p yt h ea r r a yu s i n gah a r d c o d e d7r a t h e r t h a nsizeof(struct FOO), y o uc o mp l e t e l yme s si tu p .Ta k et h i sl e s s o nt oh e a r t .I t ' sav e r ye a s ymi s t a k et oma k ea n dd i f f i c u l tt o d e t e c ta f t e rt h ef a c t .I fy o uwe r eu n a wa r eo ft h i sf a c ta n dy o u ' v ea l r e a d yd o n es o meGBAc o d i n g , c h e c ky o u rs t r u c t s( o rc l a s s e s )n o w; c h a n c e sa r et h e r ea r eg a p st h a ts h o u l d n ' tb et h e r e .Si mp l yr e a r r a n g i n gs o meo ft h eme mb e r ss h o u l ds u f f i c et oma k et h e mf i tb e t t e r . No t et h a tt h i si sn o ts p e c i f i ct ot h eGBA: s t r u c t so nPCsma yb e h a v et h es a mewa y , a sIn o t i c e dwh e nIwa swr i t i n gmyTGAf u n c t i o n s . Th e r ea r ewa y so ff o r c i n gp a c k i n g , u s i n gt h e‘ __attribute__((packed))’a t t r i b u t e .I ft h eFOOs t r u c th a dt h a t , i tr e a l l y wo u l db e7b y t e sl o n g .Th ed o wn s i d eo ft h i si st h a tt h en o n b y t eme mb e r sc o u l db emi s a l i g n e da n dh a v et ob ep u tt o g e t h e rb y t ef o rb y t e ( t h ec o mp i l e rd o e st h i sf o ry o u ) .Th i si sv e r ymu c hs l o we rt h a nt h en o n p a c k e dv e r s i o n , s oo n l yu s et h i sa t t r i b u t ei fy o uh a v en oo t h e r c h o i c e .Wh a th a p p e n swi t hmi s a l i g n e d( h a l f ) wo r d st h e nIc a n ' tt e l l y o ut h o u g h , b u tI ' ms u r ei t ' sn o tp r e t t y . Forcing alignment and packing GCCh a st woa t t r i b u t e st h a ta l l o wy o ut of o r c ea l i g n me n to fa r r a y s , a n dr e mo v eme mb e r a l i g n me n ti ns t r u c t s . // Useful macros #define ALIGN(n) #define PACKED __attribute__((aligned(n))) __attribute__((packed)) // force word alignment const u8 array[256] ALIGN(4) = {...}; typedef struct FOO {...} ALIGN(4) FOO; // force struct packing struct FOO {...} PACKED; Devkits and struct alignment Asf a ra sI ' v eb e e na b l et ot e l l , s t r u c t sh a v ea l wa y sh a dwo r da l i g n me n t .Th i swa su s e f u l b e c a u s ei tma d ec o p y i n gs t r u c t sf a s t e r .C a l l o wsy o ut oc o p ys t r u c t swi t has i n g l ea s s i g n me n t , j u s tl i k et h es t a n d a r dd a t at y p e s .Be c a u s eo ft h ewo r d a l i g n me n t , t h e s ec o p i e sa r e file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 53/331 28-03-13 Tonc : GBA Programming in rot13 f a s tb e c a u s eGCCwi l l ma k eu s eo fARM' sb l o c k c o p yi n s t r u c t i o n s , wh i c ha r emu c hf a s t e rt h a nc o p y i n gme mb e rb yme mb e r . Ho we v e r , t h i sd o e sn o ts e e mt ob et r u eu n d e rd e v k i t ARM r 1 9( a n dp r e s u ma b l yh i g h e r )a n y mo r e .Th en e wr u l es e e mst ob e“ s t r u c t s a r ea l i g n e dt ot h e i rl a r g e s tme mb e r ” .Th i sd o e sma k emo r es e n s ea sas t r u c to ft wob y t e swo u l da c t u a l l yb et wob y t e sl o n g .Ho we v e r , i t d o e sme a nt h a tGCCwi l l n o wc a l l memcpy()f o rn o n a l i g n e ds t r u c t s .Ap a r tf r o mi tb e i n gaf u n c t i o nwi t hq u i t eab i to fo v e r h e a d( i . e . , i t ' svery s l o wi fy o uwa n tt oc o p yas i n g l es ma l l s t r u c t ) , i twi l l a c t u a l l yfail t op r o d u c ec o r r e c tr e s u l t si ns o mec a s e s .Th ep r o b l e mi st h a t l o wn u mb e rc o p i e si twi l l c o p yb yt h eb y t e , wh i c hi ss o me t h i n gy o uc a n n o td of o rVRAM, PALRAM o rOAM.Fo re x a mp l e , o b j e c t s t h a twe ' l l s e el a t e ru s eas t r u c to ff o u rh a l f wo r d s ;u s i n gas t r u c t c o p yt h e r e , s o me t h i n gIa mv e r yf o n do fd o i n g , s c r e wsu pe v e r y t h i n g . Th eo n l ywa yt oma k ei two r kp r o p e r l yi st of o r c ewo r d a l i g n me n to nt h es t r u c t . // This doesn't work on devkitARM r19 anymore typedef struct OBJ_ATTR { u16 attr0, attr1, attr2; s16 fill; } OBJ_ATTR; OBJ_ATTR a, b; b= a; // Fails because of memcpy // Forcing alignment: this works properly again typedef struct OBJ_ATTR { u16 attr0, attr1, attr2; s16 fill; } ALIGN(4) OBJ_ATTR; OBJ_ATTR a, b; b= a; // No memcpy == no fail and over 10 times faster Forcing struct-alignment on devkitARM r19 is a Good Thing Th er u l e sf o rs t r u c ta l i g n me n th a v ec h a n g e ds i n c ed e v k i t ARM r 1 9 .I n s t e a do fb e i n ga l wa y swo r d a l i g n e d , t h e ya r en o w a l i g n e da swe l l a st h e i rme mb e r swi l l a l l o w.I ft h i sme a n st h e y ' r en o tn e c e s s a r i l ywo r d a l i g n e d , t h e nt h e ywi l l u s ememcpy() f o rs t r u c t c o p i e s , wh i c hi ss l o wf o rs ma l l s t r u c t s , a n dma ye v e nb ewr o n g( s e en e x ts e c t i o n ) .I fy o uwa n tt ob ea b l et od o s t r u c tc o p i e sf a s ta n ds a f e , e i t h e rf o r c ea l i g n me n to rc a s tt oo t h e rd a t a t y p e s . 5.4.6. Copying, memcpy() and sizeof Th e r ea r ema n yd i f f e r e n twa y so fc o p y i n gd a t ao nt h i sp l a t f o r m.Ar r a y s , s t r u c t c o p i e s , s t a n d a r dc o p i e r sl i k ememcpy(), a n dGBA s p e c i f i cr o u t i n e sl i k eCpuFastSet()a n dDMA.Al l o ft h e s eh a v et h e i ro wns t r e n g t h sa n dwe a k n e s s e s .Al l o ft h e mc a nb ea f f e c t e d b ymi s a l i g n me n ta n dt h en o b y t e wr i t er u l e .Id i s c u s ss o meo ft h e mi nt h et x t _s e 2d e mo . I ' v ec h o s e nt ou s ememcpy()i nt h ee a r l yd e mo sf o ran u mb e ro fr e a s o n s .Th ema i no n ei st h a ti ti sp a r to ft h es t a n d a r dCl i b r a r y , me a n i n gt h a tCp r o g r a mme r ss h o u l da l r e a d yb ef a mi l i a rwi t hi t .Se c o n d l y , i ti ss o me wh a to p t i mi z e d( s e et h etxt_se2d e mof o r d e t a i l s ) .Ho we v e r , t h e r ea r et wop o t e n t i a l p i t f a l l swi t ht h er o u t i n e .Th ef i r s ti sd a t aa l i g n me n t( y e s , that a g a i n ) .I feither t h es o u r c eor t h ed e s t i n a t i o ni sn o two r d a l i g n e d , y o u ' r ei nt r o u b l e .Se c o n d l y , i ft h en u mb e ro fb y t e si st o os ma l l , y o u ' r ei nt r o u b l et o o . Bo t ho ft h e s eh a v et od owi t ht h eb a s i cf u n c t i o no fmemcpy(), n a me l yt ob eaf a s tbyte c o p i e r .Bu ta sy o uk n o w, y o uc a n ' tc o p y s i n g l eb y t e st oVRAM d i r e c t l y .Fo r t u n a t e l y , i th a sa no p t i mi s e dmo d et h a tu s e sa nu n r o l l e dwo r d c o p yl o o pi ft woc o n d i t i o n sa r e s a t i s f i e d : 1 .Wh e nb o t hs o u r c ea n dd e s t i n a t i o n sa r ewo r da l i g n e d . 2 .Wh e ny o ua r ec o p y i n gmo r et h a n1 6b y t e s . Th i si su s u a l l yt h ec a s es oIf i g u r e di t ' db es a f ee n o u g hf o rt h ed e mo s .Th e r ea r ea l s ol o o k a l i k e si nt o n c l i bt h a td ot h es a met h i n go n l y b e t t e r , n a me l ymemcpy16()a n dmemcpy32(), b u tt h e s ea r ei na s s e mb l ys oIt h o u g h tIwo u l d n ' tl a yt h e mo ny o us os o o n .Hi g h l y r e c o mme n d e df o rl a t e rt h o u g h . Onar e l a t e ds u b j e c t , t h e r ei sa l s omemset()f o rme mo r yf i l l s .Bec a r e f u l wi t ht h a to n e , b e c a u s et h a twi l l only wo r kwi t hb y t e s . To n c l i ba l s oi n c l u d e s1 6 -a n d3 2 b i tv e r s i o n so ft h i sr o u t i n e , b u ta l s oi na s s e mb l y . Th el a s tt h i n gIwa n tt od i s c u s si st h esizeof()o p e r a t o r .I no t h e rt u t o r i a l sy o uwi l l s e et h i sb e i n gu s e dt of i n dt h es i z ei nb y t e so f a r r a y s , wh i c hi st h e nu s e di nmemcpy().I t ' sag o o dp r o c e d u r eb u twi l l n o ta l wa y swo r k .Fi r s t , sizeof()a c t u a l l yg i v e st h es i z eo f t h evariable, wh i c hn e e dn o ta l wa y sb et h ea r r a yi t s e l f .Fo re x a mp l e , i fy o uu s ei to nap o i n t e rt ot h ea r r a y , i t ' l l g i v et h es i z eo ft h e p o i n t e ra n dnot o ft h ea r r a y .Th ec o mp i l e rn e v e rc o mp l a i n s , b u ty o umi g h twh e nh a r d l ya n y t h i n gi sc o p i e d .Se c o n d l y , sizeof()i sa n operator, n o taf u n c t i o n .I ti sr e s o l v e da tc o mp i l e t i me , s oi tn e e d st ob ea b l et of i n dt h es i z ea tt h a tt i mea swe l l .Tod ot h i s , e i t h e rt h e d e c l a r a t i o n( i nt h eh e a d e r )s h o u l di n d i c a t et h es i z e , o rt h ea r r a yd e f i n i t i o n( i nt h es o u r c ef i l e )s h o u l db ev i s i b l e . Bo t t o ml i n e : y o uc a nu s esizeof(), j u s tp a ya t t e n t i o nt owh a ty o uu s ei to n . Ok a y , t h a twa st h el o n ga n db o r i n g–y e tn e c e s s a r y –s e c t i o no nd a t a .Co n g r a t u l a t i o n si fy o u ' v ema n a g e dt os t a ya wa k et i l l t h i sp o i n t , file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 54/331 28-03-13 Tonc : GBA Programming in rot13 e s p e c i a l l yi fy o u ' v ea c t u a l l yu n d e r s t o o da l l o fi t .I t ' so k a yi fy o ud i d n ' tt h o u g h , i nmo s tc a s e sy o uwo n ' tr u ni n t ot h ep r o b l e msd i s c u s s e d h e r e .Bu tj u s tr e me mb e rt h i ss e c t i o nf o ri fy o ud or u ni n t ot r o u b l ewh e nc o p y i n ga n dy o uc a n ' tf i n di ti nt h ec o d e ;i tmi g h ts a v ey o uaf e w h o u r so fd e b u g g i n g . 5.4.7. Data interpretation demo Th ebm_modesi sa ne x a mp l eo fh o wt h es a med a t ac a nr e s u l ti nd i f f e r e n tr e s u l t sd e p e n d i n go ni n t e r p r e t a t i o n( i nt h i sc a s e , mo d e s3 , 4 a n d5 ) .I nt h ec o d eb e l o w, Ima k eone c o p yi n t oVRAM, a n ds wi t c hb e t we e nt h emo d e su s i n gLe f ta n dRi g h t .Th er e s u l t sc a nb es e e n i nf i g s5 . 7 a c . I ' v ea r r a n g e dt h ed a t ao ft h eb i t ma pi ns u c hawa yt h a tt h en a meo ft h ec u r r e n tmo d ec a nb er e a dc l e a r l y , a swe l l a si n d i c a t e dt h e mo d e ' sb o u n d a r i e si nme mo r y .Be c a u s et h ed a t ai n t e n d e df o rt h eo t h e rmo d e si ss t i l l p r e s e n t , b u tn o ti n t e r p r e t e da si n t e n d e d , t h a tp a r to f t h eb i t ma pwi l l l o o kal i t t l es h i t t y .An dt h a t ' sp a r t l yt h ep o i n to ft h ed e mo : wh e nf i l l i n gVRAM, y o un e e dt ok n o wh o wt h eGBAwi l l u s e t h ed a t ai ni t , a n dma k es u r ei t ' l l b eu s e d .I ft h eb i t ma pe n d su pb e i n ga l l g a r b l e d , t h i si st h el i k e l ys u s p e c t ;c h e c kt h eb i t d e p t h , d i me n s i o n s a n df o r ma t( l i n e a r , t i l e d , c o mp r e s s e d , e t c )a n di fs o me t h i n gc o n f l i c t s , f i xi t . No w, s o me t i me st h i si sn o ta se a s ya si ts o u n d s .Th eg e n e r a l p r o c e d u r ef o rg r a p h i c si st oc r e a t ei to nt h ePC, t h e nu s ea ne x p o r t e r t o o l t oc o n v e r ti tt oar a wb i n a r yf o r ma t , t h e nc o p yi tt oVRAM.I ft h ee x p o r t e rh a sb e e ng i v e nt h ewr o n go p t i o n s , o ri fi tc a n ' th a n d l et h e i ma g ei nt h ef i r s tp l a c e , y o u ' l l g e tg a r b a g e .Th i sc a nh a p p e nwi t hs o meo ft h eo l d e rt o o l s .I ns o mec a s e s , i t ' st h eb i t ma pe d i t o rt h a ti st h e c u l p r i t .Fo rp a l e t t e di ma g e s , al o td e p e n d so nt h ee x a c tl a y o u to ft h ep a l e t t e , a n dt h e r e f o r ei ti svital t h a ty o uh a v eab i t ma pe d i t o rt h a t a l l o wst o t a l c o n t r o l o v e rt h ep a l e t t e , a n dl e a v e si ti n t a c twh e ns a v i n g .MSPa i n tf o re x a mp l ed o e sn e i t h e r .Ev e nv e r ye x p e n s i v ep h o t o e d i t i n gt o o l sd o n ' t , s ob ec a r e f u l . Fo rt h i si ma g e , Iu s e d<p l u g >myo wnb i t ma pe d i t o rUs e n t i </ p l u g >, wh i c hn o to n l yh a ss o men i c ep a l e t t ec o n t r o l o p t i o n s , a n dt i l i n g f u n c t i o n s , b u tab u i l t i nGBAg r a p h i c se x p o r t e ra swe l l .Toma k et h eb a c k g r o u n db et h es a mec o l o ri na l l mo d e s , t h et wob y t e so ft h e 1 6 b i tb a c k g r o u n dc o l o ro fmo d e s3a n d5h a dt os e r v ea sp a l e t t ee n t r i e sf o rmo d e4 , b o t hu s i n gt h a t1 6 b i tc o l o ra g a i n .I nt h i sc a s e , t h e c o l o ri s0x080F, s o r to fab r o wn i s hc o l o r .Th eb y t e sa r e8a n d1 5 , s ot h a t ' st h ep a l e t t ee n t r i e swh e r et h ec o l o rg o e st o o .No r ma l l yy o u d o n ' th a v et owo r r ya b o u ts wi t c h i n gb i t d e p t h smi d g a me , b u tk n o wi n gh o wt or e a dd a t al i k et h i si sau s e f u l d e b u g g i n gs k i l l . #include <string.h> #include "toolbox.h" #include "modes.h" int main() { int mode= 3; REG_DISPCNT= mode | DCNT_BG2; // Copy the data and palette to the right // addresses memcpy(vid_mem, modesBitmap, modesBitmapLen); memcpy(pal_bg_mem, modesPal, modesPalLen); while(1) { // Wait till VBlank before doing anything vid_vsync(); // Check keys for mode change key_poll(); if(key_hit(KEY_LEFT) && mode>3) mode--; else if(key_hit(KEY_RIGHT) && mode<5) mode++; // Change the mode REG_DISPCNT= mode | DCNT_BG2; } return 0; } file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 55/331 28-03-13 Tonc : GBA Programming in rot13 Fig 5.7a: bm_modesi nmo d e3 . Fig 5.7b: bm_modesi nmo d e4 . Fig 5.7c: bm_modesi nmo d e5 . Conclusions No wwe ' v es e e ns o meo ft h eb a s i c so ft h eGBAb i t ma pmo d e s : t h ep r o p e r t i e so fmo d e s3 , 4a n d5 , p a g ef l i p p i n g , r u d i me n t a r yd r a wi n g f o rmo d e3a n do n eo ft h emo s ti mp o r t a n tr u l e so fVRAM i n t e r a c t i o n s : y o uc a n n o twr i t et oVRAM i nb y t e s .Th e r ei smu c hmo r et h a t c a nb es a i d , o fc o u r s e .Bi t ma pg r a p h i c si sar i c hs u b j e c t , b u tg o i n gi n t omo r ed e t a i l r i g h tn o wma yn o tb et h eb e s ti d e a .Fo ro n e , t h e b i t ma pmo d e sa r ev e r yr a r e l yu s e di ng a me sa n y wa y , b u ta l s ob e c a u s et h e r ea r eo t h e rt h i n g st ot a l ka b o u ta swe l l .Th i n g sl i k eb u t t o n i n p u t , wh i c hi swh a tt h en e x tc h a p t e ri sa b o u t . Th i sc h a p t e ra l s od i s c u s s e daf e wt h i n g sa b o u th a n d l i n gd a t a , av e r yi mp o r t a n tt o p i cwh e ny o u ' r et h i sc l o s et ot h eh a r d wa r e . Da t a t y p e sma t t e r , e s p e c i a l l ywh e na c c e s s i n gme mo r yt h r o u g hp o i n t e r s , a n dy o un e e dt ob ea wa r eo ft h ed i f f e r e n c e sb e t we e nt h e m, a n d t h eo p p o r t u n i t i e sa n dd a n g e r so fe a c h .Ev e ni fy o ud o n ' tr e me mb e re v e r yl i t t l ed e t a i l i nt h ed a t as e c t i o n , a tl e a s tr e me mb e rwh e r et ol o o k wh e nt h i n g sg os c r e wy . Be f o r ec o n t i n u i n gwi t hf u r t h e rc h a p t e r s , t h i sma yb eag o o dt i met od os o mee x p e r i me n t i n gwi t hd a t a : t r yc h a n g i n gt h ed a t aa r r a y s a n ds e ewh a th a p p e n s .Lo o ka tt h ed i f f e r e n td a t ai n t e r p r e t a t i o n s , d i f f e r e n tc a s t s , a n dma y b es o mei n t e n t i o n a l e r r o r sa swe l l , j u s tt os e e wh a tk i n d so fp r o b l e msy o umi g h tf a c ea ts o mep o i n t .I t ' sb e t t e rt oma k emi s t a k e se a r l y , wh i l ep r o g r a msa r es t i l l s h o r ta n ds i mp l ea n d y o uh a v el e s sp o t e n t i a l p r o b l e ms . Orn o t , o fc o u r s e:P.Ma y b ei t ' swo r t hwa i t i n gal i t t l el o n g e rwi t ht h a t ;o ra tl e a s tu n t i l we ' v ec o v e r e db a s i ci n p u t , wh i c ha l l o wsf o rmu c h mo r ei n t e r e s t i n gt h i n g st h a nj u s tp a s s i v ei ma g e s . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 56/331 28-03-13 Tonc : GBA Programming in rot13 6. The GBA buttons (a.k.a. keys) I n t r o d u c t i o n Ke yr e g i s t e r s . Be y o n db a s i cb u t t o ns t a t e s . As i mp l ek e yd e mo . 6.1. Introduction Asy o un od o u b ta l r e a d yk n o w, t h eGBAh a so n e4 wa yd i r e c t i o n a l p a d( Dp a d ) ;t woc o n t r o l b u t t o n s( Se l e c ta n dSt a r t ) ;t wor e g u l a rf i r e b u t t o n s( Aa n dB)a n dt wos h o u l d e rb u t t o n s( La n dR) , ma k i n gat o t a l o f1 0keys.Th i si sa l l y o uh a v ei nt e r mso fu s e r GBAi n t e r a c t i o n , a n df o rmo s tp u r p o s e si ti sp l e n t y .Th ep r i n c i p l e so fk e y h a n d l i n ga r ep r e t t ys i mp l e : y o uh a v eo n er e g i s t e rwi t ht h ek e y s t a t e sa n dy o us e e wh i c hb u t t o n sa r ep r e s s e db a s e do nwh e t h e ri t sb i t sa r es e to rc l e a r e d .Iwi l l c o v e rt h i s , b u tI ' l l a l s og i v es o memo r ea d v a n c e df u n c t i o n s t h a ty o uwi l l p r o b a b l ywa n tt oh a v ea ts o mep o i n t . 6.2. Keypad registers 6.2.1. The keypad register, REG_KEYINPUT Ass a i d , t h eGBAh a st e nb u t t o n s , o f t e nr e f e r r e dt oa sk e y s .Th e i rs t a t e sc a nb ef o u n di nt h ef i r s t1 0b i t so ft h eREG_KEYINPUT r e g i s t e ra tl o c a t i o n0400:0130h( a . k . a .REG_P1) .Th ee x a c tl a y o u ti ss h o wnb e l o w.Iwi l l r e f r a i nf r o mg i v i n gab i t b y b i td e s c r i p t i o n b e c a u s ei ts h o u l db eq u i t eo b v i o u s .Th en a me so ft h ed e f i n e dc o n s t a n t sIu s ea r e" KEY_x" , wh e r ex i st h en a meo ft h eb u t t o n , i nc a p s . REG_KEYINPUT (REG_P1) @ 0400:0130h FEDCBA 9 8 - 7 6 5 4 3 2 1 0 L R down up left right start select B A Ch e c k i n gwh e t h e rak e yi sp r e s s e d( d o wn )o rn o two u l db eo b v i o u s , i fi twe r e n ' tf o ro n el i t t l ed e t a i l : t h eb i t sa r ecleare dwh e nak e yi s d o wn .Sot h ed e f a u l ts t a t eo fREG_KEYINPUTi s0x03FF, a n dn o t0.Ass u c h , c h e c k i n gi fkeyi sd o wng o e sl i k et h i s : #define KEY_DOWN_NOW(key) (~(REG_KEYINPUT) & key) I nc a s ey o u rb i t o p e r a t i o nk n o wl e d g ei sab i th a z y( g e ti tc l e a r e du p .Fa s t ! ) , t h i sf i r s ti n v e r t sREG_KEYINPUTt oamo r ei n t u i t i v e( a n d u s e f u l )‘ b i ti ss e twh e nd o wn ’s e t t i n ga n dt h e nma s k si twi t ht h ek e y ( s )y o uwa n tt oc h e c k .No t et h a tkeyc a ni nf a c tb eac o mb i n a t i o n o fmu l t i p l ek e y sa n dt h er e s u l twi l l b et h ec o mb i n a t i o no fk e y st h a ta r ea c t u a l l yd o wn . Key states are inverted Th ek e yb i t sa r el o wa c t i v e , me a n i n gt h a tt h e ya r ecleared wh e nab u t t o ni sp r e s s e da n dset wh e nt h e y ' r en o t .Th i sma yb e al i t t l ec o u n t e r i n t u i t i v e , b u tt h a t ' st h ewa yi ti s . 6.2.2. The key control register, REG_KEYCNT J u s ta b o u te v e r y t h i n gy o uwi l l e v e rn e e di nt e r mso fk e y h a n d l i n gc a nb ed o n ewi t hREG_KEYINPUT.Th a ts a i d , y o umi g h tl i k et ok n o w t h e r ei sa n o t h e rk e y r e g i s t e rf o rs o mee x t r ac o n t r o l .Th er e g i s t e ri nq u e s t i o ni sREG_KEYCNT, t h ek e yc o n t r o l r e g i s t e r .Th i sr e g i s t e ri s u s e df o rk e y p a di n t e r r u p t s , mu c hl i k eREG_DISPSTATwa su s e df o rv i d e oi n t e r r u p t s .Th el a y o u ti st h es a mea sf o rREG_KEYINPUT, e x c e p tf o rt h et o pt wob i t s , s e et h et a b l eb e l o w.Wi t hREG_KEYCNT{ 1 4 }y o uc a ne n a b l et h ek e y p a di n t e r r u p t .Th ec o n d i t i o n sf o r r a i s i n gt h i si n t e r r u p ta r ed e t e r mi n e db yREG_KEYCNT{ 0 9 } , wh i c hs a ywh a tk e y st owa t c ho u tf o ra n dREG_KEYCNT{ 1 5 } , wh i c h s t a t et h ee x a c tc o n d i t i o n s .I ft h i sb i ti sc l e a r , t h e na n yo ft h ea f o r e me n t i o n e dk e y swi l l r a i s et h ei n t e r r u p t ;i fs e t , t h e nt h e ymu s ta l l b e d o wnf o rt h ei n t e r r u p tt ob er a i s e d .Iwo u l d n ' tb es u r p r i s e di ft h i si sh o wy o uc a nr e s e tmo s tg a me sb yp r e s s i n gSt a r t +Se l e c t +B+A.Of c o u r s e , t oma k eu s eo ft h i sr e g i s t e ry o un e e dt ok n o wh o wt owo r kwi t hi n t e r r u p t sf i r s t . REG_KEYCNT (REG_P1CNT) @ 0400:0132h F E DCBA 9 8 Op I - 7 6 5 4 3 2 1 0 L R down up left right start select B A bits name define description 0 9 k e y s KEY_x k e y st oc h e c kf o rr a i s i n gak e yi n t e r r u p t . E I KCNT_I RQ En a b l e sk e y p a di n t e r r u p t F Op KCNT_OR, Bo o l e a no p e r a t o ru s e df o rd e t e r mi n i n gwh e t h e rt or a i s eak e y -i n t e r r u p to rn o t .I fc l e a r , i t KCNT_AND u s e sa nOR( r a i s ei fa n yo ft h ek e y so fb i t s0 9a r ed o wn ) ;i fs e t , i tu s e sa nAND( r a i s ei f file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 57/331 28-03-13 Tonc : GBA Programming in rot13 a l l o ft h o s ek e y sa r ed o wn ) . 6.3. Beyond basic button states Wh i l ec h e c k i n gf o rt h ek e y s t a t ewi t hKEY_DOWN_NOW()i sn i c ea n ds i mp l e , t h e r ea r eb e t t e ra n d / o rmo r ep r e f e r a b l eme t h o d so fk e y s t a t eh a n d l i n g .Iwi l l d i s c u s st wo( o rt h r e e )o ft h e mh e r e .Fi r s t , synchronous keystates.Th i si sj u s taf a n c ywa yo fr e a d i n gt h ek e y s t a t e a tag i v e np o i n ta n du s i n gt h a tv a r i a b l e , i n s t e a do fr e p e a t e dr e a d so fREG_KEYI NPUTwh e ny o up r o c e s si n p u t .Ano u t s h o o to ft h i si s transitional states, wh e r ey o ut r a c kn o to n l yt h ec u r r e n ts t a t e , b u ta l s ot h ep r e v i o u so n e .Th i sl e t sy o ut e s tf o rchanges i nk e y s t a t e s , r a t h e rt h a nj u s tt h ek e y s t a t e st h e ms e l v e s . .La s t l y , tribools: t h r e e s t a t ev a r i a b l e s( i nt h i sc a s e s−1 , 0a n d+1 )t h a tc a nb eu s e dt os i mp l i f y d i r e c t i o np r o c e s s i n g . 6.3.1. Synchronous and transitional key states Th eu s eo fKEY_DOWN_NOW()i saf o r mo fasynchronous k e yh a n d l i n g : y o uc h e c kt h es t a t ea tt h et i met h ec o d en e e d si t .Wh i l ei t wo r k s , i t ' sn o ta l wa y st h eb e s ta p p r o a c h .Fi r s t l y , i ti sl e s se f f i c i e n ti nt e r mso fc o d eb e c a u s et h er e g i s t e ri sl o a d e da n dr e a de v e r yt i mei t i sn e c e s s a r y( i t ' sv o l a t i l e , r e me mb e r ? ) .As e c o n d a r yc o n c e r ni st h a tas i mu l t a n e o u smu l t i b u t t o nt a pma yn o tb er e g i s t e r e da ss u c h b e c a u s et h ec o d er e a d i n gt h eb u t t o ns t a t e sa r eal i t t l ea p a r t . Bu tt h o s ea r ej u s tmi n o rc o n c e r n s ;t h ema i ni s s u ei st h a tt h e r e ' sj u s tl i t t l ey o uc a nr e a l l yd owi t ht h e m.Yo uc a ng e tt h ec u r r e n ts t a t e , b u tt h a t ' si t .Asas i mp l ee x a mp l eo fwh yt h i si si n s u f f i c i e n tf o rg a me s , c o n s i d e r( u n ) p a u s i n gag a me .Th i si su s u a l l yd o n eb yp r e s s i n g St a r t , a n dt h e nSt a r ta g a i nf o ru n p a u s i n g .Th a t ' sf i n eu n t i l y o uc o n s i d e rt h a tt h eg a mer u n sf a s t e rt h a ny o uc a nr e a c t( t h i si sab a s i cf a c t o fl i f e ;t h eo n l yr e a s o ny o uc a nwi ng a me si sb e c a u s et h eg a mel e t sy o u .De a l ) , s ot h eStartb u t t o nwi l l b ed o wnf o rmu l t i p l ef r a me s . Wi t hKEY_DOWN_NOW(), t h eg a mewi l l p a u s eand u n p a u s ed u r i n gt h i st i me ;t h es t a t eo ft h eg a mewh e ny o uf i n a l l yr e l e a s et h eb u t t o n i se s s e n t i a l l yr a n d o m.Ne e d l e s st os a y , t h i si saBa dTh i n g ™. En t e rs y n c h r o n o u ss t a t e s .Si mp l yr e a dt h es t a t eo n c e , a tt h eb e g i n n i n go ft h ef r a mef o re x a mp l e , a n du s et h a ta s‘ t h e ’s t a t ef o rt h e wh o l ef r a me .Th a tt a k e sc a r eo ft h ee x c e s sr e a d i n g so fREG_KEYI NPUT, a n dp o t e n t i a l l ymi s s e ds i mu l t a n e i t y .Fo rt r a c k i n gs t a t e c h a n g e s , wea l s os a v et h es t a t eo ft h ep r e v i o u sf r a me .Soa tt h ev e r yl e a s t , wen e e dt wov a r i a b l e sa n daf u n c t i o nt h a tu p d a t e st h e m, a n d f o rg o o dme a s u r e , s o mef u n c t i o n st h a tc h e c kt h es t a t e s .Be c a u s et h e s ewi l l b eq u i t es ma l l , i tma k e ss e n s et oi n l i n et h e ma swe l l . // === (tonc_core.c) ================================================== // Globals to hold the key state u16 __key_curr=0, __key_prev=0; // === (tonc_input.h) ================================================= extern u16 __key_curr, __key_prev; #define KEY_A #define KEY_B #define KEY_SELECT #define KEY_START #define KEY_RIGHT #define KEY_LEFT #define KEY_UP #define KEY_DOWN #define KEY_R #define KEY_L 0x0001 0x0002 0x0004 0x0008 0x0010 0x0020 0x0040 0x0080 0x0100 0x0200 #define KEY_MASK 0x03FF // Polling function INLINE void key_poll() { __key_prev= __key_curr; __key_curr= ~REG_KEYINPUT & KEY_MASK; } // Basic state checks INLINE u32 key_curr_state() INLINE u32 key_prev_state() INLINE u32 key_is_down(u32 key) INLINE u32 key_is_up(u32 key) INLINE u32 key_was_down(u32 key) INLINE u32 key_was_up(u32 key) { { { { { { return __key_curr; return __key_prev; return __key_curr & key; return ~__key_curr & key; return __key_prev & key; return ~__key_prev & key; } } } } } } Th ek e ys t a t e sa r es t o r e di n__key_curra n d__key_prev.Th ef u n c t i o nt h a tu p d a t e st h e mi skey_poll().No t et h a tt h i s f u n c t i o na l r e a d yi n v e r t sREG_KEYI NPUT, s ot h a tt h ev a r i a b l e sa r ea c t i v eh i g h , wh i c hma k e sl a t e ro p e r a t i o n smo r ei n t u i t i v e .Fo r e x a mp l e , t ot e s twh e t h e rAi sc u r r e n t l yd o wn( p r e s s e d ) , j u s tma s k__key_currwi t hKEY_A, t h eb i tf o rA.Th i si swh a t key_is_down()d o e s .Wh i l eKEY_DOWN_NOW()g i v e s( a l mo s t )t h es a mea n s we r , Iwo u l ds t i l l r e c o mme n du s i n g key_is_down()i n s t e a d . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 58/331 28-03-13 Tonc : GBA Programming in rot13 Invert REG_KEYINPUT reads as soon as possible Th et h i n g st h a ty o umi g h tc h e c kt h ek e y s t a t e sf o ra r es i mp l ye a s i e ri na c t i v e h i g hs e t t i n g s .Th e r e f o r e , i ti sag o o di d e at o ma k et h ek e y s t a t ev a r i a b l e swo r kt h a twa y . 6.3.2. Transitional states Ba c kt ot h ep a u s e / u n p a u s ei s s u e .Th en a s t yb e h a v i o u rKEY_DOWN_NOW()c a u s e si sk n o wna skey bounce.Th i si sb e c a u s et h ema c r o o n l yc h e c k st h ec u r r e n ts t a t e .Wh a ty o un e e df o rp r o p e r( u n ) p a u s i n gi ss o me t h i n gt h a tc h e c k swh e t h e rak e yi sgoing d o wn , r a t h e rt h a n j u s td o wn : y o un e e dt oc h e c kt h et r a n s i t i o n .Th a t ' swh e r et h ep r e v i o u ss t a t ec o me si n .Wh e nak e yi sh i t , i . e . , t h emo me n to fi tg o i n g d o wn , i twi l l b ep r e s s e di nt h ec u r r e n ts t a t e , b u tn o tt h eo n eb e f o r e .I no t h e rwo r d s , t h ek e y st h a ta r e‘ h i t ’a r ed o wnc u r r e n t l y , a n dn o t b e f o r e : __key_curr&~__key_prev.Af t e rt h a t , c h e c k i n gf o rap a r t i c u l a rk e yc a nb ea c h i e v e dwi t has i mp l ema s ka su s u a l .Th i si s d o n eb ykey_hit(). Th a t ' sr e a l l ya l l t h e r ei st oi t , a n dy o uc a nc r e a t es i mi l a rf u n c t i o n st oc h e c kf o rr e l e a s e s( b e f o r eANDNOTn o w) , i fi ti sh e l d( b e f o r e ANDn o w) , e tc e t e r a .Ag a i n , i ta l l s e e mss os i mp l eb e c a u s et h es t a t e swe r ea l r e a d yi n v e r t e d ;wh e nIf i r s tma d et h e s ef u n c t i o n s , Ih a da t e r r i b l et i mef i g u r i n go u twh a tt h er i g h tb i t o p swe r eb e c a u s et h ea c t i v e l o wl o g i cwa st h r o wi n gmeo f f .We l l o k a y , n o treally b u ti t wo u l dh a v eb e e ne a s i e ri fIh a dt h e mi n v e r t e df r o mt h es t a r t . // Transitional state checks. // Key is changing state. INLINE u32 key_transit(u32 key) { return ( __key_curr ^ __key_prev) & key; } // Key is held (down now and before). INLINE u32 key_held(u32 key) { return ( __key_curr & __key_prev) & key; } // Key is being hit (down now, but not before). INLINE u32 key_hit(u32 key) { return ( __key_curr &~ __key_prev) & key; } Key is being released (up now but down before) INLINE u32 key_released(u32 key) { return (~__key_curr & __key_prev) & key; } 6.3.3. Key tribool states Th i si sal i t t l et e c h n i q u et a k e nf r o mt h ePA_Li bwi k i .I ti s n ' ts omu c ha b o u tk e y sp e rs e , b u tas h o r t h a n di nh o wy o uc a nu s et h e f u n c t i o n s , a n dy o uwi l l h a v et oma k eu pf o ry o u r s e l fwh e t h e rwh a t ' sd i s c u s s e di nt h i ss u b s e c t i o ni sr i g h tf o ry o u . I ma g i n ey o uh a v eag a me / d e mo / wh a t e v e ri nwh i c hy o uc a nmo v es t u f fa r o u n d .Toma k eac h a r a c t e rmo v el e f ta n dr i g h t , f o r e x a mp l e , y o umi g h td ou s es o me t h i n gl i k et h i s . // variable x, speed dx if(key_is_down(KEY_RIGHT)) x += dx; else if(key_is_down(KEY_LEFT)) x -= dx; Th i n gmo v e sr i g h t , xi n c r e a s e s ;t h i n gmo v e sl e f t , xd e c r e a s e s , s i mp l ee n o u g h .Wo r k sf i n et o o .Ho we v e r , a n dt h i sma yj u s tb emyi f p h o b i a a c t i n gu p , i t ' sn o tv e r yp r e t t yc o d e .Sol e t ' ss e ei fwec a nf i n ds o me t h i n gs mo o t h e r . Ta k eal o o ka twh a tt h ec o d ei sa c t u a l l yd o i n g .De p e n d i n go nt woc h o i c e s , t h ev a r i a b l ei se i t h e ri n c r e a s e d( +) , d e c r e a s e d( −) , o r u n c h a n g e d( 0 ) .Th a t ' sap r e t t yg o o dd e f i n i t i o no fatribool, av a r i a b l ewi t ht h r e ep o s s i b l es t a t e s , i nt h i sc a s e+1 , 0a n d−1 .Wh a tI ' ma f t e r i ss o me t h i n gt h a tl e t sy o uu s et h e s es t a t e st od ot h ef o l l o wi n g . x += DX*key_tri_horz(); Is u p p o s eIc o u l dj u s twr a pt h eifsi nt h i sf u n c t i o n , b u tIp r e f e rt od oi tv i ab i to p e r a t i o n s .Al l In e e dt od of o rt h i si ss h i f tt h eb i t sf o r s p e c i f i ck e y sd o wn , ma s kt h a twi t ho n e , a n ds u b t r a c tt h er e s u l t s . // === (tonc_core.h) ================================================== // tribool: 1 if {plus} on, -1 if {minus} on, 0 if {plus}=={minus} INLINE int bit_tribool(u32 x, int plus, int minus) { return ((x>>plus)&1) - ((x>>minus)&1); } // === (tonc_input.h) ================================================= enum eKeyIndex { file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 59/331 28-03-13 Tonc : GBA Programming in rot13 KI_A=0, KI_B, KI_SELECT, KI_START, KI_RIGHT, KI_LEFT, KI_UP, KI_DOWN, KI_R, KI_L, KI_MAX }; // --- TRISTATES --INLINE int key_tri_horz() // right/left : +/{ return bit_tribool(__key_curr, KI_RIGHT, KI_LEFT); } INLINE int key_tri_vert() // down/up : +/{ return bit_tribool(__key_curr, KI_DOWN, KI_UP); } INLINE int key_tri_shoulder() // R/L : +/{ return bit_tribool(__key_curr, KI_R, KI_L); } INLINE int key_tri_fire() // B/A : -/+ { return bit_tribool(__key_curr, KI_A, KI_B); } Th ei n l i n ef u n c t i o nbit_tribool()c r e a t e sat r i b o o l v a l u ef r o ma n yt wob i t si nan u mb e r( r e g i s t e ro ro t h e r wi s e ) .Th er e s to ft h e f u n c t i o n sl i s t e dh e r eu s et h ec u r r e n tk e y s t a t ea n dt h ek e y b i t st oc r e a t et r i b o o l sf o rh o r i z o n t a l , v e r t i c a l , s h o u l d e ra n df i r eb u t t o n s ;o t h e r s c a nb ec r e a t e swi t hr e l a t i v ee a s e .Th e s ef u n c t i o n sma k et h ec o d el o o kc l e a n e ra n da r ef a s t e rt ob o o t .Yo uwi l l b es e e i n gt h e mq u i t e o f t e n . Wh i l et h ef u n c t i o n sme n t i o n e da b o v eo n l yu s e__key_curr, i ti se a s yt owr i t ec o d et h a tu s e so t h e rk e y s t a t et y p e s .Fo re x a mp l e , ar i g h t l e f tkey_hitv a r i a n tmi g h tl o o ks o me t h i n gl i k et h i s : // increase/decrease x on a right/left hit x += DX*bit_tribool(key_hit(-1), KI_RIGHT, KI_LEFT); I t ' sj u s tac a l l t obit_tribool()wi t hu s i n gkey_hit()i n s t e a do f__key_curr.I nc a s ey o u ' r ewo n d e r i n gwh a tt h e“ −1 ”i s d o i n gt h e r e , Ij u s tn e e di tt og e tt h ef u l l h i ts t a t e .Re me mb e rt h a t−1i s0xFFFFFFFFi nh e x , i no t h e rwo r d saf u l l ma s k , wh i c hwi l l b e o p t i mi z e do u to ft h ef i n a l c o d e .Yo uwi l l s e et h i su s eo ft r i b o o l sac o u p l eo ft i me sa swe l l . 6.4. A simple key demo Ih a v et h ekey_demod e moi l l u s t r a t e sh o wt h e s ek e yf u n c t i o n sc a nb eu s e d .I ts h o wsamo d e4p i c t u r eo fa GBA( a2 4 0 x 1 6 08 b i tb i t ma p ) ;t h ec o l o r sc h a n g ea c c o r d i n gt ot h eb u t t o np r e s s e s .Th en o r ma l s t a t ei sg r e y ; wh e ny o up r e s st h ek e y , i tt u r n sr e d ;wh e ny o ur e l e a s ei t , i tg o e sy e l l o w;a n da sl o n ga si t ' sh e l di t ' sg r e e n .Fi g 6 . 1s h o wst h i sf o rt h eLa n dBb u t t o n s .He r e ' st h ec o d et h a td o e st h er e a l wo r k : #include <string.h> #include "toolbox.h" #include "input.h" Fig 6.1: k e y_d e mo s c r e e n s h o t , wi t hLa n dB h e l d . #include "gba_pic.h" #define BTN_PAL_ID 5 #define CLR_UP RGB15(27,27,29) int main() { int ii; u32 btn; COLOR clr; int frame=0; memcpy(vid_mem, gba_picBitmap, gba_picBitmapLen); memcpy(pal_bg_mem, gba_picPal, gba_picPalLen); REG_DISPCNT= DCNT_MODE4 | DCNT_BG2; while(1) { vid_vsync(); // slowing down polling to make the changes visible if((frame & 7) == 0) key_poll(); // check state of each button for(ii=0; ii<KI_MAX; ii++) { clr=0; file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 60/331 28-03-13 Tonc : GBA Programming in rot13 btn= 1<<ii; if(key_hit(btn)) clr= CLR_RED; else if(key_released(btn)) clr= CLR_YELLOW; else if(key_held(btn)) clr= CLR_LIME; else clr= CLR_UP; pal_bg_mem[BTN_PAL_ID+ii]= clr; } frame++; } return 0; } BTN_PAL_IDi st h es t a r t i n gi n d e xo ft h ep a l e t t e p a r tu s e df o rt h eb u t t o n sa n dCLR_UPi sas h a d eo fg r e y ;t h er e s to ft h ec o l o r ss h o u l d b eo b v i o u s .Toma k es u r et h a ty o uc a na c t u a l l ys e et h ec h a n g e si nb u t t o nc o l o r sI ' mo n l yp o l l i n gt h ek e y so n c ee v e r y8f r a me s .I fId i d n ' t d ot h a t , y o u ' l l h a r d l ye v e rs e ear e do ry e l l o wb u t t o n .( Byt h ewa y , Id o n ' ta c t u a l l yc h a n g et h eb u t t o n s ' c o l o r s , b u to n l yt h ep a l e t t ec o l o r t h a tt h a tb u t t o n ' sp i x e l su s e ;p a l e t t ea n i ma t i o ni saGo o dTh i n g ™) . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 61/331 28-03-13 Tonc : GBA Programming in rot13 7. Sprite and background overview Sp r i t e sa n db a c k g r o u n d si n t r o d u c t i o n . Sp r i t ea n db a c k g r o u n dc o n t r o l . Sp r i t ea n db a c k g r o u n dma p p i n g . Sp r i t ea n db a c k g r o u n di ma g ed a t a . Su mma r y . Wh a t ' si nan a me ? 7.1. Sprites and backgrounds introduction Al t h o u g hy o uc a nma k eg a me sb a s e dp u r e l yo nt h eb i t ma pmo d e s , y o u ' l l f i n dv e r yf e wt h a td o .Th es i mp l er e a s o nf o rt h i si st h a ta l l g r a p h i c swo u l db er e n d e r e db ys o f t wa r e .Noma t t e rh o wg o o dy o u rc o d ei s , t h a t ' sa l wa y sg o i n gt ob eas l o wp r o c e s s .No w, I ' mn o t s a y i n gi tc a n ' tb ed o n e : t h e r ea r es e v e r a l FPSso nt h eGBA( Wo l f e n s t e i na n dDo o mf o re x a mp l e ) .Iam s a y i n gt h a tu n l e s sy o u ' r ewi l l i n g t oo p t i mi z et h ec r a po u to fy o u rc o d e , y o u ' l l h a v eah a r dt i med o i n gi t . Th ev a s tma j o r i t yu s e st h eGBA' shardware graphics, wh i c hc o mei nt h ef o r mso fsprites a n dtiled backgrounds ( s i mp l y “ b a c k g r o u n d ”o r“ b g ”f o rs h o r t ) .AsIs a i di nt h ev i d e oi n t r o d u c t i o n , at i l e db a c k g r o u n di sc o mp o s e do fama t r i xo ft i l e s( h e n c et h en a me ) a n de a c ht i l ec o n t a i n sa ni n d e xt oa n8 x 8p i x e l b i t ma pk n o wna satile.Sowh a te n d su po ns c r e e ni sama t r i xo ft i l e s .Th e r ea r ef o u ro f t h e s eb a c k g r o u n d swi t hs i z e sb e t we e n1 2 8 x 1 2 8p i x e l s( 3 2 x 3 2t i l e s )t o1 0 2 4 x 1 0 2 4p i x e l s( 1 2 8 x 1 2 8t i l e s ) .Sp r i t e sa r es ma l l e ro b j e c t s b e t we e n8 x 8t o6 4 x 6 4p i x e l si ns i z e .Th e r ea r e1 2 8o ft h e ma n dy o uc a nmo v et h e mi n d e p e n d e n t l yo fe a c ho t h e r .Li k eb a c k g r o u n d s , s p r i t e sa r eb u i l to u to ft i l e s . Th eh a r d wa r et a k e sc a r eo fs e v e r a l o t h e ra s p e c t so fr e n d e r i n gb e s i d e sme r er a s t e rb l a s t i n g .Fo ro n et h i n g , i tu s e scolor keying t o e x c l u d es o mep i x e l sf r o ms h o wi n gu p( i . e . , t h e s ea r et r a n s p a r e n t ) .Ba s i c a l l y , i ft h et i l e ' sp i x e l h a sav a l u eo fz e r oi ti st r a n s p a r e n t . Fu r t h e r mo r e , t h eh a r d wa r et a k e sc a r eo fan u mb e ro fo t h e re f f e c t sl i k ef l i p p i n g , a l p h a b l e n d i n ga n da f f i n et r a n s f o r ma t i o n sl i k er o t a t i o n a n ds c a l i n g . Th et r i c ki ss e t t i n gu pt h ep r o c e s s .Th e r ea r et h r e eb a s i cs t e p st ob ea wa r eo f : control, mapping a n dimage data.Th eb o u n d a r i e s o ft h e s es t e p sa r eab i tv a g u e , b u ti th e l p st os e ei ti nt h i sma n n e rs i n c ei ta l l o wsy o ut os e es p r i t e sa n db a c k g r o u n d sa st wos i d e so ft h e s a mec o i n .An du n i f i c a t i o ni saGo o dTh i n g ®.Th e r ea r es t i l l d i f f e r e n c e s , o fc o u r s e , b u to n l yi nt h ed e t a i l s . Th i sp a g eg i v e sab r o a do v e r v i e wo fwh a tIwi l l t a l ka b o u ti nt h en e x tc o u p l eo fp a g e s .Do n ' two r r yi fy o ud o n ' tu n d e r s t a n dr i g h ta wa y , t h a t ' sn o tr e a l l yt h ep o i n tr i g h tn o w.J u s tl e ti ts e e pi n t oy o u rb r a i n , r e a dt h eo t h e rp a g e sa n dt h e ny o u ' l l s e ewh a tI ' mo na b o u th e r e . 7.2. Sprite and background control Th ef i r s ts t e po fr e n d e r i n gi sc o n t r o l .Co n t r o l c o v e r st h i n g st h a ta c to nt h es p r i t e so rb a c k g r o u n d sa sawh o l e , l i k ea c t i v a t i o no ft h et h i n g s t h e ms e l v e s , wh e t h e rt ou s e1 6o r2 5 6c o l o rt i l e s , a n de f f e c t sl i k ea l p h a b l e n d i n ga n dt r a n s f o r ma t i o n s .Fi r s tu pi swh e t h e ro rn o ty o uwa n t t ou s et h et h i n g si nt h ef i r s tp l a c e .Th i si sd o n eb ys e t t i n gt h er i g h tb i t si nt h ed i s p l a yc o n t r o l r e g i s t e rREG_DISPCNT.On c ey o u ' v ed o n e t h a tt h e r ea r ef u r t h e rc o n t r o l r e g i s t e r sf o rb a c k g r o u n d s : t h eREG_BGxCNTr e g i s t e r s( 0400:0008h0400:000Fh) .Fo rs p r i t e s t h e r e ' st h eObject Attribute Memory, o rOAM, wh i c hc a nb ef o u n da t0700:0000h.Ea c ho ft h e1 2 8s p r i t e sh a st h r e es o c a l l e d attributes ( h e n c eOAM)wh i c hc o v e r sb o t ht h ec o n t r o l a n dma p p i n ga s p e c t so ft h es p r i t e s . 7.3. Sprite and background mapping Th e r e ' sal o to fg r e ya r e ab e t we e nc o n t r o l a n dma p p i n g , b u th e r eg o e s .Ma p p i n gc o n c e r n se v e r y t h i n ga b o u twh i c ht i l e st ou s ea n dwh e r e t h e yg o .Ass a i d , t h es c r e e na p p e a r a n c eo fb o t hs p r i t e sa n db a c k g r o u n d sa r ec o n s t r u c t e do ft i l e s , l a i do u ts i d eb ys i d e .Yo uh a v et ot e l l t h eGBAwh i c ht i l e st ob l i tt owh a tp o s i t i o n .Fi g7 . 1 a c( b e l o w)i l l u s t r a t e st h i s .I nf i g7 . 1 by o us e et h et i l e s .No t et h a tb o t hs p r i t e sa n d b a c k g r o u n d sh a v et h e i ro wns e to ft i l e s . I nf i g1 cy o us e eh o wt h e s et i l e sa r eu s e d .Th eb a c k g r o u n du s e satile-map, wh i c hwo r k sj u s tl i k ea no r d i n a r yp a l e t t e db i t ma p e x c e p tt h a ti t ' sama t r i xo fscreenblock entries ( wi t ht i l e i n d i c e s )i n s t e a do fp i x e l s( c o n t a i n i n gc o l o r i n d i c e s ) .Ex c u s eme , awh a t ? ! ? Sc r e e n b l o c ke n t r y .Ye s , Ik n o wt h en a mei sab i ts i l l y .Th et h i n gi st h a ty o un e e dk e e pac l e a rd i s t i n c t i o nb e t we e nt h ee n t r i e si nt h ema p ( t h es c r e e n b l o c ke n t r i e s , SE f o rs h o r t )a n dt h ei ma g e d a t a( t h ea c t u a l t i l e s ) .Un f o r t u n a t e l y , t h et e r m“ t i l e ”i so f t e nu s e df o rb o t h .I ' l l s t i c k t ot i l e sf o rt h ea c t u a l g r a p h i c a l i n f o r ma t i o n , a n ds i n c et h et i l e ma pi ss t o r e di nt h i n g sc a l l e ds c r e e n b l o c k s , s c r e e n b l o c ke n t r i e so rSEf o r t h ema pd a t a .An y wa y , e a c hSEh a si t so wnt i l e i n d e x .I ta l s oc o n t a i n sb i t sf o rh o r i z o n t a l a n dv e r t i c a l f l i p p i n ga n d , i fi t ' sa1 6 c o l o r b a c k g r o u n d , a ni n d e xf o rt h ep a l b a n ka swe l l .I nf i g7 . 1 c , y o uo n l ys e et h et i l e i n d e x , t h o u g h . Fo rs p r i t e s , i t ' ssab i td i f f e r e n t , b u tt h eb a s i cs t e p sr e ma i n .Yo ug i v eone t i l e i n d e xf o rt h ewh o l es p r i t e ;t h eGBAt h e nf i g u r e so u tt h e o t h e rt i l e st ou s eb yl o o k i n ga tt h es h a p ea n ds i z eo ft h es p r i t ea n dt h esprite mapping-mode.I ' l l e x p l a i nwh a tt h i sme a n sl a t e r ;s u f f i c e t os a yt h a tt h ema p p i n gmo d ei se i t h e r1 Do r2 D, d e p e n d i n go nREG_DISPCNT{6}.I nt h i sc a s e , I ' v eu s e d1 Dma p p i n g , wh i c hs t a t e s t h a tt h et i l e st h a tas p r i t es h o u l du s ea r ec o n s e c u t i v e .Li k eb a c k g r o u n d s , t h e r e ' sa d d i t i o n a l f l i p p i n gf l a g sa n dp a l e t t e i n f of o r1 6 c o l o r s p r i t e s .Un l i k eb a c k g r o u n d s , t h e s ewo r ko nt h ewhole s p r i t e , n o tj u s to no n et i l e .Al s o , t h ec o mp o n e n tt i l e so fs p r i t e sa r ea l wa y s a d j o i n i n g , s oy o uc a ns e eas p r i t ea sami n i a t u r et i l e d b a c k g r o u n dwi t hs o mei ma g i n a t i o n . Wh a tb e l o n g st ot h ema p p i n gs t e pa swe l l i st h ea f f i n et r a n s f o r ma t i o nma t r i x , i fa n y .Wi t ht h i s2 x 2ma t r i xy o uc a nr o t a t e , s c a l eo r file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 62/331 28-03-13 Tonc : GBA Programming in rot13 s h e a rs p r i t e so rb a c k g r o u n d s .Th e r es e e mst ob eal o to fc o n f u s i o na b o u th o wt h i swo r k ss oI ' v ewr i t t e nad e t a i l e d , ma t h e ma t i c a l d e s c r i p t i o no nh o wt h i st h i n gwo r k s .Bo t t o ml i n e : t h ema t r i xma p sf r o ms c r e e ns p a c et ot e x t u r e s p a c e , a n dnot t h eo t h e rwa yr o u n d . Th o u g ha l l t h er e f e r e n c ed o c u me n t sd os t a t et h i si nar o u n d a b o u twa y , a l mo s te v e r yr o t a t i o n s c a l ema t r i xI ' v es e e ns of a ri si n c o r r e c t .I f y o u rc o d ei sb a s e do nPERN' s , c h a n c e sa r ey o u r si st o o . Fig 7.1a: 2s pr i t e so nab a c k g r o u n d . Fig 7.1c: t i l eu s a g eb yb g sa n ds pr i t e s . On et i l epe rS Ef o rb g s , a n dt h et o pl e f t t i l ef o rs pr i t e s . De f a u l tt i l e s( wi t hi n d e x0a r eo mi t t e df o rc l a r i t y' ss a k e . Fig 7.1b: b a c k g r o u n d( a b o v e )a n ds pr i t e ( b e l o w)t i l e s . 7.4. Sprite and background image data I ma g ed a t ai swh a tt h eGBAa c t u a l l yu s e st op r o d u c ea ni ma g e .Th i sme a n st wot h i n g s : t i l e sa n dp a l e t t e s . 7.4.1. Tiles Sp r i t e sa n db a c k g r o u n d sa r ec o mp o s e do fama t r i xo fs ma l l e rb i t ma p sc a l l e dtiles .Yo u rb a s i ct i l ei sa n8 x 8b i t ma p .Ti l e sc o mei n4 b p p ( 1 6c o l o r s/1 6p a l e t t e s )a n d8 b p p( 2 5 6c o l o r s/1p a l e t t e )v a r i a n t s .I na n a l o g yt of l o a t i n gp o i n tn u mb e r s , Ir e f e rt ot h e s ea ss-tiles ( s i n g l e s i z et i l e )a n dd-tiles ( d o u b l e s i z et i l e s ) .Ans t i l ei s3 2( 2 0 h )b y t e sl o n g , ad t i l e6 4( 4 0 h )b y t e s .Th ed e f a u l tt y p eo ft i l ei st h e4 b p pv a r i a n t ( t h es t i l e ) .I fIt a l ka b o u tt i l e swi t h o u tme n t i o n i n gwh i c ht y p e , i te i t h e rd o e s n ' tma t t e ro ri t ' sa ns t i l e .J u s tp a ya t t e n t i o nt ot h ec o n t e x t . Th e r ei ss o me t i me sami s u n d e r s t a n d i n ga b o u twh a two r k i n gi nt i l e sr e a l l yme a n s .I nt i l e dmo d e s , VRAM i snot ab i gb i t ma po u to f wh i c ht i l e sa r es e l e c t e d , b u tac o l l e c t i o no f8 x 8p i x e l b i t ma p s( i . e . , t h et i l e s ) .I ti si mp o r t a n tt h a ty o uu n d e r s t a n dt h ed i f f e r e n c e sb e t we e n t h e s et wome t h o d s !Co n s i d e ra n8 x 8r e c t a n g l ei nab i gb i t ma p , a n da n8 x 8t i l e .I nt h eb i gb i g ma p , t h ed a t aa f t e rt h ef i r s t8p i x e l sc o n t a i n t h en e x t8p i x e l so ft h es a mes c a n l i n e ;t h en e x tl i n eo ft h e‘ t i l e ’c a nb ef o u n df u r t h e ro n .I nt i l e dmo d e s , t h en e x ts c a n l i n eo ft h et i l e i mme d i a t e l yf o l l o wst h ec u r r e n tl i n e . Ba s i c a l l y , VRAM wo r k sa sa n8 ×N· 8b i t ma pi nt h et i l e dmo d e s .Be c a u s es u c has ma l l wi d t hi si mp r a c t i c a l t owo r kwi t h , t h e y ' r e u s u a l l yp r e s e n t e da sawi d e rb i t ma pa n y wa y .Ane x a mp l ei st h eVBAt i l ev i e we r , wh i c hd i s p l a y sc h a rb l o c k sa sa2 5 6 x 2 5 6b i t ma p ;Id o s o me t h i n gs i mi l a ri nf i g7 . 2 a .I ti si mp o r t a n tt or e me mb e rt h a tt h e s ed on o ta c c u r a t e l ymi mi ct h ec o n t e n t so fVRAM;t or e p r o d u c et h e a c t u a l c o n t e n to fVRAM y o u ' dn e e ds o me t h i n gl i k ef i g7 . 2 b , b u t , o fc o u r s e , n o o n ei si n s a n ee n o u g ht oe d i tb i t ma p si nt h a tma n n e r .I na l l l i k e l i h o o d , y o un e e dat o o l t h a tc a nb r e a ku pab i t ma pi n t o8 x 8c h u n k s .Orr e s t r u c t u r ei tt oab i t ma pwi t hawi d t ho f8p i x e l s , wh i c hi n e s s e n c ei st h es a met h i n g . Aswi t ha l l b i t ma p s , i ti st h ep r o g r a mme r ' sr e s p o n s i b i l i t y( t h a tme a n sy o u ! )t h a tt h eb i t d e p t ho ft h et i l e st h a ts p r i t e sa n db a c k g r o u n d s c o r r e s p o n dt ot h eb i t d e p t ho ft h ed a t ai nVRAM.I ft h i si so u to fs y n c , s o me t h i n gl i k ef i g7 . 2 ama ya p p e a ra sf i g7 . 2 c .So me t h i n gl i k e t h i si sl i k e l yt oh a p p e ns o o n e ro rl a t e r , b e c a u s ea l l g r a p h i c sn e e dt ob ec o n v e r t e do u t s i d eo ft h es y s t e mb e f o r eu s e ;o n emi s p l a c e d c o n v e r s i o no p t i o ni sa l l i tt a k e s . Fig 7.2a: 8 b ppt i l e s . Fig 7.2b: 8 b ppt i l e sa sb i t ma p. Fig 7.2c: t h ed a t ao ff i g7 . 2 a , i n t e r pr e t e da s4 b ppd a t a . I fyo us e es o me t h i n g l i k et h i s( a n dyo uwi l l ) , yo un o wk n o wwh y. file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 63/331 28-03-13 Tonc : GBA Programming in rot13 Tiled graphics considerations Re me mb e ra n du n d e r s t a n dt h ef o l l o wi n gp o i n t s : 1 .Th ed a t ao fe a c ht i l ea r es t o r e ds e q u e n t i a l l y , wi t ht h en e x tr o wo f8p i x e l si mme d i a t e l yf o l l o wi n gt h ep r e v i o u sr o w. VRAM i sb a s i c a l l yab i gb i t ma p8p i x e l swi d e .Gr a p h i c sc o n v e r t e r ss h o u l db ea b l et oc o n v e r tb i g g e rb i t ma p si n t o t h i sf o r ma t . 2 .Asa l wa y s , wa t c hy o u rb i t d e p t h . Tip for graphics converters I fy o uwa n tt oma k ey o u ro wnc o n v e r s i o nt o o l s , h e r e ' sal i t t l et i pt h a t ' l l h e l py o uwi t ht i l e s .Wo r ki ns t a g e s ;d onot g od i r e c t l y f r o man o r ma l , l i n e a rb i t ma pt owr i t i n gt h ed a t a f i l e .Cr e a t eat i l i n gf u n c t i o nt h a tt a k e sab i t ma pa n da r r a n g e st h et i l e si n t oa b i t ma p1t i l ewi d ea n dH t i l e sh i g h .Th i sc a nt h e nb ee x p o r t e dn o r ma l l y .I fy o ua l l o wf o rav a r i a b l et i l e wi d t h( n o th a r d c o d i n g t h e8 p i x e l wi d t h ) , y o uc a nu s ei tf o ro t h e rp u r p o s e sa swe l l .Fo re x a mp l e , t oc r e a t e1 6 x 1 6s p r i t e s , f i r s ta r r a n g ewi t hwi d t h =1 6 , t h e nwi t hwi d t h =8 . 7.4.2. Tile blocks (aka charblocks) Al l t h et i l e sa r es t o r e di ncharblocks.Asmu c ha sI ' dl i k et h e mt ob ec a l l e dt i l e b l o c k sb e c a u s et h a t ' swh a tt h e y ' r eb l o c k sof, t r a d i t i o n h a si tt h a tt i l e sa r ec h a r a c t e r s( n o tt ob ec o n f u s e dwi t ht h ep r o g r a mmi n gt y p eo fc h a r a c t e r s : a n8 b i ti n t e g e r )a n ds ot h ec r i t t e r sa r ec a l l e d c h a r b l o c k .Ea c hc h a r b l o c ki s1 6 k b( 4 0 0 0 hb y t e s )l o n g , s ot h e r e ' sr o o mf o r5 1 2( 4 0 0 0 h / 2 0 h )s t i l e so r2 5 6( 4 0 0 0 h / 4 0 h )d t i l e s .Yo uc a n a l s oc o n s i d e rc h a r b l o c k st ob ema t r i c e so ft i l e s ;3 2 x 1 6f o rs t i l e s , 1 6 x 1 6( o r3 2 x 8 )f o rd t i l e s .Th ewh o l e9 6 k bo fVRAM c a nb es e e na s 6c h a r b l o c k s . Ass a i d , t h e r ea r e6t i l e b l o c k s , t h a ti s4f o rb a c k g r o u n d s( 0 3 )a n d2f o rs p r i t e s( 4 5 ) .Fo rt i l e db a c k g r o u n d s , t i l e c o u n t i n gs t a r t sa ta g i v e ncharacter base block ( b l o c kf o rt h ec h a r a c t e rb a s e , CBBf o rs h o r t ) , wh i c ha r ei n d i c a t e db yREG_BGxCNT{ 2 3 } .Sp r i t et i l e i n d e x i n ga l wa y ss t a r t sa tt h el o we rs p r i t eb l o c k( b l o c k4 , s t a r t i n ga t0601:0000h) . I t ' db en i c ei ft i l e i n d e x i n gf o l l o we dt h es a mes c h e mef o rb a c k g r o u n d sa n ds p r i t e s , b u ti td o e s n ' t .Fo rs p r i t e s , n u mb e r i n ga l wa y s f o l l o wss t i l e s( 2 0 ho f f s e t s )e v e nf o rd t i l e s , b u tb a c k g r o u n d ss t i c kt ot h e i ri n d i c a t e dt i l e s i z e : 2 0 ho f f s e t si n4 b p pmo d e , 4 0 ho f f s e t sf o r 8 b p pmo d e . Bg vs sprite tile indexing Sp r i t e sa l wa y sh a v e3 2b y t e sb e t we e nt i l ei n d i c e s , b gt i l e i n d e x i n gu s e s3 2o r6 4b y t eo f f s e t s , d e p e n d i n go nt h e i rs e tb i t d e p t h . No w, b o t hr e g u l a rb a c k g r o u n d sa n ds p r i t e sh a v e1 0b i t sf o rt i l ei n d i c e s .Th a tme a n s1 0 2 4a l l o we di n d i c e s .Si n c ee a c hc h a r b l o c kc o n t a i n s 5 1 2s t i l e s , y o uc a na c c e s sn o to n l yt h eb a s eb l o c k , b u ta l s ot h eo n ea f t e rt h a t .An di fy o u rb a c k g r o u n di su s i n gd t i l e s , y o uc a na c t u a l l y a c c e s sat o t a l o ff o u rb l o c k s !No w, s i n c et i l e db a c k g r o u n d sc a ns t a r tc o u n t i n ga ta n yo ft h ef o u rb a c k g r o u n dc h a r b l o c k s , y o umi g h tb e t e mp t e dt ot r yt ou s et h es p r i t ec h a r b l o c k s( b l o c k s4a n d5 )a swe l l .Ont h ee mu l a t o r sI ' v et e s t e d , t h i sd o e si n d e e dwo r k .Onar e a l GBA, h o we v e r , i td o e sn o t .Th i si so n eo ft h er e a s o n swh yy o uneed t ot e s to nr e a l h a r d wa r e .Fo rmo r eo nt h i ss u b j e c ts e et h eb a c k g r o u n dt i l e s u b t l e t i e sa n dt h ecbb_demo. An o t h e rt h i n gy o un e e dt ok n o wa b o u ta v a i l a b l ec h a r b l o c k si st h a ti no n eo ft h eb i t ma pmo d e s , t h eb i t ma p se x t e n di n t ot h el o we r s p r i t eb l o c k .Fo rt h a tr e a s o n , y o uc a no n l yu s et h eh i g h e rs p r i t eb l o c k( c o n t a i n i n gt i l e s5 1 2t o1 0 2 3 )i nt h i sc a s e . Th a n k st ot h ewo n d e r f u l c o n c e p to ftypedefs , y o uc a nd e f i n et y p e sf o rt i l e sa n dc h a r b l o c k ss ot h a ty o uc a nq u i c k l yc o meu pwi t h t h ea d d r e s s e so ft i l e sb ys i mp l ea r r a y a c c e s s e s .Ana l t e r n a t i v et ot h i si su s i n gma c r o so ri n l i n ef u n c t i o n st oc a l c u l a t et h er i g h ta d d r e s s e s . I nt h ee n di th a r d l yma t t e r swh i c hme t h o dy o uc h o o s e , t h o u g h .Ofc o u r s e , t h et y p e d e fme t h o da l l o wst h eu s eo ft h esizeofo p e r a t o r , wh i c hc a nb eq u i t eh a n d ywh e ny o un e e dt oc o p yac e r t a i na mo u n to ft i l e .Al s o , s t r u c t c o p i e sa r ef a s t e rt h a ns i mp l el o o p s , a n dr e q u i r e l e s sCc o d et o o . // tile 8x8@4bpp: 32bytes; 8 ints typedef struct { u32 data[8]; } TILE, TILE4; // d-tile: double-sized tile (8bpp) typedef struct { u32 data[16]; } TILE8; // tile block: 32x16 tiles, 16x16 d-tiles typedef TILE CHARBLOCK[512]; typedef TILE8 CHARBLOCK8[256]; #define tile_mem ( (CHARBLOCK*)0x06000000) #define tile8_mem ((CHARBLOCK8*)0x06000000) //In code somewhere TILE *ptr= &tile_mem[4][12]; // block 4 (== lower object block), tile 12 // Copy a tile from data to sprite-mem, tile 12 tile_mem[4][12] = *(TILE*)spriteData; file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 64/331 28-03-13 Tonc : GBA Programming in rot13 7.4.3. Palettes and tile colors Sp r i t e sa n db a c k g r o u n d sh a v es e p a r a t ep a l e t t e s .Th eb a c k g r o u n dp a l e t t eg o e sf i r s ta t0500:0000h, i mme d i a t e l yf o l l o we db yt h es p r i t e p a l e t t e( 0500:0200h) .Bo t hp a l e t t e sc o n t a i n2 5 6e n t r i e so f1 5 b i tc o l o r s . I n8 b i tc o l o rmo d e , t h ep i x e l v a l u ei nt h et i l e si sp a l e t t e i n d e xf o rt h a tp i x e l .I n4 b i tc o l o rmo d e , t h ep i x e l v a l u ec o n t a i n st h el o we r n y b b l eo ft h ep a l e t t ei n d e x ;t h eh i g hn y b b l ei st h epalbank i n d e x , wh i c hc a nb ef o u n di ne i t h e rt h es p r i t e ' sa t t r i b u t e s , o rt h eu p p e rn y b b l e o ft h et i l e s .I ft h ep i x e l v a l u ei s0 , t h e nt h a tp i x e l wo n ' tb er e n d e r e d( i . e . , wi l l b et r a n s p a r e n t ) . Be c a u s eo f1 6 c o l o rmo d ea n dt h et r a n s p a r e n c yi s s u e , i ti sessential t h a ty o u rb i t ma pe d i t o rl e a v e st h ep a l e t t ei n t a c t .Ik n o wf r o m p e r s o n a l e x p e r i e n c et h a tMSPa i n ta n dt h eVi s u a l Cb i t ma pe d i t o rd o n ' t , s oy o umi g h twa n tt ou s es o me t h i n ge l s e .Fa v o r i t e sa mo n go t h e r GBAd e v e l o p e r sa r eGr a p h i c sGa l ea n dGI MP.Ofc o u r s e , s i n c eIh a v emymyo wnb i t ma pe d i t o r , Ip r e f e rt ou s et h a t . 7.5. Summary Th i si sas h o r tl i s to fv a r i o u sa t t r i b u t e so fs p r i t e sa n db a c k g r o u n d s .I t ' sa l r i g h ti fy o ud o n ' tu n d e r s t a n di tr i g h ta wa y ;I ' l l e x p l a i ni nmo r e d e t a i l i nt h ef o l l o wi n gp a g e s . Subject Backgrounds Sprites Nu mb e r 4( 2a f f i n e ) 1 2 8( 3 2a f f i n e ) Ma xs i z e reg: 5 1 2 x 5 1 2 aff: 1 0 2 4 x 1 0 2 4 6 4 x 6 4 Co n t r o l REG_BGxCNT OAM Ba s et i l eb l o c k 0 3 4 Av a i l a b l et i l e sids reg: 0 1 0 2 3 aff: 0 2 5 5 modes 0-2: 0 1 0 2 3 modes 3-5: 5 1 2 1 0 2 3 Ti l eme mo r yo f f s e t s Pe rt i l es i z e : 4bpp: s t a r t =b a s e+ id* 3 2 8bpp: s t a r t =b a s e+ id* 6 4 Al wa y sp e r4 b p pt i l es i z e : s t a r t =b a s e+id* 3 2 Ma p p i n g I fas p r i t ei sm × n t i l e si ns i z e : reg: t h ef u l l ma pi s d i v i d e di n t oma p b l o c k s 1D mapping: t h em*n s u c c e s s i v et i l e s o f3 2 ×3 2t e g e l s . a r eu s e d , s t a r t i n ga tid ( b a n k e dma p ) 2D mapping: t i l e b l o c k sa r e3 2 ×3 2 aff: o n ema t r i xo ft e g e l s ,ma t r i c e s ;t h eu s e dt i l e sa r et h en j u s tl i k ean o r ma l b i t ma pc o l u mn so ft h em r o wso ft h ema t r i x , ( f l a tma p ) s t a r t i n ga tid. Fl i p p i n g Ea c ht i l ec a nb ef l i p p e d Fl i p st h ewh o l es p r i t e i n d i v i d u a l l y Pa l e t t e 0500:0000h 0500:0200h 7.6. What's in a name? We l l , s i n c ey o ua r eap r o g r a mme ry o us h o u l dk n o wt h ea n s we r : p l e n t y .I fy o ud i s a g r e e , v i s i tt h eHo wToWr i t eUn ma i n t a n a b l eCo d e we b s i t ea n dl o o ka tan u mb e ro ft h e i re n t r i e s .Myn a mi n gs c h e mei sab i td i f f e r e n tf r o mt h a to ft h eGBAc o mmu n i t y .Id o n ' td ot h i sj u s t b e c a u s eIf e e l l i k eb e i n gc o n t r a r y .If i n ds o meo ft h ec o n v e n t i o n a l n a me sa r ei n c o mp l e t e , mi s l e a d i n ga n da mb i g u o u s .If e e l l i t t l en e e d , a t l e a s ta tp r e s e n t , t of o l l o wt r a d i t i o ns i mp l yb e c a u s ee v e r y o n ee l s ed o e s .Bu ty o us t i l l n e e dt ok n o wt h et r a d i t i o n a l n a me s , s i mp l yb e c a u s e e v e r y o n ee l s ed o e s .Soh e r e ' sal i s to fd i f f e r e n c e si nn a me s . Subject Traditional Tonc Sp r i t ea n db gi ma g ed a t a t i l e s t i l e s Ti l e ma pe n t r i e s t i l e s( c a ny o uf e e l t h ec o n f u s i o n ? ) s c r e e n b l o c k e n t r i e s/SE Ma t r i xf o rt r a n s f o r ma t i o n s Ro t / Sc a l ema t r i x a f f i n ema t r i x/P Sp r i t et y p e s ? ?v sRo t / Sc a l e r e g u l a rv sa f f i n e Ba c k g r o u n dt y p e s t e x tv sr o t r e g u l a rv sa f f i n e De p o s i t o r yf o rs p r i t et i l e s ( 0601:0000) OAMData( i . e . , n o tt h ereal OAM, wh i c hi s tile_mem_obj a t0700:0000) OAM ( 0700:0000) OAMDatao rOAMMem file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm oam_mem 65/331 28-03-13 file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm Tonc : GBA Programming in rot13 66/331 28-03-13 Tonc : GBA Programming in rot13 8. Regular sprites Sp r i t ei n t r o d u c t i o n . Sp r i t ei ma g ed a t aa n dma p p i n gmo d e . Sp r i t ec o n t r o l : Ob j e c tAt t r i b u t eMe mo r y . Ob j e c ta t t r i b u t e s : OBJ _ATTR. Bi t f i e l dma c r o s( OAM o ro t h e r wi s e ) . De mot i me . 8.1. Sprite introduction Ac c o r d i n gt oWe b s t e r ' s , as p r i t ei s“ a ni ma g i n a r yb e i n go rs p i r i t , a saf a i r y , e l f , o rg o b l i n ” .Ri g h t , g l a dt h a t ' sc l e a r e du p . Fo rg a me s , t h o u g h , wh e nr e f e r r i n gt oas p r i t eo n ei su s u a l l yt a l k i n ga b o u t“ a[ s ma l l ]a n i ma t e do b j e c tt h a tc a nmo v ef r e e l y f r o mt h eb a c k g r o u n d ”( PERN) .Pr i ma r ye x a mp l e sa r eg a mec h a r a c t e r s , b u ts t a t u so b j e c t sl i k es c o r e sa n dl i f eb a r sa r e o f t e ns p r i t e sa swe l l .Fi g8 . 1o nt h er i g h ts h o wsas p r i t eo fe v e r y b o d y ' sf a v o r i t ev a mp i r ej e l l y f i s h , t h eme t r o i d .Iwi l l u s e t h i ss p r i t ei nt h ed e moa tt h ee n do ft h i sc h a p t e r . Fig 8.1. Me t r o i d . Ra wr . Sp r i t e sa r eal i t t l et r i c k i e rt ou s et h a nab i t ma pb a c k g r o u n d , b u tn o tb ymu c h .Yo uj u s th a v et op a yal i t t l emo r ea t t e n t i o nt o wh a ty o u ' r ed o i n g .Fo rs t a r t e r s , t h eg r a p h i c sh a v et ob eg r o u p e di n t o8 x 8t i l e s ;ma k es u r ey o u rg r a p h i c sc o n v e r t e rc a nd o t h a t .As i d ef r o mt h eo b v i o u sa c t i o n ss u c ha se n a b l i n gs p r i t e si nt h ed i s p l a yc o n t r o l a n dl o a d i n gu pt h eg r a p h i c sa n dp a l e t t e , y o ua l s oh a v e t os e t u pt h ea t t r i b u t e so ft h es p r i t e sc o r r e c t l yi nOAM.Mi s sa n yo ft h e s es t e p sa n dy o u ' l l s e en o t h i n g .Th e s et h i n g sa n dmo r ewi l l b e c o v e r e di nt h i sc h a p t e r . Essential Sprite Steps Th e r ea r e3t h i n g st h a ty o uh a v et od or i g h tt og e ts p r i t e st os h o wu p : Lo a dt h eg r a p h i c sa n dp a l e t t ei n t oo b j e c tVRAM a n dp a l e t t e . Se ta t t r i b u t e si nOAM t ou s et h ea p p r o p r i a t et i l e sa n ds e tt h er i g h ts i z e . Swi t c ho no b j e c t si nREG_DI SPCNT, a n ds e tt h ema p p i n gmo d et h e r et o o . Sprites aren't objects Ors o me t h i n gl i k et h a t .Ik n o wi ts o u n d swe i r d , b u tt h emo r eIt h i n ka b o u ti t , t h emo r eIr e a l i z et h a ts p r i t e sa n do b j e c t s s h o u l d n ' tb ec o n s i d e r e di n t e r c h a n g e a b l e .Th et e r m‘ o b j e c t ’ , i sah a r d wa r ef e a t u r e , c o n t r o l l e di nOAM.Ri g h tn o w, It h i n kt h a t ‘ s p r i t e ’i smo r eo fac o n c e p t u a l t e r m, a n ds h o u l db er e s e r v e df o ra c t o r s , l i k ep l a y i n gc h a r a c t e r s , mo n s t e r s , b u l l e t s , e t c .Th e s e c a ni nf a c tb eb u i l tu po fmu l t i p l eh a r d wa r eo b j e c t s , o re v e nu s eab a c k g r o u n d . Yo uc o u l da l s ot h i n go fi ti nt h i swa y : o b j e c t sa r esystem e n t i t i e sl i n k e dt ot h ec o n s o l ei t s e l f , a n ds p r i t e sa r egame e n t i t i e s , l i v i n gi nt h eg a mewo r l d .Th ed i f f e r e n c ema yb es u b t l e , b u ta ni mp o r t a n to n e . Th i si sme r e l ymyo p i n i o n , a n dIc a n ' ts a yh o wr i g h tIa mi nt h i s .To n cs t i l l s wi t c h e sb a c ka n df o r t hb e t we e nt h et wowo r d s b e c a u s ei t ' st o ol a t et od oa n y t h i n ga b o u ti tn o w.Me ac u l p a .I ' dl o v et oh e a rt h eo p i n i o no fo t h e r so nt h es u b j e c t , s of e e l f r e e t os p e a ky o u rmi n di fy o uwa n t . 8.2. Sprite image data and mapping mode Li k eIs a i di nt h es p r i t ea n db a c k g r o u n do v e r v i e w, s p r i t e sa r ec o mp o s e do fan u mb e ro f8 x 8mi n i b i t ma p sc a l l e dt i l e s , wh i c hc o mei nt wo t y p e s : 4 b p p( s t i l e s , 3 2b y t e sl o n g )a n d8 b p p( d t i l e s , 6 4b y t e sl o n g ) .Th et i l e sa v a i l a b l ef o rs p r i t e sa r es t o r e di nobject VRAM, o r OVRAM f o rs h o r t .OVRAM i s3 2 k bl o n ga n di sma p p e do u tb yt h el a s tt woc h a r b l o c k so ftile_mem, wh i c ha r ea l s ok n o wna st h e l o we r( b l o c k4 , s t a r t i n ga t0601:0000h)a n dh i g h e r( b l o c k5 , 0601:4000h)s p r i t eb l o c k s .Co u n t i n ga l wa y ss t a r t sa tt h el o we rs p r i t e b l o c ka n di salways d o n ei n3 2b y t eo f f s e t s , me a n i n gt h a ts p r i t e t i l e#1i sa t0601:0020h, n oma t t e rwh a tt h eb i t d e p t hi s( s e e t a b l e8 . 1 ) .Wi t h4 0 0 0 hb y t e sp e rc h a r b l o c k , aq u i c kc a l c u l a t i o nwi l l s h o wy o ut h a tt h e r ea r e5 1 2t i l e si ne a c hc h a r b l o c k , g i v i n gat o t a l r a n g eo f1 0 2 4 .Ho we v e r , s i n c et h eb i t ma pmo d e se x t e n di n t ot h el o we rs p r i t eb l o c k , y o uc a no n l yu s et h eh i g h e rs p r i t eb l o c k( c o n t a i n i n g t i l e s5 1 2t o1 0 2 3 )i nmo d e s3 5 . I tma ys e e mt h a tc a l c u l a t i n gt h o s et i l ea d d r e s s e sc a nb ea n n o y i n g , a n di two u l db ei fy o uh a dt od oi tma n u a l l y .Th a t ' swh yIh a v e ma p p e dt h ewh o l eo fVRAM wi t hac h a r b l o c k / t i l ema t r i xc a l l e dtile_mem, a sd i s c u s s e di nt h eo v e r v i e w.Ne e dt i l e#1 2 3o fOVRAM? Th a t ' db etile_mem[4][123].Ne e di t sa d d r e s s ?Us et h ea d d r e s so p e r a t o r : &tile_mem[4][123].Qu i c k , e a s y , s a f e . Al s o , d o n ' tf o r g e tt h a tt h es p r i t e sh a v et h e i ro wnp a l e t t ewh i c hs t a r t sa t0500:0200h( r i g h ta f t e rt h eb a c k g r o u n dp a l e t t e ) .I fy o u a r ec e r t a i ny o u ' v el o a d e dy o u rt i l e sc o r r e c t l yb u tn o t h i n gs h o wsu p , i t ' sp o s s i b l ey o uf i l l e dt h ewr o n gp a l e t t e . memory 0601: 0000 0020 0040 0060 0080 0100 ... file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 67/331 28-03-13 Tonc : GBA Programming in rot13 4bpp tile 8bpp tile 0 0 1 2 2 3 4 4 5 Table 8.1: t i l ec o u n t i n gf o rs pr i t e s , a l wa yspe r3 2b yt e s . ( Yo uc a n u s eo d dn u mb e r sf o r8 b ppt i l e s , b u tb es u r eyo uf i l l t h eVRAM a c c or d i n g l y. ) Bitmap modes and Object VRAM On l yt h eh i g h e rs p r i t eb l o c ki sa v a i l a b l ef o rs p r i t e si nmo d e s3 5 .I n d e x i n gs t i l l s t a r t sa tt h el o we rb l o c k , t h o u g h , s ot h et i l e r a n g ei s5 1 2 1 0 2 3 . 8.2.1. The sprite mapping mode Sp r i t e sa r e n ' tl i mi t e dt oas i n g l et i l e .I nf a c t , mo s ts p r i t e sa r el a r g e r( s e eTa b l e8 . 4f o ral i s to ft h ea v a i l a b l es i z e sf o rGBAs p r i t e s ) . La r g e rs p r i t e ss i mp l yu s emu l t i p l et i l e s , b u tt h i sma yp r e s e n tap r o b l e m.Fo rb a c k g r o u n d s , y o uc h o o s ee a c ht i l ee x p l i c i t l ywi t ht h et i l e ma p .I nt h ec a s eo fs p r i t e s , y o uh a v et woo p t i o n s : 1 Da n d2 Dma p p i n g .Th ed e f a u l ti s2 Dma p p i n g , a n dy o uc a ns wi t c ht o1 Dma p p i n g b ys e t t i n gREG_DISPCNT{ 6 } . Ho wd ot h e s ewo r k ?Co n s i d e rt h ee x a mp l es p r i t eo ff i g8 . 2 a , s h o wi n gt h eme t r o i do ff i g8 . 1d i v i d e di n t ot i l e s .I n2 Dma p p i n g , y o u ' r e i n t e r p r e t i n gt h es p r i t ec h a r b l o c k sa so n eb i gb i t ma po f2 5 6 x 2 5 6p i x e l sa n dt h es p r i t ear e c t a n g l eo u to ft h a tb i t ma p( s t i l l d i v i d e di n t ot i l e s , o fc o u r s e ) .I nt h i sc a s e , e a c ht i l e r o wo fas p r i t ei sa ta3 2 t i l eo f f s e t .Th i si ss h o wni nf i g 8 . 2 b .Ont h eo t h e rh a n d , y o uc a na l s oc o n s i d e r t h ec h a r b l o c k sa so n eb i ga r r a yo ft i l e s , a n dt h et i l e so fe v e r ys p r i t ea r ec o n s e c u t i v e .Th i si ss h o wni nf i g8 . 2 c .Th en u mb e r si nf i g8 . 2 a s h o wt h ed i f f e r e n c eb e t we e n1 Da n d2 Dma p p i n g .As s u mi n gwes t a r ta tt i l e0 , t h er e da n dc y a nn u mb e r sf o l l o w2 da n d1 dma p p i n g , r e s p e c t i v e l y . Fr o maGBAp r o g r a mmi n gv i e wp o i n t , i ti se a s i e rt ou s e1 dma p p i n g , a sy o ud o n ' th a v et owo r r ya b o u tt h eo f f s e to fe a c ht i l e r o w wh e ns t o r i n gs p r i t e s .Ho we v e r , a c t u a l l ycreating s p r i t e si se a s i e ri n2 d mo d e .Ime a n , d oy o ureally wa n tt oe d i tab i t ma pt i l eb yt i l e ? Th a t ' swh a tIt h o u g h t .Ofc o u r s e , i ts h o u l db et h ee x p o r t i n gt o o l ' sj o bt oc o n v e r ty o u rs p r i t e sf r o m2 dt o1 dma p p i n gf o ry o u .Yo uc a nd o t h i swi t hUs e n t i t o o . Fig 8.2b: h o wf i g8 . 2 as h o u l db es t o r e di nme mo r ywh e n u s i n g2 dma ppi n g . Fig 8.2c: h o wf i g8 . 2 as h o u l db es t o r e di nme mo r ywh e n u s i n g1 dma ppi n g . Fig 8.2a: z o o me do u tv e r s i o no fFi g8 . 1 , d i v i de di n t ot i l e s ; c o l o r e dn u mb e r s i n d i c a t ema ppi n gmo d e : r e df o r2 d , c ya n f o r1d . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 68/331 28-03-13 Tonc : GBA Programming in rot13 Object data conversion via CLI So mec o mma n d l i n ei n t e r f a c e sc a nt i l eb i t ma p sf o ru s ewi t ho b j e c t s( a n dt i l e ma p s ) .I ns o mec a s e s , t h e yc a na l s oc o n v e r t i ma g e swi t hmu l t i p l es p r i t e f r a me st oas e to fo b j e c tt i l e si n1 Do b j e c tma p p i n gmo d e .I ff o o . b mpi sa6 4 x 1 6b i t ma pwi t h4 1 6 x 1 6o b j e c t s , h e r e ' sh o wy o uc a nc o n v e r ti tt o8 x 84 b p pt i l e su s i n gg f x 2 g b aa n dg r i t( f l a g sf o r1 Dma p p i n ga r eg i v e ni n b r a c k e t s ) # gfx2gba # 4x 16x16@4 objects (C array; u8 foo_Bitmap[], u16 master_Palette[]; foo.raw.c, master.pal.c) gfx2gba -fsrc -c16 -t8 [-T32] foo.bmp # grit # 4x 16x16@4 objects (C array; u32 fooTiles[], u16 fooPal[]; foo.c, foo.h) grit foo.bmp -gB4 [-Mw 2 -Mh 2] Twon o t e so nt h e1 Dma p p i n gf l a g sh e r e .Fi r s t , g f x 2 g b ac a no n l yme t a t i l e( -T)s q u a r eo b j e c t s ;f o rs o me t h i n gl i k e1 6 x 8 o b j e c t sy o u ' dn e e dt od ot h e1 Dma p p i n gy o u r s e l f .Se c o n d , g r i t ' sme t a t i l i n gf l a g s( -Mwa n d-Mh)c a nb ea n y t h i n g , a n du s e t i l eu n i t s , n o tp i x e l s . Size units: tiles vs pixels Th ed e f a u l tu n i tf o rb i t ma pd i me n s i o n si so fc o u r s et h ep i x e l , b u ti nt i l e dg r a p h i c si ti ss o me t i me smo r eu s e f u l t ou s et i l e sa s t h eb a s i cu n i t , i . e . , t h ep i x e l s i z ed i v i d e db y8 .Th i si se s p e c i a l l yt r u ef o rb a c k g r o u n d s .I nmo s tc a s e st h ec o n t e x twi l l s u f f i c et o i n d i c a t ewh i c ho n ei sme a n t , b u ta tt i me sIwi l l d e n o t et h eu n i t swi t ha‘ p ’f o rp i x e l so r‘ t ’f o rt i l e s .Fo re x a mp l e , a6 4 x 6 4 p s p r i t ei st h es a mea sa8 x 8 ts p r i t e . 8.3. Sprite control: Object Attribute Memory Mu c hu n l i k ei nt h eb i t ma pmo d e s , y o ud o n ' th a v et od r a wt h es p r i t e sy o u r s e l f : t h eGBAh a ss p e c i a l h a r d wa r et h a td o e si tf o ry o u .Th i s c a ng e tt h es p r i t e so ns c r e e nf a s t e rt h a ny o uc o u l de v e ra c h i e v ep r o g r a mma t i c a l l y .Th e r ea r es t i l l l i mi t s , t h o u g h : t h e r ei sal i mi tt ot h e a mo u n to fs p r i t e p i x e l sy o uc a nc r a mi no n es c a n l i n e .Ab o u t9 6 0 , i ft h ef o r aa r ea n y t h i n gt og ob y . Soy o ud o n ' th a v et od r a wt h es p r i t e sy o u r s e l f ;h o we v e r , y o udo n e e dt ot e l l t h eGBAh o wy o uwa n tt h e m.Th i si swh a tt h eObject Attribute Memory –OAM f o rs h o r t –i sf o r .Th i ss t a r t sa ta d d r e s s0700:0000ha n di s1 0 2 4b y t e sl o n g .Yo uc a nf i n dt wot y p e so f s t r u c t u r e si nOAM: t h eOBJ_ATTR s t r u c tf o rr e g u l a rs p r i t ea t t r i b u t e s , a n dt h eOBJ_AFFINE s t r u c tc o n t a i n i n gt h et r a n s f o r ma t i o n d a t a .Th ed e f i n i t i o n so ft h e s es t r u c t u r e sc a nb ef o u n db e l o w.No t et h a tn a me sma yv a r yf r o ms i t et os i t e . typedef struct tagOBJ_ATTR { u16 attr0; u16 attr1; u16 attr2; s16 fill; } ALIGN4 OBJ_ATTR; typedef struct OBJ_AFFINE { u16 fill0[3]; s16 pa; u16 fill1[3]; s16 pb; u16 fill2[3]; s16 pc; u16 fill3[3]; s16 pd; } ALIGN4 OBJ_AFFINE; Th e r ea r eaf e wi n t e r e s t i n gt h i n g sa b o u tt h e s es t r u c t u r e s .Fi r s t , y o us e eal o to ffille rf i e l d s .Se c o n d , i fy o uwo u l dt a k e4OBJ_ATTR s t r u c t u r e sa n dl a yt h e mo v e ro n eOBJ_AFFINEs t r u c t u r e , a sd o n ei nt a b l e2 , y o u ' ds e et h a tt h ef i l l e r so fo n ewo u l de x a c t l yc o v e rt h e d a t ao ft h eo t h e r , a n dv i c ev e r s a .Th i si sn oc o i n c i d e n c e : OAM i si nf a c tawe a v eo fOBJ_ATTRsa n dOBJ_AFFINEs .Wh ywo u l d Ni n t e n d ou s eawe a v ei n s t e a do fs i mp l yh a v i n go n es e c t i o no fa t t r i b u t e sa n do n ef o rt r a n s f o r md a t a ?Th a t ' sag o o dq u e s t i o na n d d e s e r v e sag o o da n s we r .Wh e nIh a v eo n e , I ' l l t e l l y o u( I ' mg u e s s i n gi t ' sad a t a a l i g n me n tt h i n g ) .Al s o , n o t et h a tt h ee l e me n t so ft h e OBJ_AFFINEa r esigned s h o r t s .I ' v eg o n et h r o u g hawo r l do fh u r twi t ht h eobj_affc o d eb e c a u s eIu s e du16i n s t e a do fs16.Wi t h 1 0 2 4b y t e sa to u rd i s p o s a l , weh a v er o o mf o r1 2 8OBJ_ATTRs t r u c t u r e sa n d3 2OBJ_AFFINEs .Th er e s to ft h i sf i l ewi l l e x p l a i n r e g u l a rs p r i t e st h a to n l yu s eOBJ_ATTR.Iwa n tt og i v et h ea f f i n et r a n s f o r ma t i o nma t r i xt h ef u l l ma t h e ma t i c a l t r e a t me n ti td e s e r v e sa n d wi l l s a v ea f f i n es p r i t e sf o rl a t e r . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 69/331 28-03-13 Tonc : GBA Programming in rot13 mem (u16) OBJ_ATTR 0 3 4 012 OBJ_AFFINE 7 8 012 pa b c 012 pb f 012 pc pd Table 8.2: memory interlace of OBJ_ATTR and OBJ_AFFINE structures. Force alignment on OBJ_ATTRs Aso fd e v k i t ARM r 1 9 , t h e r ea r en e wr u l e so ns t r u c ta l i g n me n t s , wh i c hme a n st h a ts t r u c t sma yn o ta l wa y sb ewo r da l i g n e d , a n di nt h ec a s eo fOBJ _ATTRs t r u c t s( a n do t h e r s ) , me a n st h a ts t r u c t c o p i e sl i k et h eo n ei noam_update()l a t e ro n , wi l l n o to n l yb es l o w, t h e yma ya c t u a l l yb r e a k .Fo rt h a tr e a s o n , Iwi l l f o r c ewo r d a l i g n me n to nma n yo fmys t r u c t swi t h ‘ ALIGN4’ , wh i c hi sama c r of o r‘ __attribute__((aligned(4)))’ .Fo rmo r eo nt h i s , s e et h es e c t i o no nd a t a a l i g n me n t . 8.4. Object attributes: OBJ_ATTR Th eb a s i cc o n t r o l f o re v e r ys p r i t ei st h eOBJ_ATTRs t r u c t u r e .I tc o n s i s t so ft h r e e1 6 b i ta t t r i b u t e sf o rs u c hq u a l i t i e sa ss i z e , s h a p e , p o s i t i o n , b a s et i l ea n dmo r e .Ea c ho ft h et h r e ea t t r i b u t e si sc o v e r e db e l o w. 8.4.1. Attribute 0 Th ef i r s ta t t r i b u t ec o n t r o l sag r e a td e a l , b u tt h emo s ti mp o r t a n tp a r t sa r ef o rt h ey c o o r d i n a t e , a n dt h es h a p eo ft h es p r i t e .Al s oi mp o r t a n t a r ewh e t h e ro rn o tt h es p r i t ei st r a n s f o r ma b l e( a na f f i n es p r i t e ) , a n dwh e t h e rt h et i l e sa r ec o n s i d e r e dt oh a v eab i t d e p t ho f4( 1 6c o l o r s , 1 6s u b p a l e t t e s )o r8( 2 5 6c o l o r s/1p a l e t t e ) . OBJ_ATTR.attr0 FE D C BA 98 76543210 Sh CM Mos GM OM Y bits name define description 0 7 Y ATTR0_Y# r k st h et o po ft h es p r i t e . Y coordinate .Ma 8 9 OM ATTR0 _REG, et oh i d et h es p r i t eo rg o v e r na f f i n emo d e . (Affine) object mode .Us ATTR0 _AFF, 00.No r ma l r e n d e r i n g . ATTR0 _HI DE, 01.Sp r i t ei sa na f f i n es p r i t e , u s i n ga f f i n ema t r i xs p e c i f i e db y ATTR0 _AFF_DBL. attr1{9-D} ATTR0_MODE# 10.Di s a b l e sr e n d e r i n g( h i d e st h es p r i t e ) 11.Af f i n es p r i t eu s i n gd o u b l er e n d e r i n ga r e a .Se ea f f i n es p r i t e sf o r mo r e . AB GM ATTR0 _BLEND, Gfx mode .Fl a g sf o rs p e c i a l e f f e c t s . ATTR0 _WI N. 00.No r ma l r e n d e r i n g . ATTR0_GFX# 01.En a b l e sa l p h ab l e n d i n g .Co v e r e dh e r e . 10.Ob j e c ti sp a r to ft h eo b j e c twi n d o w.Th es p r i t ei t s e l fi s n ' tr e n d e r e d , b u ts e r v e sa sama s kf o rb g sa n do t h e rs p r i t e s .( It h i n k , h a v e n ' tu s e di t y e t ) 11.Fo r b i d d e n . C Mo s ATTR0 _MOSAI C En a b l e smo s a i ce f f e c t .Co v e r e dh e r e . D CM ATTR0 _4 BPP, ATTR0 _8 BPP EF Sh 6c o l o r s( 4 b p p )i fc l e a r e d ;2 5 6c o l o r s( 8 b p p )i fs e t . Color mode .1 ATTR0 _SQUARE,Sprite shape .Th i sa n dt h es p r i t e ' ss i z e( e t e r mi n e st h es p r i t e ' s attr1{E-F})d ATTR0 _WI DE, r e a l s i z e , s e et a b l e8 . 4 . ATTR0 _TALL. ATTR0_SHAPE# Twoe x t r an o t e so na t t r i b u t e0 .Fi r s t , attr0c o n t a i n st h ey c o o r d i n a t e ;attr1c o n t a i n st h ex c o o r d i n a t e .Fo rs o mer e a s o nIk e e p me s s i n gt h e s et wou p ;i fy o uf i n dy o u rs p r i t ei smo v i n gl e f twh e ni ts h o u l db emo v i n gu p , t h i sma yb ewh y .Se c o n d , t h ea f f i n ea n dg f x mo d e sa r e n ' ta l wa y sn a me da ss u c h .I np a r t i c u l a r , attr0{9}i ss i mp l yr e f e r r e dt oa sthe d o u b l e s i z ef l a g , e v e nt h o u g hi to n l ywo r k si n t h a tc a p a c i t yi fb i t8i ss e tt o o .I fi ti s n ' t , t h e ni th i d e st h es p r i t e .It h i n kt h a ti t ' sa c t u a l l yt a k e no u to ft h eo b j e c tr e n d e r i n gs t a g ee n t i r e l y l e a v i n gmo r et i mef o rt h eo t h e r s , b u tI ' mn o t1 0 0 %s u r eo ft h a t . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 70/331 28-03-13 Tonc : GBA Programming in rot13 shape\size 00 01 10 11 00 8x8 16x1632x3264x64 01 16x832x8 32x1664x32 10 8x168x32 16x3232x64 Table 8.4: GBA sprite sizes 8.4.2. Attribute 1 Th ep r i ma r yp a r t so ft h i sa t t r i b u t ea r et h ex c o o r d i n a t ea n dt h es i z eo ft h es p r i t e .Th er o l eo fb i t s8t o1 4d e p e n do nwh e t h e ro rn o tt h i si s aa f f i n es p r i t e( d e t e r mi n e db yattr0{ 8 } ) .I fi ti s , t h e s eb i t ss p e c i f ywh i c ho ft h e3 2OBJ_AFFINEss h o u l db eu s e d .I fn o t , t h e yh o l d f l i p p i n gf l a g s . OBJ_ATTR.attr1 FE D C BA9 876543210 Sz VF HF - - AID X - bits name define description 0 8 X ATTR1_X# r k sl e f to ft h es p r i t e . X coordinate .Ma 9 D AI D ATTR1_AFF# e c i f i e st h eOAM_AFF_ENTYt h i ss p r i t eu s e s .Va l i donly i ft h ea f f i n e Affine index .Sp f l a g( attr0{ 8 } )i ss e t . CD HF, VF ATTR1 _HFLI P,Horizontal/vertical flipping f l a g s .Us e donly i ft h ea f f i n ef l a g( sc l e a r ; attr0)i ATTR1 _VFLI P.o t h e r wi s et h e y ' r ep a r to ft h ea f f i n ei n d e x . ATTR1_FLIP# EF Sz ATTR1_SIZE# n d a .To g e t h e rwi t ht h es h a p eb i t s( EF} )t h e s ed e t e r mi n et h e Sprite size .Ki attr0{ s p r i t e ' sr e a l s i z e , s e et a b l e8 . 4 . I ' l l s a yi th e r et o o : attr0c o n t a i n sy, attr1c o n t a i n sx.No t et h a tb i t s1 2a n d1 3h a v ead o u b l er o l ea se i t h e rf l i p p i n gf l a g so ra f f i n e i n d e x .An di fy o ua r ewo n d e r i n gi fy o uc a ns t i l l f l i pa f f i n es p r i t e s , t h ea n s we ri sy e s : s i mp l yu s en e g a t i v es c a l e si nt h ema t r i x . 8.4.3. Attribute 2 Th i sa t t r i b u t et e l l st h eGBAwh i c ht i l e st od i s p l a ya n di t sb a c k g r o u n dp r i o r i t y .I fi t ' sa4 b p ps p r i t e , t h i si sa l s ot h ep l a c et os a ywh a ts u b p a l e t t es h o u l db eu s e d . OBJ_ATTR.attr2 FEDC BA 9876543210 PB bits name 0 9 TI D Pr TID description ATTR2_ID# Ba s etile-index o fs p r i t e .No t et h a ti nb i t ma pmo d e st h i smu s tb e5 1 2o rh i g h e r . AB Pr ATTR2_PRIO# g h e rp r i o r i t i e sa r ed r a wnf i r s t( a n dt h e r e f o r ec a nb ec o v e r e db yl a t e r Priority.Hi s p r i t e sa n db a c k g r o u n d s ) .Sp r i t e sc o v e rb a c k g r o u n d so ft h es a mep r i o r i t y , a n df o r s p r i t e so ft h es a mep r i o r i t y , t h eh i g h e rOBJ_ATTRsa r ed r a wnf i r s t . CF PB ATTR2_PALBANK# Palette-bank t ou s ewh e ni n1 6 c o l o rmo d e .Ha sn oe f f e c ti ft h ec o l o rmo d ef l a g ( attr0{ C} )i ss e t . 8.4.4. Attribute 3 Th e r ei sno a t t r i b u t e3 .Al t h o u g ht h eOBJ_ATTRs t r u c td o e shave af o u r t hh a l f wo r d , t h i si so n l yaf i l l e r .Th eme mo r yi nt h a tf i l l e r a c t u a l l yb e l o n g st ot h eOBJ_AFFINEs .No b o d yi st omi s t r e a tattr3i na n ywa y… i ft h e r e ' sa n ya f f i n es p r i t ea c t i v e . 8.4.5. OAM double buffering Yo ucould wr i t ea l l y o u rs p r i t ed a t ad i r e c t l yt ot h eOAM a t0700:0000h, b u tt h a tmi g h tn o ta l wa y sb et h eb e s tmo v e .I fi t ' sd o n e d u r i n gVDr a wt h e r e ' st h ep o s s i b i l i t yo ft e a r i n g .Ev e nwo r s e , y o umi g h tc h a n g et h es p r i t e ' st i l e i n d e xi nmi d r e n d e rs ot h a tt h et o pi si n o n ea n i ma t i o nf r a mea n dt h eb o t t o mi si na n o t h e r .No tap r e t t ys i g h t .Ac t u a l l y , t h i si s n ' ts o me t h i n gt owo r r ya b o u tb e c a u s ey o ucan't u p d a t eOAM d u r i n gVDr a w;i t ' sl o c k e dt h e n .Wh a t ' so f t e nd o n ei sc r e a t i n gas e p a r a t eb u f f e ro fOAM e n t r i e s( a l s ok n o wna st h eobject shadow)t h a tc a nb emo d i f i e da ta n yt i me , a n dt h e nc o p yt h a tt ot h er e a l OAM d u r i n gVBl a n k .He r e ' smyt a k eo nt h i s . OBJ_ATTR obj_buffer[128]; OBJ_AFFINE *const obj_aff_buffer= (OBJ_AFFINE*)obj_buffer; I ' mu s i n g1 2 8n o w, b u tIs u p p o s ey o uc o u l du s eal o we rn u mb e ri fy o ud o n ' tu s ea l l t h es p r i t e s .An y wa y , n o wy o uh a v ead o u b l eb u f f e r f o rb o t hOBJ_ATTRa n dOBJ_AFFINEd a t a , wh i c hi sa v a i l a b l ea ta n yg i v e nt i me .J u s tma k es u r ey o uc o p yi tt ot h ereal OAM wh e n t h et i mei sr i g h t . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 71/331 28-03-13 Tonc : GBA Programming in rot13 8.5. Bitfield macros (OAM or otherwise) Se t t i n ga n dc l e a r i n gi n d i v i d u a l b i t si se a s y , b u ts o me t i me si t ' sn o tt o oc o n v e n i e n tt od oi ta l l y o u r s e l f .Th i si se s p e c i a l l yt r u ef o rf i e l do fb i t s l i k ep o s i t i o n so rp a l e t t eb a n k s , wh i c hwo u l di n v o l v el o n gs t a t e me n t swi t hma s k sa n ds h i f t si fy o uwa n tt od oi tn i c e l y .Toi mp r o v eo nt h i s al i t t l eb i t , Ih a v ean u mb e ro fma c r o st h a tma ys h o r t e dt h ea mo u n to fa c t u a l c o d e .Th e r ea r ee s s e n t i a l l yt h r e ec l a s s e so fma c r o sh e r e , b u tb e f o r eIg oi n t ot h a t , Ih a v et oe x p l a i nal i t t l eb i tmo r ea b o u tt h eh a s h e d( foo‘ #” )d e f i n e si nt h ea t t r i b u t el i s t sa b o v e . Th eh a s hme a n st h a tf o re a c ho ft h e s e , t h e r ewi l l b et h r e e#d e f i n e swi t hfoo a st h e i rr o o t s : foo_MASK, foo_SHIFT, a n dfoo(_n). Th e s eg i v et h eb i t ma s k , b i t s h i f ta n dab i t f i e l ds e tma c r of o rt h ec o r r e s p o n d i n gt y p e . Fo re x a mp l e , t h eo n ea t t a c h e dt ot h et i l ei n d e x , ATTR2_ID#.Th et i l ei n d e xf i e l dh a s1 0b i t sa n ds t a r t sa tb i t 0 .Th ec o r r e s p o n d i n g d e f i n e st h e r e f o r ea r e : // The 'ATTR2_ID#' from the attr2 list means these 3 #defines exist #define ATTR2_ID_MASK 0x03FF #define ATTR2_ID_SHIFT 0 #define ATTR2_ID(n) ((n)<<ATTR2_ID_SHIFT) Mo s tGBAl i b r a r i e so u tt h e r eh a v e#d e f i n e sl i k et h e s e , a l b e i twi t hd i f f e r e n tn a me s .Th ea c t u a l ma c r oi s n ' t1 0 0 %s a f eb e c a u s ei td o e sn o r a n g ec h e c k i n g , b u ti t ' ss h o r ta n ds we e t .No w, a sf a ra sTo n c ' st e x ti sc o n c e r n e d , e v e r yt i mey o us e et h eh a s hi nt h ed e f i n el i s tf o ra r e g i s t e r , i t ' l l h a v et h e s et h r e e#d e f i n e st og owi t ht h a tn a me . Ia l s oh a v eas e c o n db a t c ho fma c r o sy o uc a nu s ef o rs e t t i n ga n dg e t t i n gs p e c i f i cf i e l d s , wh i c hu s et h ema s ka n ds h i f tn a me se x p l a i n e d a b o v e .I ' l l a d mi tt h ema c r o sl o o kh o r r i b l e , b u tIa s s u r ey o ut h e yma k es e n s ea n dc a nc o mei nh a n d y . // bit field set and get routines #define BF_PREP(x, name) ( ((x)<<name##_SHIFT)& name##_MASK ) #define BF_GET(x, name) ( ((x) & name##_MASK)>> name##_SHIFT ) #define BF_SET(y, x, name) (y = ((y)&~name##_MASK) | BF_PREP(x,name) ) #define BF_PREP2(x, name) #define BF_GET2(y, name) #define BF_SET2(y, x, name) ( (x) & name##_MASK ) ( (y) & name##_MASK ) (y = ((y)&~name##_MASK) | BF_PREP2(x, name) ) We l l , Id i dwa r ny o u .Th eǹame' a r g u me n th e r ei st h efoo f r o mb e f o r e .Th ep r e p r o c e s s o rc o n c a t e n a t i o no p e r a t o ri su s et oc r e a t et h e f u l l ma s ka n ds h i f tn a me s .Ag a i nu s i n gt h et i l e i n d e xa sa ne x a mp l e , t h e s ema c r o se x p a n dt ot h ef o l l o wi n g : // Create bitfield: attr2 |= BF_PREP(id, ATTR0_SHAPE); // becomes: attr2 |= (id<<ATTR2_ID_SHIFT) & ATTR2_ID_MASK; // Retrieve bitfield: id= BF_GET(attr2, ATTR2_ID); // becomes: id= (attr2 & ATTR2_ID_MASK)>>ATTR2_ID_SHIFT; // Insert bitfield: BF_SET(attr2, id, ATTR2_ID); // becomes: attr2= (attr&~ATTR2_ID_MASK) | ((id<<ATTR2_ID_SHIFT) & ATTR2_ID_MASK); BF_PREP()c a nb eu s e dt op r e p a r eab i t f i e l df o rl a t e ri n s e r t i o no rc o mp a r i s o n .BF_GET()g e t sab i t f i e l df r o mav a l u e , a n d BF_SET()s e t sab i t f i e l di nav a r i a b l e , wi t h o u td i s t u r b i n gt h er e s to ft h eb i t s .Th i si sb a s i c a l l yh o wb i t f i e l d sn o r ma l l ywo r k , e x c e p tt h a t t r u eb i t f i e l d sc a n n o tb ec o mb i n e dwi t hORa n ds u c h . Th ema c r o swi t ha‘ 2 ’i nt h e i rn a me swo r ki nas i mi l a rwa y , b u td on o ta p p l ys h i f t s .Th e s ec a nb eu s e f u l wh e ny o uh a v ea l r e a d y s h i f t e d#d e f i n e sl i k eATTR0_WIDE, wh i c hc a n ' tu s et h eo t h e ro n e s . // Insert pre-shifted bitfield: // BF_SET2(attr0, ATTR0_WIDE, ATTR0_SHAPE); attr0= (attr0&~ATTR0_SHAPE_MASK) | (id & ATTR0_SHAPE_MASK); No t et h a tn o n eo ft h e s et h r e eh a v ea n y t h i n gGBAs p e c i f i ci nt h e m;t h e yc a nb eu s e do na n yp l a t f o r m. Fi n a l l y , wh a tIc a l l myb u i l dma c r o s .Th e s ep i e c et o g e t h e rt h ev a r i o u sb i t f l a g si n t oas i n g l en u mb e ri na no r d e r l yf a s h i o n , s i mi l a rt o HAM' st o o l ma c r o s .Ih a v e n ' tu s e dt h e mt h a to f t e ny e t , a n dI ' mn o tf o r c i n gy o ut o , b u to no c c a s i o nt h e ya r eu s e f u l t oh a v ea r o u n d e s p e c i a l l yn e a ri n i t i a l i z a t i o nt i me . // Attribute 0 #define ATTR0_BUILD(y, shape, bpp, mode, mos, bld, win) ( file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm \ \ 72/331 28-03-13 Tonc : GBA Programming in rot13 ((y)&255) | (((mode)&3)<<8) | (((bld)&1)<<10) | (((win)&1)<<11) \ | (((mos)&1)<<12) | (((bpp)&8)<<10) | (((shape)&3)<<14) \ ) // Attribute 1, regular sprites #define ATTR1_BUILD_R(x, size, hflip, vflip) \ ( ((x)&511) | (((hflip)&1)<<12) | (((vflip)&1)<<13) | (((size)&3)<<14) ) // Attribute 1, affine sprites #define ATTR1_BUILD_A(x, size, aff_id) \ ( ((x)&511) | (((aff_id)&31)<<9) | (((size)&3)<<14) ) Attribute 2 #define ATTR2_BUILD(id, pbank, prio) \ ( ((id)&0x3FF) | (((pbank)&15)<<12) | (((prio)&3)<<10) ) I n s t e a do fd o i n gORr i n gt h eb i t f l a g st o g e t h e ry o u r s e l f , y o uc a nu s et h e s ea n dp e r h a p ss a v es o met y p i n g .Th eo r d e ro fa r g u me n t sma y b e a n n o y i n gt or e me mb e rf o rs o me , a n dt h ea mo u n to fs a f e t yc h e c k i n gma yb eab i te x c e s s i v e( g e e , y at h i n k ? ! ? ) , b u ti ft h en u mb e r sy o u g i v et h e ma r ec o n s t a n t st h eo p e r a t i o n sa r ed o n ea tc o mp i l et i mes ot h a t ' so k a y , a n ds o me t i me st h e yr e a l l yc a nb eh e l p f u l .Orn o t:P. Li k eIs a i d , I ' mn o tf o r c i n gy o ut ou s et h e m;i fy o ut h i n kt h e y ' r ewr e t c h e dp i e c e so fc o d e( a n dIa d mi tt h e ya r e )a n dd o n ' twa n tt ot a i n t y o u rp r o g r a mwi t ht h e m, t h a t ' sf i n e . No t et h a twi t ht h ee x c e p t i o no fbpp, t h ea r g u me n t sa r ea l l s h i f t e db yt h ema c r o s , me a n i n gt h a ty o us h o u l dnot u s et h e#d e f i n ef l a g s f r o mt h el i s t s , j u s ts ma l l v a l u e sl i k ey o u ' du s ei ft h e ywe r es e p a r a t ev a r i a b l e sr a t h e rt h a nb i t si nav a r i a b l e . 8.6. Demo time No w, t oa c t u a l l yu s et h eb l o o d yt h i n g s .Th ec o d eb e l o wi sp a r to ft h eobj_demo.I ti st h emo s tc o mp l e xI ' v es h o wny e t , b u ti fy o ut a k e i to n es t e pa tat i mey o u ' l l b ef i n e .Es s e n t i a l l y , t h i sd e mop l a c e st h et i l e so fab o x e dme t r o i di nt h eVRAM a l l o t t e df o ro b j e c t sa n dt h e n l e t sy o us c r e wa r o u n dwi t hv a r i o u sOBJ _ATTRb i t sl i k ep o s i t i o na n df l i p p i n gf l a g s .Th ec o n t r o l sa r ea sf o l l o ws : v e st h es p r i t e .No t et h a ti fy o umo v ef a re n o u g ho f f s c r e e n , i t ' l l c o meu po nt h eo t h e rs i d e . D-pad Mo i p st h es p r i t eh o r i z o n t a l l yo rv e r t i c a l l y , r e s p e c t i v e l y . A,B Fl k ei tg l o w.We l l , ma k e si tp a l e t t e s wa p , a c t u a l l y .Ha n d yf o rd a ma g e f l a s h i n g . Select Ma g g l e sb e t we e n1 Da n d2 Dma p p i n gmo d e s .Fi g8 . 2 ba n df i g8 . 2 cs h o u l de x p l a i nwh a th a p p e n s .Si n c et h es p r i t ei si n1 Dmo d e , Start To t h e r e ' sr e a l l yn o tmu c ht os e ewh e ny o us wi t c ht o2 Dma p p i n g , b u tIh a daf e wb u t t o n st os p a r e , s oIt h o u g h twh yn o t . c r e a s e so ri n c r e a s et h es t a r t i n gt i l e , r e s p e c t i v e l y .Ag a i n , Ih a daf e wk e y st os p a r e . L,R De // Excerpt from toolbox.h void oam_init(OBJ_ATTR *obj, uint count); void oam_copy(OBJ_ATTR *dst, const OBJ_ATTR *src, uint count); INLINE OBJ_ATTR *obj_set_attr(OBJ_ATTR *obj, u16 a0, u16 a1, u16 a2); INLINE void obj_set_pos(OBJ_ATTR *obj, int x, int y); INLINE void obj_hide(OBJ_ATTR *oatr); INLINE void obj_unhide(OBJ_ATTR *obj, u16 mode); // === INLINES ======================================================== //! Set the attributes of an object. INLINE OBJ_ATTR *obj_set_attr(OBJ_ATTR *obj, u16 a0, u16 a1, u16 a2) { obj->attr0= a0; obj->attr1= a1; obj->attr2= a2; return obj; } //! Set the position of \a obj INLINE void obj_set_pos(OBJ_ATTR *obj, int x, int y) { BF_SET(obj->attr0, y, ATTR0_Y); BF_SET(obj->attr1, x, ATTR1_X); } //! Hide an object. INLINE void obj_hide(OBJ_ATTR *obj) { BF_SET2(obj->attr0, ATTR0_HIDE, ATTR0_MODE); //! Unhide an object. INLINE void obj_unhide(OBJ_ATTR *obj, u16 mode) { BF_SET2(obj->attr0, mode, ATTR0_MODE); file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm } } 73/331 28-03-13 Tonc : GBA Programming in rot13 // toolbox.c void oam_init(OBJ_ATTR *obj, uint count) { u32 nn= count; u32 *dst= (u32*)obj; // Hide each object while(nn--) { *dst++= ATTR0_HIDE; *dst++= 0; } // init oam oam_copy(oam_mem, obj, count); } void oam_copy(OBJ_ATTR *dst, const OBJ_ATTR *src, uint count) { // NOTE: while struct-copying is the Right Thing to do here, // there's a strange bug in DKP that sometimes makes it not work // If you see problems, just use the word-copy version. #if 1 while(count--) *dst++ = *src++; #else u32 *dstw= (u32*)dst, *srcw= (u32*)src; while(count--) { *dstw++ = *srcw++; *dstw++ = *srcw++; } #endif } Th i si st h eb a s i cu t i l i t yc o d ef o rt h ed e mo , a n dc o n t a i n smo s to ft h et h i n g sy o u ' da c t u a l l yl i k et oh a v ef u n c t i o n sf o r .No t et h a tt h ei n l i n e f u n c t i o n sma k eg o o du s eo ft h eb i t f i e l dma c r o ss h o wne a r l i e r ;i fIh a d n ' td o n et h a t , t h ec o d ewo u l db eag o o dd e a l l o n g e r . An o t h e rp o i n tt h a tIn e e dt oma k ei st h a ti fI ' dp u te v e r y t h i n gi n t otoolbox.h, t h ef i l ewo u l db ep r e t t yb i g , a r o u n d7 0 0l i n e so rs o . An dwi t hf u t u r ed e mo s , i t ' db eal o tl o n g e r .Wi t ht h a ti nmi n d , I ' v es t a r t e dr e d i s t r i b u t i n gt h ec o n t e n t sal i t t l e : a l l t h et y p e sg oi ntypes.h, e v e r y t h i n gt od owi t ht h eme mo r yma pg o e si n t omemmap.h, a l l t h er e g i s t e rd e f i n e sg oi n t omemdef.ha n dt h ei n p u ti n l i n e sa n d ma c r o sc a nb ef o u n di ninput.h.Th er e s ti ss t i l l i ntoolbox.h, b u twi l l f i n dt h e ms e l v e sr e d i s t r i b u t e di nt h ee n da swe l l . Th et wof u n c t i o n si ntoolbox.cn e e ds o memo r ec l a r i f i c a t i o na swe l l Ig u e s s .I noam_init()Ic a s tt h eo b j e c t st oawo r d p o i n t e ra n du s et h a tf o rs e t t i n gt h i n g s ;a g a i n , t h i si ss i mp l yb e c a u s ei t ' sal o tf a s t e r .Be c a u s ei tma yb eu s e dt oi n i t i a l i s es o me t h i n go t h e r t h a nt h er e a l OAM, Ic o p yt h ei n i t i a l i z e db u f f e rt oOAM j u s ti nc a s e . Th eo t h e rp o i n tc o n c e r n ss o me t h i n go fav e r ys p e c i f i cb u gi nt h eo p t i mi z e ro ft h ec u r r e n tc o mp i l e r( DKPr 1 9 b ) .Ie x p e c tt h i st ob e f i x e di nal a t e ra d d i t i o na n dt h eb a s i cv e r s i o nh e r eshould wo r k , b u tj u s ti nc a s ei ti s n ' t , s e tt h e#i fe x p r e s s i o nt o0i fy o us e eOAM g e t c o r r u p t e d .I fy o umu s tk n o w, t h ep r o b l e ms e e mst ob es t r u c t c o p y i n go fOBJ _ATTRsi naf o r l o o p .Ye s , i t ' st h a ts p e c i f i c .Ev e nt h o u g h s t r u c t c o p y i n gi sl e g a l a n df a s ti ft h e y ' r ewo r da l i g n e d , i ts e e msGCCg e t sc o n f u s e dwi t h8 b y t eb l o c k si nl o o p sa n du s e smemcpy()f o r e a c hs t r u c ta n y wa y , s o me t h i n gt h a two u l d n ' two r ko nOAM.Ohwe l l . #include <string.h> #include "toolbox.h" #include "metr.h" OBJ_ATTR obj_buffer[128]; OBJ_AFFINE *obj_aff_buffer= (OBJ_AFFINE*)obj_buffer; void obj_test() { int x= 96, y= 32; u32 tid= 0, pb= 0; // (3) tile id, pal-bank OBJ_ATTR *metr= &obj_buffer[0]; obj_set_attr(metr, ATTR0_SQUARE, file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm // Square, regular sprite 74/331 28-03-13 Tonc : GBA Programming in rot13 ATTR1_SIZE_64, ATTR2_PALBANK(pb) | tid); // 64x64p, // palbank 0, tile 0 // (4) position sprite (redundant here; the _real_ position // is set further down obj_set_pos(metr, x, y); while(1) { vid_vsync(); key_poll(); // (5) Do various interesting things // move left/right x += 2*key_tri_horz(); // move up/down y += 2*key_tri_vert(); // increment/decrement starting tile with R/L tid += bit_tribool(key_hit(-1), KI_R, KI_L); // flip if(key_hit(KEY_A)) // horizontally metr->attr1 ^= ATTR1_HFLIP; if(key_hit(KEY_B)) // vertically metr->attr1 ^= ATTR1_VFLIP; // make it glow (via palette swapping) pb= key_is_down(KEY_SELECT) ? 1 : 0; // toggle mapping mode if(key_hit(KEY_START)) REG_DISPCNT ^= DCNT_OBJ_1D; // Hey look, it's one of them build macros! metr->attr2= ATTR2_BUILD(tid, pb, 0); obj_set_pos(metr, x, y); oam_copy(oam_mem, obj_buffer, 1); // (6) Update OAM (only one now) } } int main() { // (1) Places the tiles of a 4bpp boxed metroid sprite // into LOW obj memory (cbb == 4) memcpy(&tile_mem[4][0], metr_boxTiles, metr_boxTilesLen); memcpy(pal_obj_mem, metrPal, metrPalLen); // (2) Initialize all sprites oam_init(obj_buffer, 128); REG_DISPCNT= DCNT_OBJ | DCNT_OBJ_1D; obj_test(); while(1); return 0; } 8.6.1. Setting up sprites Be f o r ea n ys p r i t e ss h o wu p , t h e r ea r et h r e et h i n g sy o uh a v et od o , a l t h o u g hn o tn e c e s s a r i l yi nt h i so r d e r .Th e ya r e : c o p y i n gs p r i t e g r a p h i c st oVRAM, s e t t i n gu pOAM t ou s et h e s eg r a p h i c s , a n de n a b l i n gs p r i t e si nt h ed i s p l a yc o n t r o l , REG_DISPCNT. Display control St a r t i n gwi t ht h el a s to n e , y o ue n a b l es p r i t e sb ys e t t i n gb i t1 2o fREG_DISPCNT.Us u a l l yy o u ' l l a l s owa n tt ou s e1 Dma p p i n g , s os e tb i t 6a swe l l .Th i si sd o n ea tpoint (2) o ft h ec o d e . Hiding all sprites Th eo t h e rs t e pp e r f o r me dh e r ei sac a l l t ooam_init().Th i si s n ' ts t r i c t l yn e c e s s a r y , b u tag o o di d e an o n e t h e l e s s .Wh a t file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 75/331 28-03-13 Tonc : GBA Programming in rot13 oam_init()d o e si sh i d ea l l t h es p r i t e s .Wh yi st h i sag o o di d e a ?We l l , b e c a u s eaf u l l yz e r o e do u tOAM d o e snot me a nt h es p r i t e s a r ei n v i s i b l e .I fy o uc h e c kt h ea t t r i b u t e sy o u ' l l s e et h a tt h i swi l l me a nt h a tt h e y ' r ea l l 8 x 8p i x e l s p r i t e s , u s i n gt i l e0f o rt h e i rg r a p h i c s , l o c a t e da t( 0 , 0 ) .I ft h ef i r s tt i l ei s n ' te mp t y , y o u ' l l s t a r twi t h1 2 8v e r s i o n so ft h a tt i l ei nt h et o p l e f tc o r n e r , wh i c hl o o k sr a t h e rs t r a n g e .So , ma k es u r et h e y ' r ea l l i n v i s i b l ef i r s t .Th ed e moa l s oc o me swi t hobj_hide()a n dobj_unhide()f u n c t i o n s , a l t h o u g ht h e ya r e n ' t u s e dh e r e . Loading sprite graphics Th ef i r s tt h i n gt od o( point (1))i st os t o r et h es p r i t eg r a p h i c si no b j e c tVRAM.AsI ' v ea l r e a d ys a i daf e wt i me sn o w, t h e s eg r a p h i c s s h o u l db es t o r e da s8 x 8t i l e s , n o ta saf l a tb i t ma p .Fo re x a mp l e , mys p r i t eh e r ei s6 4 x 6 4 pi ns i z e , s ot os t o r ei tI ' v eh a dt oc o n v e r tt h i st o 8 x 8s e p a r a t et i l e sf i r s t .I fy o ud onot d ot h i s , y o u rs p r i t e swi l l l o o kv e r ys t r a n g ei n d e e d . Ex a c t l ywh e r ey o up u tt h e s et i l e si sa c t u a l l yn o ta l l t h a tr e l e v a n t( a p a r tf r o mt h eo b v i o u s , l i k ema p p i n gmo d e , a n dt i l ea l i g n me n t , o f c o u r s e ) .Ob j e c tVRAM wo r k sa sat e x t u r e p o o l a n dh a sn o t h i n gt od owi t ht h es c r e e nd i r e c t l y .Yo us t o r et h et i l e st h a ty o uwa n tt ob e a v a i l a b l et h e r e , a n di ti sb yma n i p u l a t i n gt h eOAM a t t r i b u t e st h a tt h es y s t e mk n o wswh i c ht i l e sy o uwa n tt ou s ea n dwh e r ey o uwa n t t h e m.Th e r ei sn or e a s o nwh ys p r i t e0c o u l d n ' ts t a r ta tt i l e4 2 , o rwh ymu l t i p l es p r i t e sc o u l d n ' tu s et h es a met i l e s .Th i si sa l s owh y OAMData, wh i c hi ss o me t i me su s e df o ro b j e c tVRAM, i ss u c hami s n o me r : o b j e c tVRAM h a sn o t h i n gt od owi t hOAM.Nothing!I f y o u rh e a d e r su s et h i sn a mef o r0601:0000, o re v e n0601:4000, c h a n g ei t .Pl e a s e .An db ec a r e f u l wh e r ey o up u tt h i n g si nt h e b i t ma pmo d e s , a sy o uc a n ' tu s et i l e s0 5 1 2t h e r e . AsIs a i d , l o a d i n gt h es p r i t e sh a p p e n sa tpoint (1) i nt h ec o d e .I fy o up a i da t t e n t i o nt ot h eo v e r v i e w, y o u ' l l r e me mb e rt h a t tile_mem[][]i sat wod i me n s i o n a l a r r a y , ma p p i n gc h a r b l o c k sa n d4 b i tt i l e s .Yo u ' l l a l s or e me mb e rt h a to b j e c tVRAM i sc h a r b l o c k s 4a n d5 , s o&tile_mem[4][0]p o i n t st ot h ef i r s tt i l ei no b j e c tVRAM.SoI ' ml o a d i n gmyb o x e dme t r o i di n t ot h ef i r s t6 4t i l e so f o b j e c tVRAM. Ia ma l s ol o a d i n gi t sp a l e t t ei n t ot h es p r i t ep a l e t t e .Th a t ' ssprite p a l e t t e( 0500:0200) , n o tb a c k g r o u n dp a l e t t e .Lo a di tt ot h ewr o n g p l a c ea n dy o uwo n ' ts e ea n y t h i n g . Finding tile addresses Us etile_memo rama c r ot of i n dt h ea d d r e s s e st oc o p yy o u rt i l e st o o , i t ' smu c hmo r er e a d a b l ea n dma i n t a i n a b l et h a n c a l c u l a t i n gt h e mma n u a l l y .Yo us h o u l dn o th a v ea n yh a r d c o d e dVRAM a d d r e s s e si ny o u rc o d e , e v e r . OAMData He a d e r sf r o mo t h e rs i t e ss o me t i me s#d e f i n e‘ OAMData’a sp a r to fVRAM.I ti sn o t .Re n a mei t . Setting attributes La s t l y , I ' l l s e tu po n eOBJ _ATTRs ot h a ti ta c t u a l l yu s e st h eme t r o i dt i l e s .Th i si sd o n ea tpoint (3), u s i n gt h eobj_set_attr() i n l i n ef u n c t i o n .Al l i td o e si st h r e ea s s i g n me n t st ot h ea t t r i b u t e so ft h ef i r s ta r g u me n t , b yt h ewa y , n o t h i n gs p e c t a c u l a r .Th i sj u s ts a v e s t y p i n gd o i n gi tt h i swa yr a t h e rt h a nt h r e es e p a r a t es t a t e me n t s .Wi t ht h i sp a r t i c u l a rc a l l , It e l l t h i ss p r i t et h a ti t ' sa6 4 x 6 4p i x e l ( 8 x 8t i l e ) s p r i t e , a n di t ss t a r t i n gt i l ei stid, wh i c hi s0 .Th i sme a n st h a ti t ' l l u s et h e6 4t i l e s , s t a r t i n ga tt i l e0 . No t et h a tt h es p r i t eI ' ms e t t i n gi sa c t u a l l yp a r to ft h eOAM b u f f e r , n o tt h er e a l OAM.Th i sme a n st h a te v e na f t e rIs e tt h ea t t r i b u t e s t h e r e , n o t h i n gh a p p e n sy e t .Tof i n a l i z et h es p r i t eIn e e dt ou p d a t et h ereal OAM, wh i c hi sd o n eb yac a l l t ooam_copy()( point (6)) . Th i sc a r r i e st woa r g u me n t s : a ni n d e xa n dac o u n td e n o t i n gh o wma n ys p r i t e st ou p d a t e , a n dwh i c hs p r i t et os t a r ta t .Ia l s oh a v e obj_copy(), wh i c ho n l yc o p i e sa t t r i b u t e s0 , 1a n d2 , b u tnot 3 !Th i si sn e c e s s a r ywh e ny o us t a r tu s i n ga f f i n es p r i t e s , wh i c hma yb e c o p i e di n c o r r e c t l yo t h e r wi s e . Th ep r e v i o u ss t e p sa r ee n o u g ht og e tt h eme t r o i ds p r i t eo n s c r e e n .Th es t o r yd o e s n ' te n dt h e r e , o fc o u r s e .He r ea r eaf e wt h i n g st h a t y o uc a nd owi t hs p r i t e s . 8.6.2. Sprite positioning Th ef i r s to r d e ro fb u s i n e s si su s u a l l yt op l a c ei ta ts o mep o s i t i o no ns c r e e n , o re v e no f fs c r e e n .Tod ot h i sy o uh a v et ou p d a t et h eb i t sf o r t h ey a n dx p o s i t i o n si na t t r i b u t e s0a n d1 , r e s p e c t i v e l y .On emi s t a k eIo f t e ns e e mt oma k ei sf i l l xi n t oa t t r 0a n dy i n t oa t t r 1 , wh e ni t s h o u l db et h eo t h e rwa ya r o u n d .I fy o u rs p r i t emo v e ss t r a n g e l y , t h i smi g h tb ewh y . No t et h a tt h e s ec o o r d i n a t e sma r kt h etop-left o ft h es p r i t e .Al s o , t h en u mb e ro fb i t sf o rt h ec o o r d i n a t e sme a n sweh a v e5 1 2p o s s i b l e xv a l u e sa n d2 5 6yv a l u e s .Th ec o o r d i n a t er a n g e swr a pa r o u n d , s oy o uc o u l da l s os a yt h a tt h e s ea r es i g n e di n t e g e r s , wi t ht h er a n g e sx ∈ [ 2 5 6 , 2 5 5 ]a n dy ∈ [ 1 2 8 , 1 2 7 ] .Ye s , t h a two u l dma k et h eh i g h e s tyv a l u es ma l l e rt h a nt h eh e i g h to ft h es c r e e n , b u tt h a n k st ot h e wr a p p i n gi ta l l wo r k so u t .We l l , a l mo s t .An y wa y , t h a n k st ot h e2 s c o mp l e me n tn a t u r eo fi n t e g e r s , s i mp l yma s k i n gt h ex a n dy v a l u e sb y 0x01FFa n d0x00FF, r e s p e c t i v e l y , wi l l g i v ep r o p e r9a n d8b i ts i g n e dv a l u e s .Yo uc a nd ot h i sma n u a l l y , o ru s et h eobj_set_pos() f u n c t i o nu s e da tpoint (4). Yo umi g h ts e ec o d et h a tc l e a r st h el o we rb i t so ft h ea t t r i b u t e sa n dt h e nd i r e c t l yORRsi nx a n dy.Th i si sn o tag o o di d e a , b e c a u s e n e g a t i v ev a l u e sa r ea c t u a l l yr e p r e s e n t e db yu p p e rh a l fo fad a t a t y p e ' sr a n g e .−1f o re x a mp l ei sa l l b i t ss e t( 0 x FFFFFFFF) .Wi t h o u t ma s k i n go f ft h eh i g h e rb i t s , n e g a t i v ev a l u e swo u l do v e r wr i t et h er e s to ft h ea t t r i b u t eb i t s , wh i c hwo u l db eb a d . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 76/331 28-03-13 Tonc : GBA Programming in rot13 Mask your coordinates I fy o u ' r ema k i n gas p r i t ep o s i t i o n i n gf u n c t i o no ru s es o me o n ee l s e ' smake sure y o uma s ko f ft h eb i t si nx a n dy b e f o r ey o u i n s e r tt h e mi n t ot h ea t t r i b u t e s .I fn o t , n e g a t i v ev a l u e swi l l o v e r wr i t et h ewh o l ea t t r i b u t e . Th i si sb a d obj->attr0= (obj->attr0 &~ 0x00FF) | (y); Th i si sg o o d : obj->attr0= (obj->attr0 &~ 0x00FF) | (y & 0x00FF); Position variables and using tribools I n s t e a do fu s i n ga nOBJ _ATTRt os t o r et h es p r i t e ' sp o s i t i o n , i ti sb e t t e rt ok e e pt h e mi ns e p a r a t ev a r i a b l e s , i nt h i sc a s exa n dy.Th i s a v o i d sh a v i n gt oma s kc o o r d i n a t ef i e l d sa l l t h et i me , b u tmo r ei mp o r t a n t l y , t h ep o s i t i o n sc a ne x t e n db e y o n dt h es i z eo ft h es c r e e n .As mo s tg a mewo r l d sa r e n ' tr e s t r i c t e dt oas i n g l es c r e e n , t h i si sa ni mp o r t a n tp o i n t .Th e n , wh e nt h et i mei sr i g h t , t h e s ea r ef e dt o oam_set_pos()t ou p d a t et h es p r i t e . Al s o , n o t et h eu s eo fmyt r i b o o l k e yf u n c t i o n st ou p d a t et h ep o s i t i o n s .I n p u tp r o c e s s i n go f t e nf o l l o wsap a t t e r no f“ k e yXp r e s s e d : i n c r e me n t , k e yo p p o s i t eo fYp r e s s e d , d e c r e me n t ”Th et r i b o o l f u n c t i o n sb r i n gt h a tk i n do fc o d ed o wnf r o mf o u rl i n e st oo n e , wh i c h ma k e st h ec o d ee a s i e rt or e a d( o n c ey o ug e to v e rt h ei n i t i a l h u r d l e ) .Fo re x a mp l e , key_tri_horz()r e t u r n s+1i f‘ r i g h t ’i sp r e s s e d , −1i f‘ l e f t ’i sp r e s s e d , a n d0i fn e i t h e ro rb o t ha r ep r e s s e d .key_tri_vert()d o e ss o me t h i n gs i mi l a rf o rv e r t i c a l mo v e me n ta n dt h e l i n ewi t hbit_tribool()f u n c t i o nma k e sav a r i a n tu s i n gkey_hit()a n dRa n dLt oi n c r e me n to rd e c r e me n tt h et i l ei n d e x . 8.6.3. Other attrs Sp r i t ec o o r d i n a t e sa r eo n l yt woo ft h ema n ys p r i t ea t t r i b u t e st h a tc a nb ec o n t r o l l e dwi t hv i as p e c i f i cOAM b i t s , e v e nwh i l et h es p r i t ei s a l r e a d ya c t i v e .So meo ft h eo b v i o u so n e sa r ef l i p p i n go rmi r r o r i n gi t , wh i c hc a nb ed o n eu s i n gAa n dBh e r e .Or , i fy o u ' r eu s i n ga4 b p p s p r i t e , y o uc a ns wa pp a l e t t e ss ot h a ta l l t h ec o l o r sc h a n g e .Pr e s s i n gSe l e c ti nt h ed e mos wi t c h e sf r o mp a l e t t eb a n k0t o1 , wh i c hh a p p e n s t oh a v eag r e yt owh i t eg r a d i e n t .To g g l i n gb e t we e nt h e s ep a l e t t eb a n k sq u i c k l yc a nma k et h es p r i t ef l a s h .Yo uc o u l da l s oc h a n g et h e p r i o r i t i e si nwh i c ht h es p r i t e sa r er e n d e r e d , o rt o g g l ea l p h ab l e n d i n g , a l t h o u g hIh a v e n ' td o n et h o s et h i n g sh e r e . No w, t h e s et h i n g sd o n ' tr e a l l yc h a n g et h eo v e r a l l i ma g eo ft h es p r i t e .Wh a ty o us h o u l dr e a l i z et h o u g hi st h a ti tis p o s s i b l et od ot h a t . AsI ' v ea l r e a d yn o t e db e f o r e , i t ' sn o tt r u et h a tt h ec o n t e n t so fVRAM are t h es p r i t e , r a t h e rt h a tas p r i t euses p a r t so fVRAM t os h o w s o me t h i n g , a n y t h i n g , o ns c r e e n .Yo uc o u l d , f o re x a mp l e , c h a n g et h es t a r t i n gt i l etidt h a tt h es p r i t eu s e s , wh i c hi nt h i sc a s ec a nb ed o n e u s i n gLa n dR.No to n l yi st h i sl e g a l , i t ' st h es t a n d a r dp r a c t i c ef o ra n i ma t i o n( a l t h o u g hy o uc a na l s oo v e r wr i t eVRAM f o rt h a t–r e s e t t i n g t h et i l ei n d e xi sj u s tf a s t e r ) .Un d e r s t a n d i n gt h i si so n eo ft h ep o i n t so fmo v i n gf r o mau s e rt oad e v e l o p e rp e r s p e c t i v e : t h eu s e ro n l ys e e s t h es u r f a c e ;t h ec o d e rl o o k sb e l o wi ta n ds e e swh a t ' sr e a l l yg o i n go n . An dt h a t ' si tf o rr e g u l a rs p r i t e s .Us i n gmu l t i p l es p r i t e si s n ' tmu c hd i f f e r e n t–s e e no n e , s e e nt h e ma l l .Ba s i ca n i ma t i o ns h o u l d n ' tb e p r o b l e ma t i ce i t h e r , u n t i l y o ur u no u to fVRAM t op u tt h e mi n .Th e r ea r es t i l l af e wr e g i o n sl e f tu n t o u c h e dl i k eb l e n d i n ga n dmo s a i c , b u t I ' l l d e a l wi t ht h o s el a t e r . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 77/331 28-03-13 Tonc : GBA Programming in rot13 9. Regular tiled backgrounds Ti l e ma pi n t r o d u c t i o n . Ba c k g r o u n dc o n t r o l . Re g u l a rb a c k g r o u n dt i l e ma p s . Ti l e ma pd e mo s . I nc o n c l u s i o n . 9.1. Tilemap introduction Ti l e ma p sa r et h eb r e a da n db u t t e rf o rt h eGBA.Al mo s te v e r yc o mme r c i a l GBAg a mema k e su s eo ft i l e mo d e s , wi t ht h eb i t ma pmo d e s s e e no n l yi n3 Dl i k eg a me st h a tu s er a y t r a c i n g .Ev e r y t h i n ge l s eu s e st i l e dg r a p h i c s . Th er e a s o nwh yt i l e ma p sa r es op o p u l a ri st h a tt h e y ' r ei mp l e me n t e di nh a r d wa r ea n dr e q u i r el e s ss p a c et h a nb i t ma pg r a p h i c s . Co n s i d e rf i g9 . 1 a .Th i si sa5 1 2b y2 5 6i ma g e , wh i c he v e na t8 b p pwo u l dt a k eu p1 3 1k Bo fVRAM, a n dwes i mp l yd o n ' th a v et h a t .I f y o uwe r et oma k eo n eb i gb i t ma po fan o r ma l l e v e l i nag a me , y o uc a ne a s i l yg e tu pt o1 0 0 0 ×1 0 0 0p i x e l s , wh i c hi sj u s tn o tp r a c t i c a l .An d then t h e r e ' st h ema t t e ro fs c r o l l i n gt h r o u g ht h el e v e l , wh i c hme a n su p d a t i n ga l l p i x e l se a c hf r a me .Ev e nwh e ny o u rs c r o l l i n gc o d ei sf u l l y o p t i mi z e dt h a t ' dt a k eq u i t eab i to ft i me . No w, n o t i c et h a tt h e r ea r ema n yr e p e a t e de l e me n t si nt h i si ma g e .Th eb i t ma ps e e mst ob ed i v i d e di n t og r o u p so f1 6 ×1 6p i x e l s .Th e s e a r et h etiles.Th el i s to fu n i q u et i l e si st h etileset, wh i c hi sg i v e ni nf i g9 . 1 b .Asy o uc a ns e e , t h e r ea r eo n l y1 6u n i q u et i l e sma k i n gu pt h e i ma g e .Toc r e a t et h ei ma g ef r o mt h e s et i l e s , wen e e datilemap.Th ei ma g ei sd i v i d e di n t oama t r i xo ft i l e s .Ea c he l e me n ti nt h ema t r i x h a satile index wh i c hi n d i c a t e swh i c ht i l es h o u l db er e n d e r e dt h e r e ;t h et i l e ma pc a nb es e e ni nf i g9 . 1 c . Su p p o s eb o t ht h et i l e s e ta n dma pu s e d8 b i te n t r i e s , t h es i z e sa r e1 6 ×( 1 6 ×1 6 )=4 0 9 6b y t e sf o rt h et i l e s e ta n d3 2 ×1 6=5 1 2b y t e sf o r t h et i l e ma p .Sot h a t ' s4 . 6k Bf o rt h ewh o l es c e n er a t h e rt h a nt h e1 3 1k Bweh a db e f o r e ;as i z er e d u c t i o no faf a c t o r2 8 . Fig 9.1a: i ma g eo ns c r e e n . Th et i l ema p p i n gp r o c e s s .Us i n gt h et i l e s e to ff i g9 . 1 b , a n dt h et i l ema po ff i g9 . 1 c , t h ee n d r e s u l ti s f i g9 . 1 a . Fig 9.1b: Fig 9.1c: t h et i l ema p( wi t ht h epr ope rt i l e sa sab a c k d r o p) . t h et i l e s e t . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 78/331 28-03-13 Tonc : GBA Programming in rot13 Th a t ' sb a s i c a l l yh o wt i l e ma p swo r k .Yo ud o n ' td e f i n et h ewh o l ei ma g e , b u tg r o u pp i x e l st o g e t h e ri n t ot i l e sa n dd e s c r i b et h ei ma g ei n t e r mso ft h o s eg r o u p s .I nt h ef i g9 . 1 , t h et i l e swe r e1 6 ×1 6p i x e l s , s ot h et i l e ma pi s2 5 6t i me ss ma l l e rt h a nt h eb i t ma p .Th eu n i q u et i l e sa r e i nt h et i l e s e t , wh i c hc a n( a n du s u a l l ywi l l )b el a r g e rt h a nt h et i l e ma p .Th es i z eo ft h et i l e s e tc a nv a r y : i ft h eb i t ma pi sh i g h l yv a r i a b l e , y o u ' l l p r o b a b l yh a v ema n yu n i q u et i l e s ;i ft h eg r a p h i c sa r en i c e l ya l i g n e dt ot i l eb o u n d a r i e sa l r e a d y( a si ti sh e r e ) , t h et i l e s e twi l l b es ma l l .Th i si s wh yt i l e e n g i n e so f t e nh a v ead i s t i n c tl o o kt ot h e m. [[ref:ssec-intro-gba] Tilemaps for the GBA I nt h et i l e dv i d e o mo d e s( 0 , 1a n d2 )y o uc a nh a v eu pt of o u rb a c k g r o u n d st h a td i s p l a yt i l e ma p s .Th es i z eo ft h ema p si ss e tb yt h e c o n t r o l r e g i s t e r sa n dc a nb eb e t we e n1 2 8 ×1 2 8a n d1 0 2 4 ×1 0 2 4p i x e l s .Th es i z eo fe a c ht i l ei sa l wa y s8 ×8p i x e l s , s of i g9 . 1i s n ' tquite t h e wa yi t ' dwo r ko nt h eGBA.Be c a u s ea c c e s s i n gt h et i l e ma p si sd o n ei nu n i t so ft i l e s , t h ema ps i z e sc o r r e s p o n dt o1 6 ×1 6t o1 2 8 ×1 2 8t i l e s . Bo t ht h et i l e sa n dt i l e ma p sa r es t o r e di nVRAM, wh i c hi sd i v i d e di n t ocharblocks a n dscreenblocks.Th et i l e s e ti ss t o r e di nt h e c h a r b l o c k sa n dt h et i l e ma pg o e si n t ot h es c r e e n b l o c k s .I nt h ec o mmo nv e r n a c u l a r , t h ewo r d“ t i l e ”i su s e df o rb o t ht h eg r a p h i c a l t i l e sa n d t h ee n t r i e so ft h et i l e ma p s .Be c a u s et h i si ss o me wh a tc o n f u s i n g , I ' l l u s et h et e r mscreen entry ( SE f o rs h o r t )a st h ei t e msi nt h e s c r e e n b l o c k s( i . e . , t h ema pe n t r i e s )a n dr e s t r i c tt i l e st ot h et i l e s e t . 6 4k Bo fVRAM i ss e ta s i d ef o rt i l e ma p s( 0600:0000h0600:FFFFh) .Th i si su s e df o rb o t hs c r e e n b l o c k sand c h a r b l o c k s .Yo u c a nc h o o s ewh i c ho n e st ou s ef r e e l yt h r o u g ht h ec o n t r o l r e g i s t e r s , b u tb ec a r e f u l t h a tt h e yc a no v e r l a p( s e et a b l e9 . 1 ) .Ea c hs c r e e n b l o c k i s2 0 4 8( 800h)b y t e sl o n g , g i v i n g3 2s c r e e n b l o c k si nt o t a l .Al l b u tt h es ma l l e s tb a c k g r o u n d su s emu l t i p l es c r e e n b l o c k sf o rt h ef u l l t i l e ma p .Ea c hc h a r b l o c ki s1 6k B( 4000hb y t e s )l o n g , g i v i n gf o u rb l o c k so v e r a l l . Memory charblock 0600:0000 0600:4000 0600:8000 0600:C000 0 screenblock 0 … 1 2 3 7 8… 1 5 1 6… 2 32 4… 3 1 Table 9.1: c h a r b l o c ka n ds c r e e n b l o c ko v e r l a p. Tiles vs ‘tiles’ Bo t ht h ee n t r i e so ft h et i l e ma pa n dt h ed a t ai nt h et i l e s e ta r eo f t e nr e f e r r e dt oa s‘ t i l e s ’ , wh i c hc a nma k ec o n v e r s a t i o n c o n f u s i n g .Ir e s e r v et h et e r m‘ t i l e ’f o rt h eg r a p h i c s , a n d‘ s c r e e n ( b l o c k )e n t r y ’o r‘ ma pe n t r y ’f o rt h ema p ' sc o n t e n t s . Charblocks vs screenblocks Ch a r b l o c k sa n ds c r e e n b l o c k su s et h es a mea d d r e s s e si nme mo r y .Wh e nl o a d i n gd a t a , ma k es u r et h et i l e st h e ms e l v e sd o n ' t o v e r wr i t et h ema p , o rv i c ev e r s a . Si z ewa so n eo ft h eb e n e f i t so fu s i n gt i l e ma p s , s p e e dwa sa n o t h e r .Th er e n d e r i n go ft i l e ma p si nd o n ei nh a r d wa r ea n di fy o u ' v ee v e r p l a y e dPCg a me si nh a r d wa r ea n ds o f t wa r emo d e s , y o u ' l l k n o wt h a th a r d wa r ei sg o o d .An o t h e rn i c ep o i n ti st h a ts c r o l l i n gi sd o n ei n h a r d wa r et o o .I n s t e a do fr e d r a wi n gt h ewh o l es c e n e , y o uj u s th a v et oe n t e rs o mec o o r d i n a t e si nt h er i g h tr e g i s t e r s . AsIs a i di nt h eo v e r v i e w, t h e r ea r et h r e es t a g e st os e t t i n gu pat i l e db a c k g r o u n d : c o n t r o l , ma p p i n ga n di ma g e d a t a .I ' v ea l r e a d yc o v e r e d mo s to ft h ei ma g e d a t ai nt h eo v e r v i e w, a swe l l a ss o meo ft h ec o n t r o l a n dma p p i n gp a r t st h a ta r es h a r e db ys p r i t e sa n db a c k g r o u n d s a l i k e ;t h i sc h a p t e rc o v e r so n l yt h i n g ss p e c i f i ct ob a c k g r o u n d si ng e n e r a l a n dr e g u l a rb a c k g r o u n d si np a r t i c u l a r .I ' ma s s u mi n gy o u ' v er e a d t h eo v e r v i e w. Essential tilemap steps Lo a dt h eg r a p h i c s : t i l e si n t oc h a r b l o c k sa n dc o l o r si nt h eb a c k g r o u n dp a l e t t e . Lo a dama pi n t oo n eo rmo r es c r e e n b l o c k s . Swi t c ht ot h er i g h tmo d ei nREG_DI SPCNTa n da c t i v a t eab a c k g r o u n d . I n i t i a l i z et h a tb a c k g r o u n d ' sc o n t r o l r e g i s t e rt ou s et h er i g h tCBB, SBBa n db i t d e p t h . 9.2. Background control 9.2.1. Background types J u s tl i k es p r i t e s , t h e r ea r et wot y p e so ft i l e db a c k g r o u n d s : r e g u l a ra n da f f i n e ;t h e s ea r ea l s ok n o wna st e x ta n dr o t a t i o nb a c k g r o u n d s , r e s p e c t i v e l y .Th et y p eo ft h eb a c k g r o u n dd e p e n d so ft h ev i d e omo d e( s e et a b l e9 . 2 ) .Att h e i rc o r e s , b o t hr e g u l a ra n da f f i n eb a c k g r o u n d s wo r kt h es a mewa y : y o uh a v et i l e s , at i l e ma pa n daf e wc o n t r o l r e g i s t e r s .Bu tt h a t ' swh e r et h es i mi l a r i t ye n d s .Af f i n eb a c k g r o u n d su s e file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 79/331 28-03-13 Tonc : GBA Programming in rot13 mo r ea n dd i f f e r e n tr e g i s t e r st h a nr e g u l a ro n e s , a n de v e nt h ema p sa r ef o r ma t t e dd i f f e r e n t l y .Th i sp a g eo n l yc o v e r st h er e g u l a r b a c k g r o u n d s .I ' l l l e a v et h ea f f i n eo n e st i l l a f t e rt h ep a g eo nt h ea f f i n ema t r i x . mode BG0 BG1 BG2 BG3 0 r e g r e g r e g r e g 1 r e g r e g a f f 2 - - - a f f a f f Table 9.2: v i d e omo d e sa n d b a c k g r o u n dt ype 9.2.2. Control registers Al l b a c k g r o u n d sh a v e3p r i ma r yc o n t r o l r e g i s t e r s .Th ep r i ma r yc o n t r o l r e g i s t e ri sREG_BGxCNT, wh e r ex i n d i c a t e st h eb a c k g r o u n d s0 t h r o u g h3 .Th i sr e g i s t e ri swh e r ey o us a ywh a tt h es i z eo ft h et i l e ma pi s , a n dwh i c hc h a r b l o c ka n ds c r e e n b l o c ki tu s e s .Th eo t h e rt woa r e t h es c r o l l i n gr e g i s t e r s , REG_BGxHOFSa n dREG_BGxVOFS. Ea c ho ft h e s ei sa1 6 i tr e g i s t e r .REG_BG0CNTc a nb ef o u n da t0400:0008, wi t ht h eo t h e rc o n t r o l sr i g h tb e h i n di t .Th eo f f s e t sa r e p a i r e db yb a c k g r o u n d , f o r mi n gc o o r d i n a t ep a i r s .Th e s es t a r ta t0400:0010 Register length address REG_BGxCNT 2 0 4 0 0 : 0 0 0 8 h+2 · x REG_BGxHOFS 2 0 4 0 0 : 0 0 1 0 h+4 · x REG_BGxVOFS 2 0 4 0 0 : 0 0 1 2 h+4 · x Table 9.3: Ba c k g r o u n dr e g i s t e ra d d r e s s e s Th ed e s c r i p t i o no fREG_BGxCNTc a nb ef o u n di nt a b l e9 . 4 .Mo s to fi ti sp r e t t ys t a n d a r d , e x c e p tf o rt h es i z e : t h e r ea r ea c t u a l l ytwo l i s t s o fp o s s i b l es i z e s ;o n ef o rr e g u l a rma p sa n do n ef o ra f f i n ema p s .Th eb o t hu s et h es a meb i t sy o uma yh a v et ob ec a r e f u l t h a ty o u ' r eu s i n g t h er i g h t#d e f i n e s . REG_BGxCNT @ 0400:0008 + 2x FE D CBA98 7 Sz Wr SBB 6 54 32 10 CM Mos - CBB Pr bits name define description 0 1 Pr BG_PRIO# t e r mi n e sd r a wi n go r d e ro fb a c k g r o u n d s . Priority.De 2 3 CBB BG_CBB# t st h ec h a r b l o c kt h a ts e r v e sa st h eb a s ef o rc h a r a c t e r / t i l e Character Base Block .Se i n d e x i n g .Va l u e s : 0 3 . 6 Mo s BG_MOSAI CMosaic f l a g .En a b l e smo s a i ce f f e c t . 7 CM BG_4 BPP, Color Mode .1 6c o l o r s( 4 b p p )i fc l e a r e d ;2 5 6c o l o r s( 8 b p p )i fs e t . BG_8 BPP 8 C SBB BG_SBB# D Wr BG_WRAP Affine Wrapping f l a g .I fs e t , a f f i n eb a c k g r o u n dwr a pa r o u n da tt h e i re d g e s .Ha sn o e f f e c to nr e g u l a rb a c k g r o u n d sa st h e ywr a pa r o u n db yd e f a u l t . EF Sz Sz-flag t st h es c r e e n b l o c kt h a ts e r v e sa st h eb a s ef o rs c r e e n e n t r y / ma p Screen Base Block .Se i n d e x i n g .Va l u e s : 0 3 1 . BG_SIZE#, Background Size .Re g u l a ra n da f f i n eb a c k g r o u n d sh a v ed i f f e r e n ts i z e sa v a i l a b l et o see below t h e m.Th es i z e s , i nt i l e sa n di np i x e l s , c a nb ef o u n di nt a b l e9 . 5 . define (tiles) (pixels) Sz-flag define (tiles) (pixels) 0 0 BG_REG_3 2 x 3 23 2 x 3 22 5 6 x 2 5 6 0 0 BG_AFF_1 6 x 1 6 1 6 x 1 6 1 2 8 x 1 2 8 0 1 BG_REG_6 4 x 3 26 4 x 3 25 1 2 x 2 5 6 0 1 BG_AFF_3 2 x 3 2 3 2 x 3 2 2 5 6 x 2 5 6 1 0 BG_REG_3 2 x 6 43 2 x 6 42 5 6 x 5 1 2 1 0 BG_AFF_6 4 x 6 4 6 4 x 6 4 5 1 2 x 5 1 2 1 1 BG_REG_6 4 x 6 46 4 x 6 45 1 2 x 5 1 2 Table 9.5a: r e g u l a rb gs i z e s 1 1 BG_AFF_1 2 8 x 1 2 81 2 8 x 1 2 81 0 2 4 x 1 0 2 4 Table 9.5b: a f f i n eb gs i z e s Ea c hb a c k g r o u n dh a st wo1 6 b i ts c r o l l i n gr e g i s t e r st oo f f s e tt h er e n d e r i n g( REG_BGxHOFSa n dREG_BGxVOFS) .Th e r ea r ean u mb e r o fi n t e r e s t i n gp o i n t sa b o u tt h e s e .Fi r s t , b e c a u s er e g u l a rb a c k g r o u n d swr a pa r o u n d , t h ev a l u e sa r ee s s e n t i a l l ymo d u l omapsize.Th i si sn o t r e a l l yr e l e v a n ta tt h emo me n t , b u ty o uc a nu s et h i st oy o u rb e n e f i to n c ey o ug e tt omo r ea d v a n c e dt i l e ma p s .Se c o n d , t h e s er e g i s t e r sa r e write-only!Th i si sal i t t l ea n n o y i n g , a si tme a n st h a ty o uc a n ' tu p d a t et h ep o s i t i o nb ys i mp l yd o i n gR̀EG_BG0HOFS++' a n dt h el i k e . An dn o wt h et h i r dp a r t , wh i c hma yb et h emo s ti mp o r t a n t , n a me l ywh a tt h ev a l u e sa c t u a l l ydo.Th es i mp l e s twa yo fl o o k i n ga tt h e m i st h a tt h e yg i v et h ec o o r d i n a t e so ft h es c r e e no nt h ema p .Re a dt h a ta g a i n , c a r e f u l l y : i t ' st h ep o s i t i o no ft h es c r e e no nt h ema p .I ti snot t h ep o s i t i o no ft h ema po nt h es c r e e n , wh i c hi sh o ws p r i t e swo r k .Th ed i f f e r e n c ei so n l yami n u ss i g n , b u te v e ns o me t h i n ga ss ma l l a sa file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 80/331 28-03-13 Tonc : GBA Programming in rot13 s i g nc h a n g ec a nwr e a kh a v o co ny o u rc a l c u l a t i o n s . Fig 9.2a: S c r o l l i n go f f s e tdx s e t si st h epo s i t i o no ft h es c r e e no nt h ema p. I nt h i sc a s e , dx =( 1 9 2 , 6 4 ) . So , i fy o ui n c r e a s et h es c r o l l i n gv a l u e s , y o umo v et h es c r e e nt ot h er i g h t , wh i c hc o r r e s p o n d st ot h ema pmo v i n gleft o nt h es c r e e n .I n ma t h e ma t i c a l t e r ms , i fy o uh a v ema pp o s i t i o np a n ds c r e e np o s i t i o nq, t h e nt h ef o l l o wi n gi st r u e : (9.1) q + dx =p q =p − dx Direction of offset registers Th eo f f s e tr e g i s t e r sREG_BGx HOFSa n dREG_BGx VOFSi n d i c a t ewh i c hma pl o c a t i o ni sma p p e dt ot h et o p l e f to ft h e s c r e e n , me a n i n gp o s i t i v eo f f s e t ss c r o l l t h ema pl e f ta n du p .Wa t c hy o u rmi n u ss i g n s . Offset registers are write only Th eo f f s e tr e g i s t e r sa r ewrite-only!Th a tme a n st h a td i r e c ta r i t h me t i cl i k e‘ +=’wi l l n o two r k . 9.2.3. Useful types and #defines To n c ' sc o d eh a ss e v e r a l u s e f u l e x t r at y p e sa n dma c r o st h a tc a nma k el i f eal i t t l ee a s i e r . // === Additional types (tonc_types.h) ================================ //! Screen entry conceptual typedef typedef u16 SCR_ENTRY; //! Affine parameter struct for backgrounds, covered later typedef struct BG_AFFINE { s16 pa, pb; s16 pc, pd; s32 dx, dy; } ALIGN4 BG_AFFINE; //! Regular map offsets typedef struct BG_POINT { s16 x, y; } ALIGN4 BG_POINT; //! Screenblock struct typedef SCR_ENTRY SCREENBLOCK[1024]; // === Memory map #defines (tonc_memmap.h) ============================ //! Screen-entry mapping: se_mem[y][x] is SBB y, entry x #define se_mem ((SCREENBLOCK*)MEM_VRAM) //! BG control register array: REG_BGCNT[x] is REG_BGxCNT #define REG_BGCNT ((vu16*)(REG_BASE+0x0008)) file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 81/331 28-03-13 Tonc : GBA Programming in rot13 //! BG offset array: REG_BG_OFS[n].x/.y is REG_BGnHOFS / REG_BGnVOFS #define REG_BG_OFS ((BG_POINT*)(REG_BASE+0x0010)) //! BG affine params array #define REG_BG_AFFINE ((BG_AFFINE*)(REG_BASE+0x0000)) St r i c t l ys p e a k i n g , ma k i n gaSCREEN_ENTRYt y p e d e fi sn o tn e c e s s a r y , b u tma k e si t su s ec l e a r e r .se_memwo r k smu c hl i k e tile_mem: i tma p so u tVRAM i n t os c r e e n b l o c k ss c r e e n e n t r i e s , ma k i n gf i n d i n gas p e c i f i ce n t r ye a s i e r .Th eo t h e rt y p e d e f sa r eu s e dt o ma po u ta r r a y sf o rt h eb a c k g r o u n dr e g i s t e r s .Fo re x a mp l e , REG_BGCNTi sa na r r a yt h a tma p so u ta l l REG_BGx CNTr e g i s t e r s . REG_BGCNT[0]i sREG_BG0 CNT, e t c .Th eBG_POI NTa n dBG_AFFI NEt y p e sa r eu s e di ns i mi l a rf a s h i o n s .No t et h a t REG_BG_OFSs t i l l c o v e r st h es a mer e g i s t e r sa sREG_BGx HOFSa n dREG_BGx VOFSd o , a n dt h ewr i t e o n l y n e s so ft h e mh a sn o t ma g i c a l l yd i s a p p e a r e d .Th es a meg o e sf o rREG_BG_AFFI NE, b u tt h a td i s c u s s i o nwi l l b es a v e df o ra n o t h e rt i me . I nt h e o r y , i ti sa l s ou s e f u l c r e a t eas o r to fb a c k g r o u n dAPI , wi t has t r u c twi t ht h et e mp o r a r i e sf o rma pp o s i t i o n i n ga n df u n c t i o n sf o r i n i t i a l i z i n ga n du p d a t i n gt h er e g i s t e r sa n dma p s .Ho we v e r , mo s to ft o n c ' sd e mo sa r en o tc o mp l e xe n o u g ht owa r r a n tt h e s et h i n g s .Wi t h t h et y p e sa b o v e , ma n i p u l a t i n gt h en e c e s s a r yi t e msi sa l r e a d ys i mp l i f i e de n o u g hf o rn o w. 9.3. Regular background tile-maps Th es c r e e n b l o c k sf o r mama t r i xo fs c r e e ne n t r i e st h a td e s c r i b et h ef u l l i ma g eo nt h es c r e e n .I nt h ee x a mp l eo ff i g9 . 1t h et i l e ma pe n t r i e s j u s tc o n t a i n e dt h et i l ei n d e x .Th eGBAs c r e e ne n t r i e sb a h a v eal i t t l ed i f f e r e n t l y . Fo rr e g u l a rt i l e ma p s , e a c hs c r e e ne n t r yi s1 6 b i t sl o n g .Be s i d e st h et i l ei n d e x , i tc o n t a i n sf l i p p i n gf l a g sa n dap a l e t t eb a n ki n d e xf o r 4 b p p/1 6c o l o rt i l e s .Th ee x a c tl a y o u tc a nb ef o u n di nt a b l e9 . 7 .Th ea f f i n es c r e e ne n t r i e sa r eo n l y8b i t swi d ea n dj u s tc o n t a i na n8 b i tt i l e i n d e x . Screen entry format for regular backgrounds FEDC B PB A 9876543210 VF HF bits name define description 0 9 TI D SE_ID# ft h eSE. Tile-index o AB HF, VF SE_HFLI P, SE_VFLI P. SE_FLIP# CF PB TID l a g s . Horizontal/vertical flipping f SE_PALBANK# Palette bank t ou s ewh e ni n1 6 c o l o rmo d e .Ha sn oe f f e c tf o r2 5 6 c o l o rb g s ( REG_BGxCNT{6}i ss e t ) . 9.3.1. Map layout VRAM c o n t a i n s3 2s c r e e n b l o c k st os t o r et h et i l e ma p si n .Ea c hs c r e e n b l o c ki s8 0 0 hb y t e sl o n g , s oy o uc a nf i t3 2 ×3 2s c r e e ne n t r i e si n t o i t , wh i c he q u a l so n e2 5 6 ×2 5 6p i x e l ma p .Th eb i g g e rma p ss i mp l yu s emo r et h a no n es c r e e n b l o c k .Th es c r e e n b l o c ki n d e xs e ti n REG_BGxCNTi st h escreen base block wh i c hi n d i c a t e st h es t a r to ft h et i l e ma p . No w, s u p p o s ey o uh a v eat i l e ma pt h a t ' stw×th t i l e s / SEsi ns i z e .Yo umi g h te x p e c tt h a tt h es c r e e ne n t r ya tt i l e c o o r d i n a t e s( tx, ty) c o u l db ef o u n da tSEn u mb e rn =tx+ty· tw, b e c a u s et h a t ' sh o wma t r i c e sa l wa y swo r k , r i g h t ?We l l , y o u ' db ewr o n g .Atl e a s t , y o u ' db e partially wr o n g . Wi t h i ne a c hs c r e e n b l o c kt h ee q u a t i o nwo r k s , b u tt h eb i g g e rb a c k g r o u n d sd o n ' ts i mp l yuse mu l t i p l es c r e e n b l o c k s , t h e y ' r ea c t u a l l y a c c e s s e da sf o u rs e p a r a t ema p s .Ho wt h i swo r k sc a nb es e e ni nt a b l e9 . 8 : e a c hn u mb e r e db l o c ki sac o n t i n g e n tb l o c ki nme mo r y .Th i s me a n st h a tt og e tt h eSEi n d e xy o uh a v et of i n do u twh i c hs c r e e n b l o c ky o ua r ei na n dt h e nf i n dt h eSEn u mb e ri n s i d et h a ts c r e e n b l o c k . 32x32 64x32 32x64 64x64 0 0 0 0 1 1 2 3 1 Table 9.8: screenblock layout of regular backgrounds. Th i sk i n do fn e s t i n gp r o b l e mi s n ' ta sh a r da si tl o o k s .Wek n o wh o wma n yt i l e sf i ti nas c r e e n b l o c k , s ot og e tt h eSBBc o o r d i n a t e s , a l l we h a v et od od i v i d et h et i l e c o o r d sb yt h eSBBwi d t ha n dh e i g h t : sbx=tx/ 3 2a n dsby=ty/ 3 2 .Th eSBBn u mb e rc a nt h e nb ef o u n dwi t ht h e s t a n d a r dma t r i x →a r r a yf o r mu l a .Tof i n dt h ei n SBBSEn u mb e r , weh a v et ou s etx%3 2a n dty%3 2t of i n dt h ei n SBBc o o r d i n a t e s , a n d t h e na g a i nt h ec o n v e r s i o nf r o m2 Dc o o r d st oas i n g l ee l e me n t .Th i si st ob eo f f s e tb yt h eSBBn u mb e rt i l e st h es i z eo fa nSBBt of i n d t h ef i n a l n u mb e r .Th ef i n a l f o r mwo u l db e : //! Get the screen entry index for a tile-coord pair // And yes, the div and mods will be converted by the compiler uint se_index(uint tx, uint ty, uint pitch) file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 82/331 28-03-13 Tonc : GBA Programming in rot13 { uint sbb= (ty/32)*(pitch/32) + (tx/32); return sbb*1024 + (ty%32)*32 + tx%32; } Th eg e n e r a l f o r mu l ai sl e f ta sa ne x e r c i s ef o rt h er e a d e r–o n et h a ti swe l l wo r t ht h ee f f o r t , i nmyv i e w.Th i sk i n do fp r o c e s sc r o p su pi n an u mb e ro fp l a c e s , l i k eg e t t i n gt h eo f f s e tf o rb i t ma pc o o r d i n a t e si nt i l e s , a n dt i l ec o o r d si n1 Do b j e c tma p p i n g . I fa l l t h o s eo p e r a t i o n sma k ey o uq u e a s y , t h e r e ' sa l s oaf a s t e rv e r s i o ns p e c i f i c a l l yf o ra2 ×2a r r a n g e me n t .I ts t a r t swi t hc a l c u l a t i n gt h e n u mb e ra si fi t ' sa3 2 ×3 2 tma p .Th i swi l l b ei n c o r r e c tf o ra6 4 twi d ema p , wh i c hwec a nc o r r e c tf o rb ya d d i n g0 x 0 4 0 0 −0 x 2 0( i . e . , t i l e s / b l o c k−t i l e sp e rr o w) .Wen e e da n o t h e rf u l l b l o c kc o r r e c t i o ni st h es i z ei s6 4 ×6 4 t . //! Get the screen entry index for a tile-coord pair. /*! This is the fast (and possibly unsafe) way. * \param bgcnt Control flags for this background (to find its size) */ uint se_index_fast(uint tx, uint ty, u16 bgcnt) { uint n= tx + ty*32; if(tx >= 32) n += 0x03E0; if(ty >= 32 && (bgcnt&BG_REG_64x64)==BG_REG_64x64) n += 0x0400; return n; } Iwo u l dl i k et or e mi n dy o ut h a tn h e r ei st h eSEn u mb e r , n o tt h ea d d r e s s .Si n c et h es i z eo far e g u l a rSEi s2b y t e s , y o un e e dt omu l t i p l yn b y2f o rt h ea d d r e s s .( Un l e s s , o fc o u r s e , y o uh a v eap o i n t e r / a r r a yo fu16s , i nwh i c hc a s en wi l l wo r kf i n e . )Al s o , t h i swo r k sf o rr e g u l a r b a c k g r o u n d so n l y ;a f f i n eb a c k g r o u n d su s eal i n e a rma ps t r u c t u r e , wh i c hma k e st h i se x t r awo r ku n n e c e s s a r yt h e r e .Byt h ewa y , b o t ht h e s c r e e n e n t r ya n dma pl a y o u t sa r ed i f f e r e n tf o ra f f i n eb a c k g r o u n d s .Fo rt h e i rf o r ma t s , s e et h ema pf o r ma ts e c t i o no ft h ea f f i n e b a c k g r o u n dp a g e . 9.3.2. Background tile subtleties Th e r ea r et woa d d i t i o n a l t h i n g sy o un e e dt ob ea wa r eo fwh e nu s i n gt i l e sf o rt i l e ma p s .Th ef i r s tc o n c e r n st i l e n u mb e r i n g .Fo rs p r i t e s , n u mb e r i n gwe n ta c c o r d i n gt o4 b i tt i l e s( s t i l e s ) ;f o r8 b i tt i l e s( d t i l e s )y o u ' dh a v eu s emu l t i p l e so f2( ab i tl i k eu 1 6a d d r e s s e sa r ea l wa y s mu l t i p l e so f2i nme mo r y ) .I nt i l e ma p s , h o we v e r , d t i l e sa r en u mb e r e db yt h ed t i l e .Top u ti ti no t h e rwo r d s , f o rs p r i t e s , u s i n gi n d e xid i n d i c a t e st h es a met i l ef o rb o t h4a n d8 b i tt i l e s , n a me l yt h eo n et h a ts t a r t sa tid· 2 0 h .Fo rt i l e ma p s , h o we v e r , i ts t a r t sa tid· 2 0 hf o r4 b i t t i l e s , b u ta tid· 4 0 hf o r8 b i tt i l e s . memory offset 000h 020h 040h 060h 080h 100h ... 4bpp tile 8bpp tile 0 1 0 2 3 1 4 5 . . . 2 . . . Table 9.9: t i l ec o u n t i n gf o rb a c k g r o u n d s , s t i c k st oi t sb i t de pt h . Th es e c o n dc o n c e r n s , we l l , a l s ot i l e n u mb e r i n g , b u tmo r eh o wma n yt i l e sy o uc a nu s e .Ea c hma pe n t r yf o rr e g u l a rb a c k g r o u n d sh a s1 0 b i t sf o rat i l ei n d e x , s oy o uc a nu s eu pt o1 0 2 4t i l e s .Ho we v e r , aq u i c kc a l c u l a t i o ns h o wst h a tac h a r b l o c kc o n t a i n s4 0 0 0 h / 2 0 h =5 1 2s t i l e s , o r4 0 0 0 h / 4 0 h =2 5 6d t i l e s .Sowh a t ' st h ed e a l h e r e ?We l l , t h ec h a r b l o c ki n d e xy o us e ti nREG_BGxCNTi sa c t u a l l yo n l yt h eb l o c k wh e r et i l e c o u n t i n gs t a r t s : i t scharacter base block.Yo uc a nu s et h eo n e sa f t e ri ta swe l l .Co o l , h u h ?Bu twa i t , i fy o uc a na c c e s s s u b s e q u e n tc h a r b l o c k sa swe l l ;d o e st h i sme a nt h a t , i fy o us e tt h eb a s ec h a r b l o c kt o3 , y o uc a nu s et h es p r i t eb l o c k s( wh i c ha r eb a s i c a l l y b l o c k s4a n d5 )a swe l l ? Th ea n s we ri s : y e s .An dNO! Th ee mu l a t o r sI ' v et e s t e do nd oa l l o wy o ut od ot h i s .Ho we v e r , ar e a l GBAd o e s n ' t .I td o e so u t p u tsomething, t h o u g h : t h es c r e e n e n t r ywi l l b eu s e da st i l e d a t ai t s e l f , b u ti nama n n e rt h a ts i mp l yd e f i e se x p l a n a t i o n .Tr u s tmeo nt h i so n e , o k a y ?Oft h ec u r r e n tt o n c d e mo s , t h i si so n eo ft h ef e wt i me st h a tVBAg e t si twr o n g . Available tiles Fo rb o t h4 b p pa n d8 b p pr e g u l a rb g s , y o uc a na c c e s s1 0 2 4t i l e s .Th eo n l yc a v e a th e r ei st h a ty o uc a n n o ta c c e s st h et i l e si nt h e o b j e c tc h a r b l o c k se v e ni ft h ei n d e xwo u l dc a l l f o ri t . An o t h e rt h i n gy o uma yb ewo n d e r i n gi si fy o uc a nu s eap a r t i c u l a rs c r e e n b l o c kt h a ti swi t h i nac u r r e n t l yu s e dc h a r b l o c k .Fo re x a mp l e , i s i ta l l o we dt oh a v eab a c k g r o u n du s ec h a r b l o c k0a n ds c r e e n b l o c k1 .Ag a i n , y e sy o uc a nd ot h i s .Th i sc a nb eu s e f u l s i n c ey o u ' r en o tl i k e l y t of i l l a ne n t i r ec h a r b l o c k , s ou s i n gi t sl a t e rs c r e e n b l o c k sf o ry o u rma pd a t ai sag o o di d e a .( As i g no fTr u eHa c k e r d o mwo u l db ei fy o u ma n a g et ou s et h es a med a t af o rb o t ht i l e sa n dSEsa n ds t i l l g e tame a n i n g f u l i ma g e( t h i sl a s tp a r ti si mp o r t a n t ) .I fy o uh a v ed o n et h i s , p l e a s el e tmek n o w. ) file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 83/331 28-03-13 Tonc : GBA Programming in rot13 Tilemap data conversion via CLI Ac o n v e r t e rt h a tc a nt i l ei ma g e s( f o ro b j e c t s ) , c a na l s oc r e a t eat i l e s e tf o rt i l e ma p s , a l t h o u g ht h e r ewi l l l i k e l yb ema n y r e d u n d a n tt i l e s .Af e wc o n v e r t e r sc a na l s or e d u c et h et i l e s e tt oo n l yt h eu n i q u et i l e s , a n dp r o v i d et h et i l e ma pt h a tg o e swi t hi t . Th eBr i n s t a rb i t ma pf r o mf i g9 . 1i sa5 1 2 ×2 5 6i ma g e , wh i c hc o u l db et i l e dt oa6 4 x 3 2ma pwi t ha4 b p pt i l e s e tr e d u c e df o r u n i q u e n e s si nt i l e s , i n c l u d i n gp a l e t t ei n f oa n dmi r r o r i n g . # gfx2gba # (C array; u8 foo_Tiles[], u16 foo_Map[], # u16 master_Palette[]; foo.raw.c, foo.map.c, master.pal.c) gfx2gba -fsrc -c16 -t8 -m foo.bmp # grit # (C array; u32 fooTiles[], u16 fooMap[], u16 fooPal[]; foo.c, foo.h) grit foo.bmp -gB4 -mRtpf Twon o t e so ng f x 2 g b a : Fi r s t , i tme r g e st h ep a l e t t et oas i n g l e1 6 c o l o ra r r a y , r e a r r a n g i n gi ti nt h ep r o c e s s .Se c o n d , wh i l ei tl i s t s me t a ma p p i n go p t i o n si nt h er e a d me , i ta c t u a l l yd o e s n ' tg i v eame t a ma pa n dme t a t i l e s e t , i tj u s tf o r ma t st h ema pi n t od i f f e r e n t b l o c k s . 9.4. Tilemap demos Th e r ea r ef o u rd e mo si nt h i sc h a p t e r .Th ef i r s to n ei sbrin_demo, wh i c hi sv e r y , v e r ys h o r ta n ds h o wst h eb a s i cs t e p so ft i l el o a d i n g a n ds c r o l l i n g .Th en e x to n e sa r ec a l l e dsbb_rega n dcbb_demo, wh i c ha r et e c hd e mo s , i l l u s t r a t i n gt h el a y o u to fmu l t i p l e s c r e e n b l o c k sa n dh o wt i l ei n d e x i n gi sd o n eo n4 b p pa n d8 b p pb a c k g r o u n d s .I nb o t ht h e s ec a s e s , t h ema pd a t ai sc r e a t e dma n u a l l y b e c a u s ei t ' smo r ec o n v e n i e n tt od os oh e r e , b u tu s i n gma p d a t ac r e a t e db yma pe d i t o r sr e a l l yi s n ' tt h a td i f f e r e n t . 9.4.1. Essential tilemap steps: brin_demo AsI ' v eb e e nu s i n ga5 1 2 ×2 5 6p a r to fBr i n s t a rt h r o u g h o u tt h i sc h a p t e r , It h o u g h tImi g h ta swe l l u s ei tf o rad e mo . Th e r ea r eaf e wma pe d i t o r so u tt h e r et h a ty o uc a nu s e .Twog o o do n e sa r eNe s s i e ' sMa p Edo rMa p p y , b o t ho fwh i c hh a v ea n u mb e ro fi n t e r e s t i n gf e a t u r e s .Ih a v emyo wnma pe d i t o r , mi r a c h , b u ti t ' sj u s tav e r yb a s i ct h i n g .So met u t o r i a l sma yp o i n ty o ut o GBAMa p Ed i t o r .Donot u s et h i se d i t o ra si t ' sp r e t t yb u g g y , l e a v i n go u th a l fo ft h et i l e ma p ss o me t i me s .Ti l e ma p sc a nb et r o u b l e s o me e n o u g hf o rb e g i n n e r swi t h o u th a v i n gt owo r r ya b o u twh e t h e rt h ema pd a t ai sf a u l t y . I nt h i sc a u s e , h o we v e r , Ih a v e n ' tu s e da n ye d i t o ra ta l l .So meo ft h eg r a p h i c sc o n v e r t e r sc a nc o n v e r tt oat i l e s e t +t i l e ma p–i t ' sn o tt h e s t a n d a r dme t h o d , b u tf o rs ma l l ma p si tma ywe l l b ee a s i e r .I nt h i sc a s eI ' v eu s e dUs e n t i t od oi t , b u tg r i ta n dg f x 2 g b awo r kj u s ta swe l l . No t et h a tb e c a u s et h ema ph e r ei s6 4 ×3 2t i l e s , wh i c hr e q u i r e ss p l i t t i n gi n t os c r e e n b l o c k s .I nUs e n t i t h i si sc a l l e dt h e‘ s b b ’l a y o u t , i ng r i t i t ' s‘ mLs ’a n df o rg f x 2 g b ay o u ' du s e‘ mm3 2 ’… It h i n k .I na n yc a s e , a f t e rac o n v e r s i o ny o u ' dh a v eap a l e t t e , at i l e s e ta n dat i l e ma p . const unsigned short brinMap[2048]= { // Map row 0 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x3001,0x3002, 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000, 0x3001,0x3002,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000, 0x3001,0x3002,0x0000,0x0000,0x3001,0x3002,0x0000,0x0000, Fig 9.3a: brin_demopa l e t t e . Fig 9.3b: brin_demot i l e s e t . // Map row 1 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x3003,0x3004, 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000, 0x3003,0x3004,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000, 0x3003,0x3004,0x0000,0x0000,0x3003,0x3004,0x0000,0x0000, // Map row 2 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000, 0x3001,0x3002,0x3005,0x3006,0x3007,0x3008, // ... etc I nf i g9 . 3y o uc a ns e et h ef u l l p a l e t t e , t h et i l e s e ta n dp a r to ft h ema p .No t et h a tt h et i l e s e to ff i g9 . 3 bi sn o tt h es a mea st h a to ff i g9 . 1 b b e c a u s et h ef o r me ru s e s8 ×8t i l e swh i l et h el a t t e ru s e d1 6 ×1 6t i l e s .No t ea l s ot h a tt h es c r e e ne n t r i e sy o us e eh e r ea r ee i t h e r0( i . e . , t h e e mp t yt i l e )o ro ft h ef o r m0x3xxx.Th eh i g hn y b b l ei n d i c a t e st h ep a l e t t eb a n k , i nt h i sc a s et h r e e .I fy o u ' dl o o kt ot h ep a l e t t e( f i g9 . 3 a ) y o u ' ds e et h a tt h i sg i v e sb l u i s hc o l o r s . No wo nt ou s i n gt h e s ed a t a .Re me mb e rt h ee s s e n t i a l s t e p sh e r e : Lo a dt h eg r a p h i c s : t i l e si n t oc h a r b l o c k sa n dc o l o r si nt h eb a c k g r o u n dp a l e t t e . Lo a dama pi n t oo n eo rmo r es c r e e n b l o c k s . Swi t c ht ot h er i g h tmo d ei nREG_DI SPCNTa n da c t i v a t eab a c k g r o u n d . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 84/331 28-03-13 Tonc : GBA Programming in rot13 I n i t i a l i z et h a tb a c k g r o u n d ' sc o n t r o l r e g i s t e rt ou s et h er i g h tCBB, SBBa n db i t d e p t h . I fy o ud oi tc o r r e c t l y , y o us h o u l dh a v es o me t h i n gs h o wi n go ns c r e e n .I fn o t , g ot ot h et i l e / ma p / me mo r yv i e we r so fy o u re mu l a t o r ;t h e y ' l l u s u a l l yg i v ey o uag o o di d e awh e r et h ep r o b l e mi s .Ac o mmo no n ei sh a v i n gami s ma t c hb e t we e nt h eCBBa n dSBBi nREG_BGxCNT a n dwh e r ey o up u tt h ed a t a , wh i c hmo s tl i k e l ywo u l dl e a v ey o uwi t ha ne mp t yma po re mp t yt i l e s e t . Th ef u l l c o d eo fbrin_demoi sg i v e nb e l o w.Th et h r e ec a l l st omemcpy()l o a du pt h ep a l e t t e , t i l e s e ta n dt i l e ma p .Fo rs o me r e a s o n , i t ' sb e c o met r a d i t i o n a l t op l a c et h ema p si nt h el a s ts c r e e n b l o c k s .I nt h i sc a s e , t h a t ' s3 0r a t h e rt h a n3 1b e c a u s ewen e e dt wo b l o c k sf o ra6 4 ×3 2 tma p .Fo rt h es c r o l l i n gp a r t , I ' mu s i n gt wov a r i a b l e st os t o r ea n du p d a t et h ep o s i t i o n sb e c a u s et h es c r o l l i n gr e g i s t e r s a r ewr i t e o n l y .I ' ms t a r t i n ga t( 1 9 2 , 6 4 )h e r eb e c a u s et h a t ' swh a tIu s e df o rt h es c r o l l i n gp i c t u r eo ff i g9 . 2e a r l i e r . #include <string.h> #include "toolbox.h" #include "input.h" #include "brin.h" int main() { // Load palette memcpy(pal_bg_mem, brinPal, brinPalLen); // Load tiles into CBB 0 memcpy(&tile_mem[0][0], brinTiles, brinTilesLen); // Load map into SBB 30 memcpy(&se_mem[30][0], brinMap, brinMapLen); // set up BG0 for a 4bpp 64x32t map, using // using charblock 0 and screenblock 31 REG_BG0CNT= BG_CBB(0) | BG_SBB(30) | BG_4BPP | BG_REG_64x32; REG_DISPCNT= DCNT_MODE0 | DCNT_BG0; // Scroll around some int x= 192, y= 64; while(1) { vid_vsync(); key_poll(); x += key_tri_horz(); y += key_tri_vert(); REG_BG0HOFS= x; REG_BG0VOFS= y; } return 0; } Fig 9.4a: brin_demoa tdx=( 1 9 2 , 64) . Fig 9.4b: brin_demoa tdx=( 0 , 0 ) . Interlude: Fast-copying of non sbb-prepared maps Th i si sn o te x a c t l yr e q u i r e dk n o wl e d g e , b u ts h o u l dma k ef o ra ni n t e r e s t i n gr e a d .I nt h i sd e moIu s eamu l t i s b bma pt h a twa sa l r e a d y p r e p a r e df o rt h a t .Th ec o n v e r t e rma d es u r et h a tt h el e f tb l o c ko ft h ema pc a meb e f o r et h er i g h tb l o c k .I ft h i swe r e n ' tt h ec a s et h e ny o u c o u l d n ' tl o a dt h ewh o l ema pi no n eg ob e c a u s et h es e c o n dr o wo ft h el e f tb l o c kwo u l du s et h ef i r s tr o wo ft h er i g h tb l o c ka n ds oo n( s e e f i g9 . 5 ) . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 85/331 28-03-13 Tonc : GBA Programming in rot13 Fig 9.5 brin_demowi t h o u tb l o c k i n go u ti n t oS BB' sf i r s t . Th e r ea r ef e ws i mp l ea n ds l o wwa y sa n do n es i mp l ea n df a s twa yo fc o p y i n gan o ns b b p r e p a r e dma pt oamu l t i p l es c r e e n b l o c k s .Th e s l o wwa ywo u l db et op e r f o r mad o u b l el o o pt og or o wb yr o wo fe a c hs c r e e n b l o c k .Th ef a s twa yi st h r o u g hs t r u c t c o p i e sa n dp o i n t e r a r i t h me t i c , l i k et h i s : typedef struct { u32 data[8]; } BLOCK; int iy; BLOCK *src= (BLOCK*)brinMap; BLOCK *dst0= (BLOCK*)se_mem[30]; BLOCK *dst1= (BLOCK*)se_mem[31]; for(iy=0; iy<32; iy++) { // Copy row iy of the left half *dst0++= *src++; *dst0++= *src++; // Copy row iy of the right half *dst1++= *src++; *dst1++= *src++; } ABLOCKs t r u c t c o p yt a k e sc a r eo fh a l far o w, s ot wot a k e sc a r eo fawh o l es c r e e n b l o c kr o w( y e s , y o uc o u l dd e f i n eBLOCKa sa1 6 wo r ds t r u c t , b u tt h a two u l d n ' two r ko u ta n y mo r e .Tr u s tme ) .Att h a tp o i n t , t h esrcp o i n t e rh a sa r r i v e da tt h er i g h th a l fo ft h ema p , s owe c o p yt h en e x tr o wi n t ot h er i g h t h a n ds i d ed e s t i n a t i o n , dst1.Wh e nd o n ewi t ht h a t , srcp o i n t st ot h es e c o n dr o wo ft h el e f ts i d e .No w d ot h i sf o ra l l 3 2l i n e s .Hu z z a hf o rs t r u c t c o p i e s , a n dp o i n t e r s ! 9.4.2. A screenblock demo Th es e c o n dd e mo , sbb_regu s e sa6 4 x 6 4 tb a c k g r o u n dt oi n d i c a t eh o wmu l t i p l es c r e e n b l o c k sa r eu s e df o rb i g g e rma p si nmo r ed e t a i l . Wh i l et h ebrin_demou s e damu l t i s b bma pa swe l l , i twa s n ' te a s yt os e ewh a t ' swh a tb e c a u s et h ema pwa si r r e g u l a r ;t h i sd e mou s e s av e r ys i mp l et i l e s e ts oy o uc a nc l e a r l ys e et h es c r e e n b l o c kb o u n d a r i e s .I t ' l l a l s os h o wh o wy o uc a nu s et h eREG_BG_OFSr e g i s t e r sf o r s c r o l l i n gr a t h e rt h a nREG_BGxHOFSa n dREG_BGxVOFS. #include "toolbox.h" #include "input.h" #define CBB_0 0 #define SBB_0 28 #define CROSS_TX 15 #define CROSS_TY 10 BG_POINT bg0_pt= { 0, 0 }; SCR_ENTRY *bg0_map= se_mem[SBB_0]; uint se_index(uint tx, uint ty, uint pitch) { uint sbb= ((tx>>5)+(ty>>5)*(pitch>>5)); return sbb*1024 + ((tx&31)+(ty&31)*32); } void init_map() { int ii, jj; file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 86/331 28-03-13 Tonc : GBA Programming in rot13 // initialize a background REG_BG0CNT= BG_CBB(CBB_0) | BG_SBB(SBB_0) | BG_REG_64x64; REG_BG0HOFS= 0; REG_BG0VOFS= 0; // (1) create the tiles: basic tile and a cross const TILE tiles[2]= { {{0x11111111, 0x01111111, 0x01111111, 0x01111111, 0x01111111, 0x01111111, 0x01111111, 0x00000001}}, {{0x00000000, 0x00100100, 0x01100110, 0x00011000, 0x00011000, 0x01100110, 0x00100100, 0x00000000}}, }; tile_mem[CBB_0][0]= tiles[0]; tile_mem[CBB_0][1]= tiles[1]; // (2) create a palette pal_bg_bank[0][1]= RGB15(31, 0, 0); pal_bg_bank[1][1]= RGB15( 0, 31, 0); pal_bg_bank[2][1]= RGB15( 0, 0, 31); pal_bg_bank[3][1]= RGB15(16, 16, 16); // (3) Create a map: four contingent blocks of // 0x0000, 0x1000, 0x2000, 0x3000. SCR_ENTRY *pse= bg0_map; for(ii=0; ii<4; ii++) for(jj=0; jj<32*32; jj++) *pse++= SE_PALBANK(ii) | 0; } int main() { init_map(); REG_DISPCNT= DCNT_MODE0 | DCNT_BG0 | DCNT_OBJ; u32 tx, ty, se_curr, se_prev= CROSS_TY*32+CROSS_TX; bg0_map[se_prev]++; // initial position of cross while(1) { vid_vsync(); key_poll(); // (4) Moving around bg0_pt.x += key_tri_horz(); bg0_pt.y += key_tri_vert(); // (5) Testing se_index // If all goes well the cross should be around the center of // the screen at all times. tx= ((bg0_pt.x>>3)+CROSS_TX) & 0x3F; ty= ((bg0_pt.y>>3)+CROSS_TY) & 0x3F; se_curr= se_index(tx, ty, 64); if(se_curr != se_prev) { bg0_map[se_prev]--; bg0_map[se_curr]++; se_prev= se_curr; } REG_BG_OFS[0]= bg0_pt; // write new position } return 0; } Th einit_map()c o n t a i n sa l l o ft h ei n i t i a l i z a t i o ns t e p s : s e t t i n gu pt h er e g i s t e r s , t i l e s , p a l e t t e sa n dma p s .Un l i k et h ep r e v i o u sd e mo , t h e t i l e s , p a l e t t ea n dt h ema pa r ea l l c r e a t e dma n u a l l yb e c a u s ei t ' sj u s te a s i e ri nt h i sc a s e .Atp o i n t( 1 ) , Id e f i n et wot i l e s .Th ef i r s to n el o o k s al i t t l el i k eap a n ea n dt h es e c o n do n ei sar u d i me n t a r yc r o s s .Yo uc a ns e et h e mc l e a r l yi nt h es c r e e n s h o t( f i g9 . 6 ) .Th ep a n e l i k et i l ei s l o a d e di n t ot i l e0 , a n di st h e r e f o r et h e‘ d e f a u l t ’t i l ef o rt h ema p . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 87/331 28-03-13 Tonc : GBA Programming in rot13 Th ep a l e t t ei ss e ta tp o i n t( 2 ) .Th ec o l o r sa r et h es a mea si nt a b l e9 . 8 : r e d , g r e e n , b l u ea n dg r e y .Ta k en o t eo fwh i c hp a l e t t ee n t r i e s I ' mu s i n g : t h ec o l o r sa r ei nd i f f e r e n tp a l e t t eb a n k ss ot h a tIc a nu s ep a l e t t es wa p p i n gwh e nIf i l l t h ema p .Sp e a k i n go fwh i c h… Lo a d i n gt h ema pi t s e l f( p o i n t( 3 ) )h a p p e n st h r o u g had o u b l el o o p .Th eo u t e rl o o ps e t st h ep a l e t t e b a n kf o rt h es c r e e ne n t r i e s .Th e i n n e rl o o pf i l l s1 0 2 4SEswi t hp a l e t t e s wa p p e dt i l e 0 ' s .No w, i fb i gma p su s e daf l a tl a y o u t , t h er e s u l two u l db eab i gma pi nf o u rc o l o r e d b a n d s .Ho we v e r , wh a ta c t u a l l yh a p p e n si st h a ty o us e eblocks, n o tb a n d s , p r o v i n gt h a t i n d e e dr e g u l a rma p sa r es p l i ti n t os c r e e n b l o c k sj u s tl i k et a b l e9 . 8s a i d .Ye s , i t ' sa n n o y i n g , b u t t h a t ' sj u s tt h ewa yi ti s . Th a twa sc r e a t i n gt h ema p , n o wwet u r nt ot h ema i nl o o pi nmain().Th ek e y s( p o i n t( 4 ) ) l e ty o us c r o l l a r o u n dt h ema p .Th eRI GHTb u t t o ni st i e dt oap o s i t i v ec h a n g ei nx, b u tt h e ma pi t s e l fa c t u a l l ys c r o l l st ot h eleft!Wh e nIs a yi tl i k et h a ti tma ys e e mc o u n t e r i n t u i t i v e , b u ti fy o ul o o ka tt h ed e moy o us e et h a ti ta c t u a l l yma k e ss e n s e .Th i n ko fi tf r o ma h y p o t h e t i c a l p l a y e rs p r i t ep o i n to fv i e w.Ast h es p r i t emo v e st h r o u g ht h ewo r l d , y o un e e dt o u p d a t et h eb a c k g r o u n dt ok e e pt h es p r i t ef r o mg o i n go f f s c r e e n .Tod ot h a t , t h e Fig 9.6: sbb_reg. Co mpa r et a b l e9 . 8, 64 x 64 t b a c k g r o u n d ' smo v e me n ts h o u l db et h eo p p o s i t eo ft h es p r i t e ' smo v e me n t .Fo re x a mp l e , i f backgr o u n d . No t et h el i t t l ec r o s si nt het o pl e f t t h es p r i t emo v e st ot h eright, y o uh a v et omo v et h eb a c k g r o u n dt ot h eleft t oc o mp e n s a t e . cor n e r . Fi n a l l y , t h e r e ' so n emo r et h i n gt od i s c u s s : t h ec r o s st h a ta p p e a r sc e n t e r e do nt h ema p . Tod ot h i sa sy o us c r o l l a l o n g , Ik e e pt r a c ko ft h es c r e e n e n t r ya tt h ec e n t e ro ft h es c r e e nv i aan u mb e ro fv a r i a b l e sa n dt h e se_index()f u n c t i o n .Va r i a b l e stxa n dtya r et h et i l ec o o r d i n a t e so ft h ec e n t e ro ft h es c r e e n , f o u n db ys h i f t i n ga n dma s k i n gt h e b a c k g r o u n dp i x e l c o o r d i n a t e s .Fe e d i n gt h e s et ose_index()g i v e smet h es c r e e n e n t r yo f f s e tf r o mt h es c r e e nb a s eb l o c k .I ft h i si s d i f f e r e n tt h a nt h ep r e v i o u so f f s e t , Ir e p a i n tt h ef o r me ro f f s e ta sap a n e , a n du p d a t et h en e wo f f s e tt ot h ec r o s s .Th a twa y , t h ec r o s s s e e mst omo v eo v e rt h ema p ;mu c hl i k eas p r i t ewo u l d .Th i swa sa c t u a l l yd e s i g n e da sat e s tf o rse_index();i ft h ef u n c t i o nwa s f l a we d , t h ec r o s swo u l dj u s td i s a p p e a ra ts o mep o i n t .Bu ti td o e s n ' t .Ya yme^_^ 9.4.3. The charblock demo Th et h i r dd e mo , cbb_democ o v e r ss o meo ft h ed e t a i l so fc h a r b l o c k sa n dt h ed i f f e r e n c e si n4 b p pa n d8 b p pt i l e s .Th eb a c k g r o u n d si n q u e s t i o na r eBG0a n dBG1 .Bo t hwi l l b e3 2 x 3 2 tb a c k g r o u n d s , b u tBG0wi l l u s e4 b p pt i l e sa n dCBB0a n dBG2u s e s8 b p pt i l e sa n d CBB2 .Th ee x a c tl o c a t i o n sa n dc o n t e n t so ft h es c r e e n b l o c k sa r en o ti mp o r t a n t ;wh a ti si mp o r t a n ti st ol o a dt h et i l e st ot h es t a r t so fa l l 6 c h a r b l o c k sa n ds e ewh a th a p p e n s . #include <toolbox.h> #include "cbb_ids.h" #define CBB_4 0 #define SBB_4 2 #define CBB_8 2 #define SBB_8 4 void load_tiles() { int ii; TILE *tl= (TILE*)ids4Tiles; TILE8 *tl8= (TILE8*)ids8Tiles; // Loading tiles. don't get freaked out on how it looks // 4-bit tiles to blocks 0 and 1 tile_mem[0][1]= tl[1]; tile_mem[0][2]= tl[2]; tile_mem[1][0]= tl[3]; tile_mem[1][1]= tl[4]; // and the 8-bit tiles to blocks 2 though 5 tile8_mem[2][1]= tl8[1]; tile8_mem[2][2]= tl8[2]; tile8_mem[3][0]= tl8[3]; tile8_mem[3][1]= tl8[4]; tile8_mem[4][0]= tl8[5]; tile8_mem[4][1]= tl8[6]; tile8_mem[5][0]= tl8[7]; tile8_mem[5][1]= tl8[8]; // And let's not forget the palette (yes, obj pal too) u16 *src= (u16*)ids4Pal; for(ii=0; ii<16; ii++) pal_bg_mem[ii]= pal_obj_mem[ii]= *src++; } void init_maps() { // se4 and se8 map coords: (0,2) and (0,8) SB_ENTRY *se4= &se_mem[SBB_4][2*32], *se8= &se_mem[SBB_8][8*32]; // show first tiles of char-blocks available to bg0 // tiles 1, 2 of char-block CBB_4 se4[0x01]= 0x0001; se4[0x02]= 0x0002; file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 88/331 28-03-13 Tonc : GBA Programming in rot13 // tiles 0, 1 of char-block CBB_4+1 se4[0x20]= 0x0200; se4[0x21]= 0x0201; // show first tiles of char-blocks available to bg1 // tiles 1, 2 of char-block CBB_8 (== 2) se8[0x01]= 0x0001; se8[0x02]= 0x0002; // tiles 1, 2 of char-block CBB_8+1 se8[0x20]= 0x0100; se8[0x21]= 0x0101; // tiles 1, 2 of char-block CBB_8+2 (== CBB_OBJ_LO) se8[0x40]= 0x0200; se8[0x41]= 0x0201; // tiles 1, 2 of char-block CBB_8+3 (== CBB_OBJ_HI) se8[0x60]= 0x0300; se8[0x61]= 0x0301; } int main() { load_tiles(); init_maps(); // init backgrounds REG_BG0CNT= BG_CBB(CBB_4) | BG_SBB(SBB_4) | BG_4BPP; REG_BG1CNT= BG_CBB(CBB_8) | BG_SBB(SBB_8) | BG_8BPP; // enable backgrounds REG_DISPCNT= DCNT_MODE0 | DCNT_BG0 | DCNT_BG1 | DCNT_OBJ; while(1); return 0; } Th et i l e s e t sc a nb ef o u n di ncbb_ids.c.Ea c ht i l ec o n t a i n st won u mb e r s : o n ef o rt h ec h a r b l o c kI ' mp u t t i n gi ta n do n ef o rt h et i l e i n d e x i nt h a tb l o c k .Fo re x a mp l e , t h et i l et h a tIwa n ti nc h a r b l o c k0a tt i l e1s h o ws‘ 0 1 ’ , CBB1t i l e0s h o ws‘ 1 0 ’ , CBB1 , t i l e1h a s‘ 1 1 ’ , e t c .I h a v et we l v et i l e si nt o t a l , 4s t i l e st ob eu s e df o rBG0a n d8d t i l e sf o rBG1 . No w, Ih a v es i xp a i r so ft i l e sa n dIi n t e n dt op l a c et h e mi nt h ef i r s tt i l e so fe a c ho ft h e6c h a r b l o c k( e x c e p tf o rCBBs0a n d2 , wh e r e t i l e0wo u l db eu s e da sd e f a u l tt i l e sf o rt h eb a c k g r o u n d , wh i c hIwa n tt ok e e pe mp t y ) .Ye ss i x , I ' ml o a d i n gi n t ot h es p r i t ec h a r b l o c k sa s we l l .Ic o u l dd ot h i sb yh a n d , c a l c u l a t i n ga l l t h ea d d r e s s e sma n u a l l y( 0600:0020f o rCBB0 , t i l e1 , e t c )a n dh o p eId o n ' tma k ea mi s t a k ea n dc a nr e me mb e rwh a tI ' md o i n gwh e nr e v i s i t i n gt h ed e mol a t e r , o rIc a nj u s tu s emytile_mema n dtile8_memme mo r y ma pma t r i c e sa n dg e tt h ea d d r e s s e sq u i c k l ya n dwi t h o u ta n yh a s s l e .Ev e nb e t t e r , Ca l l o wss t r u c ta s s i g n me n t ss oIc a nl o a dt h ei n d i v i d u a l t i l e swi t has i mp l ea s s i g n me n t !Th a ti se x a c t l ywh a tI ' md o i n gi nload_tiles().Th es o u r c et i l e sa r ec a s tt oTI LEa n dTI LE8a r r a y s f o r4 b p pa n d8 b p pt i l e sr e s p e c t i v e l y .Af t e rt h a t , l o a d i n gt h et i l e si sv e r ys i mp l ei n d e e d . Th ema p st h e ms e l v e sa r ec r e a t e di ninit_maps().Th eo n l yt h i n gI ' mi n t e r e s t e di nf o rt h i sd e moi st os h o wh o wa n dwh i c h c h a r b l o c k sa r eu s e d , s ot h ep a r t i c u l a r so ft h ema pa r e n ' tt h a ti mp o r t a n t .Th eo n l yt h i n gIwa n tt h e mt od oi st ob ea b l et os h o wt h et i l e s t h a tIl o a d e di nload_tiles().Th et wop o i n t e r sIc r e a t eh e r e , se4a n dse8, p o i n tt os c r e e n e n t r i e si nt h es c r e e n b l o c k su s e df o r BG0a n dBG1 , r e s p e c t i v e l y .BG0 ' sma p , c o n t a i n i n gs t i l e s , u s e s1a n d5 1 2o f f s e t s ;BG1 ' se n t r i e s , 8 b p pt i l e s , c a r r i e s1a n d2 5 6o f f s e t s . I fwh a tIs a i db e f o r ea b o u tt i l e i n d e xf o rd i f f e r e n tb i t d e p t h si st r u e , t h e ny o us h o u l ds e et h ec o n t e n t so fa l l t h el o a d e dt i l e s .An dl o o k i n ga t t h er e s u l to ft h ed e mo( f i g9 . 7 ) , i tl o o k sa si fId i dmyma t hc o r r e c t l y : b a c k g r o u n dt i l e i n d i c e sf o l l o wt h eb g ' sa s s i g n e db i t d e p t h , i nc o n t r a s t t os p r i t e swh i c ha l wa y sc o u n t si n3 2b y t eo f f s e t s . Th e r ei s , h o we v e r , o n ep o i n to fc o n c e r n : o nh a r d wa r e , y o uwo n ' ts e et h et i l e st h a ta r ea c t u a l l yi no b j e c tVRAM ( b l o c k s4a n d5 ) . Wh i l ey o umi g h te x p e c tt ob ea b l et ou s et h es p r i t eb l o c k sf o rb a c k g r o u n d sd u et ot h ea d d r e s s e s , t h ea c t u a l wi r i n gi nt h eGBAs e e mst o f o r b i di t .Th i si swh yy o us h o u l dt e s to nh a r d wa r ei si mp o r t a n t : e mu l a t o r sa r e n ' ta l wa y sp e r f e c t .Bu ti fh a r d wa r et e s t i n gi sn o ta v a i l a b l e t oy o u , t e s to nmu l t i p l ee mu l a t o r s ;i fy o us e ed i f f e r e n tb e h a v i o u r , b ewa r yo ft h ec o d et h a tp r o d u c e di t . Fig 9.7a: cbb_demoo nVBA( a n dBo yc o t tAd v Fig 9.7b: cbb_demoo nh a r d wa r e . S po tt h e a n dMa ppywe l l , a l mo s t )) . d i f f e r e n c e s ! file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 89/331 28-03-13 Tonc : GBA Programming in rot13 9.4.4. Bonus demo: the 'text' in text bg and introducing tonclib Wo o , b o n u sd e mo !Th i se x a mp l ewi l l s e r v ean u mb e ro fp u r p o s e s .Th ef i r s ti st oi n t r o d u c etonclib, al i b r a r yo fc o d et oma k el i f eo n t h eGBAab i te a s i e r .I np a s td e mo s , I ' v eb e e nu s i n gtoolbox.h/ct os t o r eu s e f u l ma c r o sa n df u n c t i o n s .Th i si sa l r i g h tf o rv e r ys ma l l p r o j e c t s , b u ta sc o d eg e t sa d d e d , i tb e c o me sv e r yh a r dt oma i n t a i ne v e r y t h i n g .I t ' sb e t t e rt os t o r ec o mmo nf u n c t i o n a l i t yi nl i b r a r i e st h a t c a nb es h a r e da mo n gp r o j e c t s . Th es e c o n dr e a s o ni st os h o wh o wy o uc a no u t p u tt e x t , wh i c hi so b v i o u s l ya ni mp o r t a n ta b i l i t yt oh a v e .To n c l i bh a sa ne x t e n s i v el i s t o fo p t i o n sf o rt e x tr e n d e r i n g–t o omu c ht oe x p l a i nh e r e–b u ti t si n t e r f a c ei sp r e t t ye a s y .Fo rd e t a i l s , v i s i tt h eTo n cTe x tEn g i n ec h a p t e r . An y wa y , h e r e ' st h ee x a mp l e . #include <stdio.h> #include <tonc.h> int main() { REG_DISPCNT= DCNT_MODE0 | DCNT_BG0; // Init BG 0 for text on screen entries. tte_init_se_default(0, BG_CBB(0)|BG_SBB(31)); tte_write("#{P:72,64}"); tte_write("Hello World!"); // Goto (72, 64). // Print "Hello world!" while(1); return 0; } Fig 9.8a: hello d e mo . Fig 9.8b: t i l e s e to ft h ehellod e mo . Ye s , i ti si n d e e da“ h e l l owo r l d ”d e mo , t h es t a r t i n gp o i n to fn e a r l ye v e r yi n t r o d u c t o r yC/ C++t u t o r i a l .Ho we v e r , t h o s ea r eu s u a l l yf o r me a n tf o rPCp l a t f o r ms , wh i c hh a v en a t i v ec o n s o l ef u n c t i o n a l i t yl i k eprintf()o rcout.Th e s ed on o te x i s tf o rt h eGBA.( Or “ d i d n ' t ” , Is h o u l ds a y ;t h e r ea r ewa y st oma k eu s eo ft h e mn o wa d a y s .Se et t e : c o n i of o rd e t a i l s . ) To n c ' ss u p p o r tf o rt e x tg o e st h r o u g htte_f u n c t i o n s .I nt h i sc a s e , tte_init_se_default()s e t su pb a c k g r o u n d0f o rt i l e ma p p e dt e x t .I ta l s ol o a d st h ed e f a u l t8 ×8f o n ti n t oc h a r b l o c k0( s e ef i g9 . 8 b ) .Af t e rt h a t , y o uc a nwr i t et ot e x twi t htte_write.Th e s e q u e n c e#{P:x,y}i st h ef o r ma t t i n gc o mma n dt h a tTTEu s e st op o s i t i o nt h ec u r s o r .Th e r ea r ean u mb e ro ft h e s e , s o meo fwh i c h y o u ' l l a l s os e ei nl a t e rc h a p t e r s . Fr o mt h i sp o i n to n , I ' l l ma k el i b e r a l u s eo ft o n c l i b ' st e x tc a p a b i l i t i e si ne x a mp l e sf o rd i s p l a y i n gv a l u e sa n dt h el i k e .Th i swi l l mo s t l yh a p p e n wi t h o u te x p l a n a t i o n , b e c a u s et h a two n ' tb ep a r to ft h ed e mo .Ag a i n , t os e et h ei n t e r n a l s , g ot ot h eTTEc h a p t e r . Creating and using code libraries Us i n gt h ef u n c t i o n st h e ms e l v e si sp r e t t ys i mp l e , b u tt h e ya r es p r e a do u to v e rmu l t i p l ef i l e sa n dr e f e r e n c ee v e nmo r e .Th i sma k e si ta h a s s l et of i n dwh i c hf i l e sy o un e e dt oa d dt ot h el i s to fs o u r c e st oc o mp i l eap r o j e c t .Yo uc o u l da d de v e r y t h i n g , o fc o u r s e , b u tt h a t ' sn o ta p l e a s a n tp r o s p e c te i t h e r .Th eb e s ts o l u t i o ni st op r e c o mp i l et h eu t i l i t yc o d ei n t oal i b r a r y . Li b r a r i e sa r ee s s e n t i a l l yc l u s t e r so fo b j e c tf i l e s .I n s t e a do fl i n k i n gt h eo b j e c t si n t oa ne x e c u t a b l ed i r e c t l y , y o uarchive t h e mwi t h arm-none-eabi-ar.Th ec o mma n di ss i mi l a rt ot h el i n ks t e pa swe l l .He r ei sh o wy o uc a nc r e a t et h el i b r a r ylibfoo.af r o m o b j e c t sfoo.o, bar.oa n dbaz.o. # archive rule libfoo : foo.o bar.o baz.o arm-none-eabi-ar -crs libfoo.a foo.o bar.o baz.o # shorthand rule: $(AR) rcs $@ $^ Th et h r e ef l a g ss t a n df o rcr e a t ea r c h i v e , re p l a c eme mb e ra n dc r e a t esy mb o l t a b l e , r e s p e c t i v e l y .Fo rmo r eo nt h e s ea n do t h e ra r c h i v i n g f l a g s , Iwi l l r e f e ry o ut ot h ema n u a l , wh i c hi sp a r to ft h eb i n u t i l st o o l s e t .Th ef l a g sa r ef o l l o we db yt h el i b r a r yn a me , wh i c hi sf o l l o we db y a l l t h eo b j e c t s( t h e‘ me mb e r s ’y o uwa n tt oa r c h i v e ) . Tou s et h el i b r a r y , y o uh a v et ol i n ki tt ot h ee x e c u t a b l e .Th e r ea r et wol i n k e rf l a g so fi n t e r e s th e r e : -La n d-l.Up p e r -a n d file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 90/331 28-03-13 Tonc : GBA Programming in rot13 l o we r c a s e‘ L’ .Th ef o r me r , -La d d sal i b r a r yp a t h .Th el o we r c a s ev e r s i o n , -l, a d d st h ea c t u a l l i b r a r y , b u tt h e r ei sat wi s th e r e : o n l y n e e dt h er o o t n a meo ft h el i b r a r y .Fo re x a mp l e , t ol i n kt h el i b r a r ylibfoo.a, u s e‘ -lfoo’ .Th ep r e f i xliba n de x t e n s i o n.aa r e a s s u me db yt h el i n k e r . # using libfoo (assume it's in ../lib) $(PROJ).elf : $(OBJS) $(LD) $^ $(LDFLAGS) -L../lib -lfoo -o $@ Ofc o u r s e , t h e s ea r c h i v e sc a ng e tp r e t t yb i gi fy o ud u mpal o to fs t u f fi nt h e r e .Yo umi g h two n d e ri fa l l o fi ti sl i n k e dwh e ny o ua d da l i b r a r yt oy o u rp r o j e c t .Th ea n s we ri sn o , i ti sn o t .Th el i n k e ri ss ma r te n o u g ht ou s eo n l yt h ef i l e swh i c hf u n c t i o n sy o u ' r ea c t u a l l y r e f e r e n c i n g .I nt h ec a s eo ft h i sd e mo , f o re x a mp l e , I ' mu s i n gv a r i o u st e x tf u n c t i o n s , b u tn o n eo ft h ea f f i n ef u n c t i o n so rt a b l e s , s ot h o s e a r ee x c l u d e d .No t et h a tt h ee x c l u s i o ng o e sb yfile, n o tb yfunction.I fy o uo n l yh a v eo n ef i l ei nt h el i b r a r y( o r#i n c l u d e de v e r y t h i n g , wh i c ha mo u n t st ot h es a met h i n g ) , e v e r y t h i n gwi l l b el i n k e d . Ii n t e n dt ou s et o n c l i bi nan u mb e ro fl a t e rd e mo s .I np a r t i c u l a r , t h eme mo r yma p , t e x ta n dc o p yr o u t i n e swi l l b ep r e s e n to f t e n .Do n ' t wo r r ya b o u twh a tt h e yd of o rt h ed e mo ;j u s tf o c u so nt h ec o r ec o n t e n ti t s e l f .Do c u me n t a t i o no ft o n c l i bc a nb ef o u n di nt h et o n c l i bf o l d e r ( tonc/code/libtonc)a n da th t t p : / / www. c o r a n a c . c o m/ ma n / t o n c l i b / . Better copy and fill routines: memcpy16/32 and memset16/32 No wt h a tIa mu s i n gt o n c l i ba sal i b r a r yf o ri t st e x tr o u t i n e s , Imi g h ta swe l l u s ei tf o ri t sc o p ya n df i l l r o u t i n e sa swe l l .Th e i r n a me sa r ememcpy16()a n dmemcpy32()f o rc o p i e sa n dmemset16()a n dmemset32()f o rf i l l r o u t i n e s .Th e1 6 a n d3 2d e n o t et h e i rp r e f e r r e dd a t a t y p e s : h a l f wo r d sa n dwo r d s , r e s p e c t i v e l y .Th e i ra r g u me n t sa r es i mi l a rt ot h et r a d i t i o n a l memcpy()a n dmemset(), wi t ht h ee x c e p t i o nt h a tt h es i z ei st h en u mb e ro fi t e mst ob ec o p i e d , r a t h e rt h a ni nb y t e s . void memset16(void *dest, u16 hw, uint hwcount); void memcpy16(void *dest, const void *src, uint hwcount); void memset32(void *dest, u32 wd, uint wcount) IWRAM_CODE; void memcpy32(void *dest, const void *src, uint wcount) IWRAM_CODE; Th e s er o u t i n e sa r eo p t i mi z e da s s e mb l ys ot h e ya r ef a s t .Th e ya r ea l s os a f e rt h a nt h ed mar o u t i n e s , a n dt h eBI OSr o u t i n e CpuFastSet().Ba s i c a l l y , Ih i g h l yr e c o mme n dt h e m, a n dIwi l l u s et h e mwh e r e v e rIc a n . Linker options: object files before libraries I nmo s tc a s e s , y o uc a nc h a n g et h eo r d e ro ft h eo p t i o n sa n df i l e sf r e e l y , b u ti nt h el i n k e r ' sc a s ei ti si mp o r t a n tt h eo b j e c tf i l e so f t h ep r o j e c t sa r eme n t i o n e dbefore t h el i n k e dl i b r a r i e s .I fn o t , t h el i n kwi l l f a i l .Wh e t h e rt h i si ss t a n d a r db e h a v i o u ro ri fi ti sa n o v e r s i g h ti nt h el i n k e r ' swo r k i n g sIc a n n o ts a y , b u tb ea wa r eo fp o t e n t i a l p r o b l e msh e r e . 9.5. In conclusion Ti l e ma p sa r ee s s e n t i a l f o rmo s tt y p e so fGBAg a me s .Th e ya r et r i c k i e rt og e tt og r i p swi t ht h a nt h eb i t ma pmo d e so rs p r i t e sb e c a u s e t h e r ea r emo r es t e p st og e te x a c t l yr i g h t .An d , o fc o u r s e , y o un e e dt ob es u r et h ee d i t o rt h a tg a v ey o ut h ema pa c t u a l l ys u p p l i e dt h ed a t a y o uwe r ee x p e c t i n g .Fo o l a r o u n dwi t ht h ed e mo sal i t t l e : r u nt h e m, c h a n g et h ec o d ea n ds e ewh a th a p p e n s .Fo re x a mp l e , y o uc o u l dt r yt o a d ds c r o l l i n gc o d et ot h ebrin_demos oy o uc a ns e et h ewh o l ema p .Ch a n g es c r e e nb l o c k s , c h a n g ec h a r b l o c k , c h a n g et h eb i t d e p t h , me s su pintentionally s oy o uc a ns e ewh a tc a ng owr o n g , s oy o u ' l l b ep r e p a r e df o ri twh e ny o ut r yy o u ro wnma p s .On c ey o u ' r e c o n f i d e n te n o u g h , o n l yt h e ns t a r tma k i n gy o u ro wn .Ik n o wi t ' st h eb o r i n gwa y , b u ty o uwi l l b e n e f i tf r o mi ti nt h el o n gr u n . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 91/331 28-03-13 Tonc : GBA Programming in rot13 10. The Affine Transformation Matrix (a.k.a. P) Ab o u tt h i sp a g e . Te x t u r ema p p i n ga n da f f i n et r a n s f o r ma t i o n s . " Ma n yo ft h et r u t h swec l i n gt od e p e n dg r e a t l yu p o no u ro wnp o i n to fv i e w. " . Fi n i s h i n gu p . To n c ' sa f f i n ef u n c t i o n s . 10.1. About this page Asy o up r o b a b l yk n o w, t h eGBAi sc a p a b l eo fa p p l y i n gg e o me t r i ct r a n s f o r ma t i o n sl i k er o t a t i n ga n d / o rs c a l i n gt os p r i t e sa n db a c k g r o u n d s . Tos e tt h e ma p a r tf r o mt h er e g u l a ri t e ms , t h et r a n s f o r ma b l eo n e sa r eg e n e r a l l yr e f e r r e dt oa sRo t / Sc a l es p r i t e sa n db a c k g r o u n d s .Th e t r a n s f o r ma t i o n sa r ed e s c r i b e db yf o u rp a r a me t e r s , pa, pb, pca n dpd.Th el o c a t i o n sa n de x a c tn a me sd i f f e rf o rs p r i t e sa n d b a c k g r o u n d sb u tt h a td o e s n ' tma t t e rf o rn o w. Th e r ea r et wowa y so fi n t e r p r e t i n gt h e s en u mb e r s .Th ef i r s ti st ot h i n ko fe a c ho ft h e ma si n d i v i d u a l o f f s e t st ot h es p r i t ea n d b a c k g r o u n dd a t a .Th i si sh o wt h er e f e r e n c ed o c u me n t sl i k eGBATe ka n dCo wBi t eSp e cd e s c r i b et h e m.Th eo t h e rwa yi st os e et h e ma s t h ee l e me n t so fa2 x 2ma t r i xwh i c hIwi l l r e f e rt oa sP.Th i si sh o wp r e t t ymu c ha l l t u t o r i a l sd e s c r i b et h e m.Th e s et u t o r i a l sa l s og i v et h e f o l l o wi n gma t r i xf o rr o t a t i o na n ds c a l i n g : (10.1) P = pa pb s· c os ( α) sy· s i n ( α) = x pc pd −sx· s i n ( α) sy· c os ( α) No w, t h i si si n d e e dar o t a t i o na n ds c a l ema t r i x .Un f o r t u n a t e l y , i t ' sa l s ot h ewr o n go n e !Ora tl e a s t , i tp r o b a b l yd o e sn o td owh a ty o u ' d e x p e c t .Fo re x a mp l e , c o n s i d e rt h ec a s ewi t has c a l i n go fsx=1 . 5 , sy=1 . 0a n dar o t a t i o no fα=4 5 .Yo u ' dp r o b a b l ye x p e c ts o me t h i n gl i k e f i g1 0 . 1 a , b u twh a ty o u ' da c t u a l l yg e ti s1 0 . 1 b .Th es p r i t eh a sr o t a t e d , b u ti nt h ewr o n gd i r e c t i o n , i th a ss h r u n kr a t h e rt h a ne x p a n d e da n d t h e r e ' sa ne x t r as h e a ra swe l l .Ofc o u r s e , y o uc a na l wa y ss a yt h a ty o ume a n tf o rt h i st oh a p p e n , b u tt h a t ' sp r o b a b l yn o tq u i t et r u e . Fig 10.1a: wh e nyo us a y‘ r o t a t e Fig 10.1b: b u twi t hP f r o m a n ds c a l e ’ , yo upr o b a b l ye x pe c t e q1 0 . 1 , t h i si swh a tyoug e t . t h i s… Un f o r t u n a t e l y , t h e r ei sal o to fi n c o r r e c to rmi s l e a d i n gi n f o r ma t i o no nt h et r a n s f o r ma t i o nma t r i xa r o u n d ;t h ema t r i xo fe q1 0 . 1i sj u s to n e a s p e c to fi t .Th i sa c t u a l l ys t a r t swi t ht h emo n i k e r“ Ro t / Sc a l e ” , wh i c hd o e sn o tf i twi t hwh a ta c t u a l l yo c c u r s , c o n t i n u e swi t ht h ef a c tt h a t t h et e r msu s e da r en e v e rp r o p e r l yd e f i n e da n dt h a tmo s tp e o p l eo f t e nj u s tc o p y p a s t ef r o mo t h e r swi t h o u te v e nc o n s i d e r i n gc h e c k i n g wh e t h e rt h ei n f o r ma t i o ni sc o r r e c to rn o t .Th ei r o n yi st h a tt h ep r i n c i p l er e f e r e n c ed o c u me n t , GBATe k , g i v e st h ec o r r e c td e s c r i p t i o n so f e a c ho ft h ee l e me n t s , b u ts o me h o wi tg o tl o s ti nt h et r a n s l a t i o nt oma t r i xf o r mi nt h et u t o r i a l s . I nt h i sc h a p t e r , I ' l l p r o v i d et h ecorrect i n t e r p r e t a t i o no ft h ePma t r i x ;h o wt h eGBAu s e si ta n dh o wt oc o n s t r u c to n ey o u r s e l f .Tod o t h i s , t h o u g h , I ' mg o i n gi n t of u l l ma t h mo d e .I fy o ud o n ' tk n o wy o u rwa ya r o u n dv e c t o ra n dma t r i xc a l c u l a t i o n sy o uma yh a v es o me d i f f i c u l t i e su n d e r s t a n d i n gt h ef i n e rp o i n t so ft h et e x t .Th e r ei sa na p p e n d i xo nl i n e a ra l g e b r af o rs o mep o i n t e r so nt h i ss u b j e c t . Th i si sg o i n gt ob eap u r e l yt h e o r e t i c a l p a g e : y o uwi l l f i n dn o t h i n gt h a tr e l a t e sd i r e c t l yt os p r i t e so rb a c k g r o u n d sh e r e ;t h a t ' swh a tt h en e x t t wos e c t i o n sa r ef o r .On c ea g a i n , wewi l l b ea s s i s t e db yt h el o v e l yme t r o i d( k e e pi nc o l ds t o r a g ef o rs a f eu s e ) .Pl e a s emi n dt h ed i r e c t i o n o ft h ey a x i sa n dt h ea n g l e s , a n dd onot l e a v ewi t h o u tr e a d i n gt h ef i n i s h i n gu pp a r a g r a p h .Th i sc o n t a i n ss e v e r a l k e yi mp l e me n t a t i o n d e t a i l st h a twi l l b ei g n o r e di nt h et e x tp r e c e d i n gi t , b e c a u s et h e ywi l l o n l yg e ti nt h ewa ya tt h a tp o i n t . Be wary of documents covering affine parameters I t ' st r u e .Pr e t t ymu c he v e r yd o c u me n tI ' v es e e nt h a td e a l swi t ht h i ss u b j e c ti sp r o b l e ma t i ci ns o mewa y .Al o to ft h e mg i v e t h ewr o n gr o t a t e s c a l ema t r i x( n a me l y , t h eo n ei ne q1 0 . 1 ) , o rmi s n a mea n d / o rmi s r e p r e s e n tt h ema t r i xa n di t se l e me n t s . 10.2. Texture mapping and affine transformations. 10.2.1. General 2D texture mapping file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 92/331 28-03-13 Tonc : GBA Programming in rot13 Wh a tt h eGBAd o e st og e ts p r i t e sa n dt i l e db a c k g r o u n d so ns c r e e ni sv e r ymu c hl i k et e x t u r ema p p i n g .Sof o r g e ta b o u tt h eGBAr i g h t n o wa n dl o o ka th o wt e x t u r ema p p i n gi sd o n e .I nf i g1 0 . 2 a , wes e eame t r o i dt e x t u r e .Fo rc o n v e n i e n c eIa mu s i n gt h es t a n d a r dCa r t e s i a n 2 Dc o o r d i n a t es y s t e m( y a x i sp o i n t su p )a n dh a v en o r ma l i s e dt h et e x t u r e , wh i c hme a n st h a tt h er i g h ta n dt o ps i d eo ft h et e x t u r e c o r r e s p o n dp r e c i s e l ywi t ht h eu n i t v e c t o r se xa n de y( wh i c ha r eo fl e n g t h1 ) .Th et e x t u r ema p p i n gb r i n g sp ( i nt e x t u r es p a c e )t oap o i n tq ( i ns c r e e ns p a c e ) .Th ea c t u a l ma p p i n gi sd o n eb ya2 ×2ma t r i xA: q=A · p. Soh o wd oy o uf i n dA?We l l , t h a t ' sa c t u a l l yn o tt h a th a r d .Th ema t r i xi sf o r me db yl i n i n gu pt h et r a n s f o r me db a s ev e c t o r s , wh i c ha r eu a n dv ( t h i swo r k si na n yn u mb e ro fd i me n s i o n s , b t w) , s ot h a tg i v e su s : A= ux vx uy vy A → Fig 10.2a: at e x t u r e . Fig 10.2b: at e x t u r e ma ppe d Af o r wa r dt e x t u r ema ppi n gv i aa f f i n ema t r i xA. 10.2.2. Affine transformations Th et r a n s f o r ma t i o n sy o uc a nd owi t ha2 Dma t r i xa r ec a l l e daffine t r a n s f o r ma t i o n s .Th et e c h n i c a l d e f i n i t i o no fa na f f i n et r a n s f o r ma t i o n i so n et h a tp r e s e r v e sp a r a l l e l l i n e s , wh i c hb a s i c a l l yme a n st h a ty o uc a nwr i t et h e ma sma t r i xt r a n s f o r ma t i o n s , o rt h a tar e c t a n g l ewi l l b e c o meap a r a l l e l o g r a mu n d e ra na f f i n et r a n s f o r ma t i o n( s e ef i g1 0 . 2 b ) . Af f i n et r a n s f o r ma t i o n si n c l u d er o t a t i o na n ds c a l i n g , b u talso s h e a r i n g .Th i si swh yIo b j e c tt ot h en a me“ Ro t / Sc a l e ” : t h a tt e r mo n l y r e f e r st oas p e c i a l c a s e , n o tt h eg e n e r a l t r a n s f o r ma t i o n .I ti sa k i nt oc a l l i n gc o l o r ss h a d e so fr e d : y e s , r e d sa r ec o l o r st o o , b u tn o ta l l c o l o r sa r er e d s , a n dt oc a l l t h e mt h a two u l dg i v ead i s t o r t e dv i e wo ft h es u b j e c t . AsIs a i d , t h e r ea r et h r e eb a s i c2 dt r a n s f o r ma t i o n s , t h o u g hy o uc a na l wa y sd e s c r i b eo n eo ft h e s ei nt e r mso ft h eo t h e rt wo .Th e t r a n s f o r ma t i o n sa r e : r o t a t i o n( R) , s c a l i n g( S)a n ds h e a r( H) .Ta b l e1 0 . 1s h o wswh a te a c ho ft h et r a n s f o r ma t i o n sd o e st ot h er e g u l a r me t r o i ds p r i t e .Th eb l a c ka x e sa r et h en o r ma l b a s ev e c t o r s( n o t et h a ty p o i n t sd o wn ! ) , t h eb l u ea x e sa r et h et r a n s f o r me db a s ev e c t o r s a n dt h ec y a nv a r i a b l e sa r et h ea r g u me n t so ft h et r a n s f o r ma t i o n .Al s og i v e na r et h ema t r i xa n di n v e r s ema t r i xo fe a c ht r a n s f o r ma t i o n . Wh y ?Yo u ' l l s e e . (10.2) A = a b c d A−1 ≡ 1 d −b ad − bc −c a Ta b l e1 0 . 1 : t r a n s f o r ma t i o nma t r i c e sa n dt h e i ri n v e r s e s . Identity I= 10 01 I−1=I Rotation R( θ )= c o s ( θ ) −s i n ( θ ) s i n ( θ ) c o s ( θ ) R−1( θ )=R( −θ ) Scaling S( sx, sy)= sx 0 0 sy S−1( s , s)= x y S( 1/ s , 1/ s) x y Shear H( hx, hy)= 1 hx hy 1 H−1( h , h)= x y H( −h , −h) /( 1−h h) x y xy Wec a nn o wu s et h e s ed e f i n i t i o n st of i n dt h ec o r r e c tma t r i xf o re n l a r g e me n t sb ysxa n dsy, f o l l o we db yacounter-clockwise r o t a t i o nb y α( =−θ ) , b yma t r i xmu l t i p l i c a t i o n . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 93/331 28-03-13 Tonc : GBA Programming in rot13 (10.3) A =R( α)·S( s , s)= sx· c os ( α) sy· s i n ( α) x y −sx· s i n ( α) sy· c os ( α) …e r mm, wa i tas e c… I ' mh a v i n gt h i ss t r a n g es e n s eo fd é j a v uh e r e… Clockwise vs counterclockwise I t ' sami n o ri s s u e , b u tIh a v et ome n t i o ni t .I ft h ed e f i n i t i o no fR u s e sac l o c k wi s er o t a t i o n , wh ya mIs u d d e n l yu s i n gac o u n t e r c l o c k wi s eo n e ?We l l , t r a d i t i o n a l l yR i sg i v e na st h a tp a r t i c u l a rma t r i x , i nwh i c ht h ea n g l er u n sf r o mt h ex a x i st o wa r d st h ey a x i s .Be c a u s ey i sd o wn wa r d , t h i sc o me sd o wnt oc l o c k wi s e .Ho we v e r , t h ea f f i n er o u t i n e si nBI OSu s eac o u n t e r c l o c k wi s e r o t a t i o n , a n dIt h o u g h ti t ' db eag o o di d e at ou s et h a ta sag u i d e l i n ef o rmyf u n c t i o n s . Nomenclature: Affine vs Rot/Scale Th ema t r i xP i sn o tar o t a t i o nma t r i x , n o tas c a l i n gma t r i x , b u tag e n e r a l a f f i n et r a n s f o r ma t i o nma t r i x .Ro t a t i o na n ds c a l i n g ma yb ewh a tt h ema t r i xi smo s t l yu s e df o r , b u tt h a td o e sn o tme a nt h e y ' r et h eo n l yt h i n g sp o s s i b l e , a st h et e r m‘ Ro t / Sc a l e ’ wo u l di mp l y . Tos e tt h e ma p a r tf r o mr e g u l a rb a c k g r o u n d sa n ds p r i t e s , Is u p p o s e‘ Ro t a t i o n ’o r‘ Ro t / Sc a l e ’a r es u i t a b l ee n o u g h , j u s tn o t e n t i r e l ya c c u r a t e .Ho we v e r , c a l l i n gt h ePma t r i xb yt h o s en a me si ss i mp l ywr o n g . 10.3. “Many of the truths we cling to depend greatly upon our own point of view.” Asy o umu s th a v en o t i c e d , e q1 0 . 3i si d e n t i c a l t oe q1 0 . 1 , wh i c hIs a i dwa si n c o r r e c t .Sowh a tg i v e s ? We l l , i fy o ue n t e rt h i sma t r i xi n t ot h epa-pde l e me n t sy o ud oi n d e e dg e ts o me t h i n gd i f f e r e n tt h a nwh a t y o u ' de x p e c t .On l yn o wI ' v ep r o v e nwh a ty o uwe r es u p p o s e dt oe x p e c ti nt h ef i r s tp l a c e( n a me l ya s c a l i n gb ysxa n dsy, f o l l o we db yac o u n t e r c l o c k wi s er o t a t i o nb yα) .Th ereal q u e s t i o ni so fc o u r s e , wh y d o e s n ' tt h i swo r k ?Toa n s we rt h i sIwi l l p r e s e n tt wod i f f e r e n ta p p r o a c h e st ot h e2 Dma p p i n gp r o c e s s . 10.3.1. Human point of view “ He l l o , Ia mCe a r n ' sb r a i n .Ig r o kg e o me t r ya n dc a nd oma t r i x -t r a n s f o r ma t i o n si nmyh e a d .We l l , h i s h e a da c t u a l l y .Wh e ni tc o me st ot e x t u r ema p p i n gIs e et h eo r i g i n a l ma p( i nt e x t u r es p a c e )a n dt h e n v i s u a l i z et h et r a n s f o r ma t i o n .Il o o ka tt h eo r i g i n a l ma pa n dl o o ka twh e r et h ema p ' sp i x e l se n du po n s c r e e n .Th et r a n s f o r ma t i o nma t r i xf o rt h i si sA, wh i c ht i e st e x e l pt os c r e e np i x e l qv i aq=A · p.Th e c o l u mn so fA a r es i mp l yt h et r a n s f o r me du n i tma t r i c e s .Ea s ya sπ. ” Fig 10.3: Ma ppi n gpr oc e s sa s s e e nb yh u ma n s . ua ndv a r et h e c o l u mn so fA ( i ns c r e e ns pa c e ) . 10.3.2. Computer point of view “ He l l o , Ia mCe a r n ' sGBA.I ' mal e a n , me a ng a mi n gma c h i n et h a tf i t si ny o u rp o c k e t , a n dIc a np u s h p i x e l sl i k en oo n ee l s e .Ex c e p tp e r h a p smyo wn e r ' sGe Fo r c e4Ti 4 2 0 0 , t h eb l o o d ys h o wo f f .An y wa y , o n e o ft h et h i n g sId oi st e x t u r ema p p i n g .An dn o tj u s to r d i n a r yt e x t u r e ma p p i n g , Ic a nd oc o o l s t u f fl i k e r o t a t i o na n ds c a l i n ga swe l l .Wh a tId oi sf i l l p i x e l s , a l l In e e dt ok n o wi sf o ry o ut ot e l l mewh e r eIs h o u l d g e tt h ep i x e l ' sc o l o rf r o m.I no t h e rwo r d s , t of i l l s c r e e np i x e l q, In e e dama t r i xB t h a tg i v e smet h ep r o p e r t e x e l pv i ap = B · q.I ' l l h a p p i l yu s ea n yma t r i xy o ug i v eme ;Ih a v ec o mp l e t ec o n f i d e n c ei ny o u ra b i l i t y t os u p p l ymewi t ht h ema t r i xf o rt h et r a n s f o r ma t i o ny o ur e q u i r e . ” 10.3.3. Resolution Fig 10.4: Ma ppi n gpr oc e s sa s s e e nb yc o mpu t e r s . ua n dv ( i n t e x t u r es pa c e )a r et h ec ol umn sof Ba n da r ema ppe dt ot he pr i n c i pl ea x e si ns c r e e ns pa c e . Ih o p ey o us p o t t e dt h ec r u c i a l d i f f e r e n c eb e t we e nt h et wop o i n t so fv i e w.A ma p sfrom t e x t u r es p a c eto s c r e e ns p a c e , wh i l eB d o e st h ee x a c to p p o s i t e( i . e . , B = A-1) .It h i n ky o uk n o wwh i c ho n ey o us h o u l d g i v et h eGBAb yn o w.Th a t ' sr i g h t : P = B, n o tA.Th i so n eb i to fi n f o r ma t i o ni st h ec r u c i a l p i e c eo ft h e a f f i n ema t r i xp u z z l e . Son o wy o uc a nf i g u r eo u tP' se l e me n t si nt wowa y s .Yo uc a ns t i c kt ot h eh u ma nPOVa n di n v e r tt h ema t r i xa tt h ee n d .Th a t ' swh y Ig a v ey o ut h ei n v e r s e so ft h ea f f i n et r a n s f o r ma t i o n sa swe l l .Yo uc o u l da l s ot r yt os e et h i n g si nt h eGBA' swa ya n dg e tt h er i g h tma t r i x d i r e c t l y .To n c ' sma i na f f i n ef u n c t i o n s( tonc_video.h, tonc_obj_affine.ca n dtonc_bg_affine.c)d ot h i n g st h eGBA wa y , s e t t i n gP d i r e c t l y ;b u ti n v e r t e df u n c t i o n sa r ea l s oa v a i l a b l eu s i n ga n" _inv"a f f i x .Mi n dy o u , t h e s ea r eal i t t l es l o we r .Ex c e p tf o r wh e ns c a l i n gi si n v o l v e d ;t h e ni t ' salot s l o we r . I nc a s ey o u ' r ec u r i o u s , t h ep r o p e rma t r i xf o rs c a l eb y( sx, sx)a n dc o u n t e r c l o c k wi s er o t a t i o nb yαi s : A =R( −α) · S( sx, sy) −1 P =A −1 =(R( −α) · S( sx, sy)) −1 −1 =S( sx, sy) ·R( −α) Us i n gt h ei n v e r s ema t r i c e sg i v e ne a r l i e r , wef i n d file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 94/331 28-03-13 Tonc : GBA Programming in rot13 (10.4) P = pa pb c os ( α)/sx −s i n ( α)/sx = pc pd s i n ( α)/sy c os ( α)/sy J u s tt oma k ei tp e r f e c t l yc l e a r : Th ea f f i n ema t r i xP ma p sf r o ms c r e e ns p a c eto t e x t u r es p a c e , n o tt h eo t h e rwa ya r o u n d ! I no t h e rwo r d s : p a: t e x t u r exi n c r e me n t/p i x e l p b: t e x t u r exi n c r e me n t/s c a n l i n e p c: t e x t u r eyi n c r e me n t/p i x e l p d: t e x t u r eyi n c r e me n t/s c a n l i n e 10.4. Finishing up Kn o wi n gwh a tt h ePma t r i xi su s e df o ri so n et h i n g , k n o wi n gh o wt ou s et h e mp r o p e r l yi sa n o t h e r .Th e r ea r et h r e ea d d i t i o n a l p o i n t sy o u n e e dt or e me mb e rwh e ny o u ' r eg o i n gt od e a l wi t ha f f i n eo b j e c t s / b a c k g r o u n d sa n dt h ea f f i n ema t r i c e s . 1 .Da t a t y p e s 2 .Lu t s 3 .I n i t i a l i s a t i o n 10.4.1. Data types of affine elements Af f i n et r a n s f o r ma t i o n sa r ep a r to fma t h e ma t i c sa n d , g e n e r a l l ys p e a k i n g , ma t hn u mb e r swi l l b er e a l n u mb e r s .Th a ti st os a y , f l o a t i n gp o i n t n u mb e r s .Ho we v e r , i fy o uwe r et ou s ef l o a t i n gp o i n t sf o rt h eP e l e me n t s , y o u ' db ei nf o rt wor u d es u r p r i s e s . Th ef i r s to n ei st h a tt h ema t r i xe l e me n t sa r en o tf l o a t s , b u ti n t e g e r s .Th er e a s o nb e h i n dt h i si st h a tthe GBA has no floating point unit! Al l f l o a t i n g p o i n to p e r a t i o n sh a v et ob ed o n ei ns o f t wa r ea n dwi t h o u ta nFPU, t h a t ' sg o i n gt ob ep r e t t ys l o w.Mu c hs l o we rt h a n i n t e g e rma t h , a ta n yr a t e .No w, wh e ny o ut h i n ka b o u tt h i s , i td o e sc r e a t es o mep r o b l e mswi t hp r e c i s i o na n da l l t h a t .Fo re x a mp l e , t h e ( c o ) s i n ea n df u n c t i o n sh a v ear a n g eb e t we e n−1a n d1 , ar a n g ewh i c hi s n ' te x a c t l yl a r g ewh e ni tc o me st oi n t e g e r s .Ho we v e r , t h er a n g e wo u l db emu c hg r e a t e ri fo n ed i d n ' tc o u n ti nu n i t so f1 , b u ti nf r a c t i o n s , s a yi nu n i t so f1 / 2 5 6 .Th e[ −1 , +1 ]r a n g et h e nb e c o me s[ −2 5 6 , +2 5 6 ] , Th i ss t r a t e g yo fr e p r e s e n t i n gr e a l n u mb e r swi t hs c a l e di n t e g e r si sk n o wna sfixed point arithmetic, wh i c hy o uc a nr e a dmo r ea b o u t i nt h i sa p p e n d i xa n do nwi k i p e d i a .Th eGBAma k e su s eo ff i x e dp o i n tf o ri t sa f f i n ep a r a me t e r s , b u ty o uc a nu s ei tf o ro t h e rt h i n g sa s we l l .Th ePma t r i xe l e me n t sa r e8 . 8f i x e dp o i n tn u mb e r s , me a n i n gah a l f wo r dwi t h8i n t e g e rb i t sa n d8f r a c t i o n a l b i t s .Tos e tama t r i xt o i d e n t i t y( 1 so nt h ed i a g o n a l s , 0 se l s e wh e r e ) , y o uwo u l d n ' tu s et h i s : // Floating point == Bad!! pa= pd= 1.0; pb= pc= 0.0; b u tt h i s : // .8 Fixed-point == Good pa= pd= 1<<8; pb= pc= 0; Q I naf i x e dp o i n ts y s t e mwi t hQ f r a c t i o n a l b i t s , ‘ 1 ’( ‘ o n e ’ )i sr e p r e s e n t e db y2 o r1 <<Q, b e c a u s es i mp l yt h a t ' sh o wf r a c t i o n swo r k . No w, f i x e dp o i n tn u mb e r sa r es t i l l j u s ti n t e g e r s , b u tt h e r ea r ed i f f e r e n tt y p e so fi n t e g e r s , a n di ti si mp o r t a n tt ou s et h er i g h to n e s .8 . 8 f a r e1 6 b i tv a r i a b l e s , s ot h el o g i c a l c h o i c et h e r ei sshort.Ho we v e r , t h i ss h o u l db easigned s h o r t : s16, n o tu16.So me t i me si sd o e s n ' t ma t t e r , b u ti fy o uwa n tt od oa n ya r i t h me t i cwi t ht h e mt h e y ' db e t t e rb es i g n e d .Re me mb e rt h a ti n t e r n a l l yt h eCPUwo r k si nwo r d s , wh i c h a r e3 2 b i t , a n dt h e1 6 b i tv a r i a b l ewi l l b ec o n v e r t e dt ot h a t .Yo ur e a l l ywa n t , s a y , a1 6 b i t" −1 "( 0xFFFF)t ot u r ni n t oa3 2 b i t" −1 " ( 0xFFFFFFFF) , a n dn o t" 6 5 5 3 5 "( 0x0000FFFF) , wh i c hi swh a th a p p e n si fy o uu s eu n s i g n e ds h o r t s .Al s o , wh e nd o i n gf i x e dp o i n t ma t h , i ti sr e c o mme n d e dt ou s es i g n e di n t s( t h e3 2 b i tk i n d )f o rt h e m, a n y t h i n ge l s ewi l l s l o wy o ud o wna n dy o umi g h tg e to v e r f l o w p r o b l e msa swe l l . Use 32-bit signed ints for affine temporaries Ofc o u r s ey o us h o u l du s e3 2 b i tv a r i a b l e sf o re v e r y t h i n ga n y wa y( u n l e s sy o ua c t u a l l ywant y o u rc o d et ob l o a ta n ds l o wd o wn ) . I fy o uu s e1 6 b i tv a r i a b l e s( shorto rs16) , n o to n l ywi l l y o u rc o d eb es l o we rb e c a u s eo fa l l t h ee x t r ai n s t r u c t i o n st h a ta r e a d d e dt ok e e pt h ev a r i a b l e s1 6 b i t , b u to v e r f l o wp r o b l e msc a no c c u rmu c hs o o n e r . On l yi nt h ef i n a l s t e pt oh a r d wa r es h o u l dy o ug ot o8 . 8f o r ma t .Be f o r et h a t , u s et h el a r g e rt y p e sf o rb o t hs p e e da n d a c c u r a c y . 10.4.2. LUTs file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 95/331 28-03-13 Tonc : GBA Programming in rot13 Sof i x e dp o i n tma t hi su s e db e c a u s ef l o a t i n gp o i n tma t hi sj u s tt os l o wf o re f f i c i e n tu s e .Th a t ' sa l l f i n ea n dg o o df o ry o u ro wnma t h , b u t wh a ta b o u tma t h e ma t i c a l f u n c t i o n sl i k es i n ( )a n dc o s ( ) ?Th o s ea r es t i l l f l o a t i n gp o i n ti n t e r n a l l y( e v e nwo r s e , doubles! ) , s ot h o s ea r e g o i n gt ob er i d i c u l o u s l ys l o w. Ra t h e rt h a nu s i n gt h ef u n c t i o n sd i r e c t l y , we ' l l u s eat i me h o n o r e dt r a d i t i o nt owe a s e l o u rwa yo u to fu s i n gc o s t l yma t hf u n c t i o n s : we ' r eg o i n gt ob u i l dalook-up table ( LUT)c o n t a i n i n gt h es i n ea n dc o s i n ev a l u e s .Th e r ea r ean u mb e ro fwa y st od ot h i s .I fy o uwa n ta n e a s ys t r a t e g y , y o uc a nj u s td e c l a r et woa r r a y so f3 6 08 . 8 fn u mb e r sa n df i l l t h e ma ti n i t i a l i z a t i o no fy o u rp r o g r a m.Ho we v e r , t h i si sap o o r wa yo fd o i n gt h i n g s , f o rr e a s o n se x p l a i n e di nt h es e c t i o no nLUTsi nt h ea p p e n d i x . To n c l i bh a sas i n g l es i n el u twh i c hc a nb eu s e df o rb o t hs i n ea n dc o s i n ev a l u e s .Th el u ti sc a l l e dsin_lut, aconst shorta r r a y o f5 1 24 . 1 2 fe n t r i e s( 1 2f r a c t i o n a l b i t s ) , c r e a t e db ymye x c e l l u tl u tc r e a t o r .I ntonc_math.hy o uc a nf i n dt woi n l i n ef u n c t i o n st h a t r e t r i e v es i n ea n dc o s i n ev a l u e s : //! Look-up a sine and cosine values /*! \param theta Angle in [0,FFFFh] range * \return .12f sine value */ INLINE s32 lu_sin(uint theta) { return sin_lut[(theta>>7)&0x1FF]; } INLINE s32 lu_cos(uint theta) { return sin_lut[((theta>>7)+128)&0x1FF]; } No w, n o t et h ea n g l er a n g e : 0 1 0 0 0 0 h .Re me mb e ry o ud o n ' thave t ou s e3 6 0d e g r e e sf o rac i r c l e ;i nf a c t , o nc o mp u t e r si t ' sb e t t e rt o 16p d i v i d et h ec i r c l ei nap o we ro ft woi n s t e a d .I nt h i sc a s e , t h ea n g l ei si n2 a r t sf o rc o mp a t i b i l i t ywi t hBI OSf u n c t i o n s , wh i c hi sb r o u g h t d o wnt oa5 1 2r a n g ei n s i d et h el o o k u pf u n c t i o n s . 10.4.3. Initialization Wh e nf l a g g i n gab a c k g r o u n do ro b j e c ta sa f f i n e , y o umust e n t e ra tl e a s ts o mev a l u e si n t opa-pd.Re me mb e rt h a tt h e s ea r ez e r o e do u t b yd e f a u l t .Az e r o o f f s e tme a n si t ' l l u s et h ef i r s tp i x e l f o rt h ewh o l et h i n g .I fy o ug e tas i n g l e c o l o r e db a c k g r o u n do rs p r i t e , t h i si s p r o b a b l ywh y .Toa v o i dt h i s , s e tP t ot h ei d e n t i t yma t r i xo ra n yo t h e rn o n z e r oma t r i x . 10.5. Tonc's affine functions To n c l i bc o n t a i n san u mb e ro ff u n c t i o n sf o rma n i p u l a t i n gt h ea f f i n ep a r a me t e r so fo b j e c t sa n db a c k g r o u n d s , a su s e db yt h e OBJ_AFFINEa n dBG_AFFINEs t r u c t s .Be c a u s et h ea f f i n ema t r i xi ss t o r e dd i f f e r e n t l yi nb o t hs t r u c t sy o uc a n ' ts e tt h e mwi t ht h e s a mef u n c t i o n , b u tt h ef u n c t i o n a l i t yi st h es a me .I nt a b l e1 0 . 2y o uc a nf i n dt h eb a s i cf o r ma t sa n dd e s c r i p t i o n s ;j u s tr e p l a c efoo wi t h obj_affo rbg_affa n dFOO wi t hOBJo rBGf o ro b j e c t sa n db a c k g r o u n d s , r e s p e c t i v e l y .Th ef u n c t i o n st h e ms e l v e sc a nb ef o u n di n tonc_obj_affine.cf o ro b j e c t s , tonc_bg_affine.cf o rb a c k g r o u n d s , a n di n l i n e sf o rb o t hi ntonc_video.h… s o me wh e r e . Function Description v o i dfoo_c o p y ( FOO_AFFI NE* d s t , c o n s tFOO_AFFI NE* s r c , u i n tc o u n t ) ; Co p ya f f i n ep a r a me t e r s v o i dfoo_i d e n t i t y ( FOO_AFFI NE* o a f f ) ; P =I v o i dfoo_p o s t mu l ( FOO_AFFI NE* d s t , c o n s tFOO_AFFI NE* s r c ) ; Po s t mu l t i p l y : D =D· S v o i dfoo_p r e mu l ( FOO_AFFI NE* d s t , c o n s tFOO_AFFI NE* s r c ) ; Pr e mu l t i p l y : D =S· D v o i dfoo_r o t a t e ( FOO_AFFI NE* a f f , u 1 6a l p h a ) ; Ro t a t ec o u n t e r c l o c k wi s eb yα· π/ 8 0 0 0 h . v o i dfoo_r o t s c a l e ( FOO_AFFI NE* a f f , FI XEDs x , FI XEDs y , u 1 6a l p h a ) ; Sc a l eb y1 / sxa n d1 / sy, t h e nr o t a t ec o u n t e r c l o c k wi s e b yα· π/ 8 0 0 0 h . v o i dfoo_r o t s c a l e 2 ( FOO_AFFI NE* a f f , c o n s tAFF_SRC* a s ) ; Asfoo_rotscale(), b u ti n p u ts t o r e di na n AFF_SRCs t r u c t . v o i dfoo_s c a l e ( FOO_AFFI NE* a f f , FI XEDs x , FI XEDs y ) ; Sc a l eb y1 / sxa n d1 / sy v o i dfoo_s e t ( FOO_AFFI NE* a f f , FI XEDp a , FI XEDp b , FI XEDp c , FI XED p d ) ; Se tP' se l e me n t s v o i dfoo_s h e a r x ( FOO_AFFI NE* a f f , FI XEDh x ) ; Sh e a rt o p s i d er i g h tb yh x v o i dfoo_s h e a r y ( FOO_AFFI NE* a f f , FI XEDh y ) ; Sh e a rl e f t s i d ed o wnb yh y Table 10.2: a f f i n ef u n c t i o n s 10.5.1. Sample rot/scale function Myc o d ef o rao b j e c tv e r s i o no ft h es c a l e t h e n r o t a t ef u n c t i o n( àl ae q1 0 . 4 )i sg i v e nb e l o w.No t et h a ti ti sf r o mt h ec o mp u t e r ' sp o i n to f v i e w, s ot h a tsxa n dsys c a l ed o wn .Al s o , t h ea l p h aalphau s e s1 0 0 0 0 h / c i r c l e( i . e . , t h eu n i to fαi sπ/ 8 0 0 0 h=0 . 0 9 6mr a d , o r 1 8 0 / 8 0 0 0 h=0 . 0 0 5 5 ° )a n dt h es i n el u ti si n. 1 2 ff o r ma t , wh i c hi swh yt h es h i f t sb y1 2a r er e q u i r e d .Th eb a c k g r o u n dv e r s i o ni si d e n t i c a l , e x c e p ti nn a mea n dt y p e .I ft h i swe r eC++, t e mp l a t e swo u l dh a v eb e e nmi g h t yu s e f u l h e r e . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 96/331 28-03-13 Tonc : GBA Programming in rot13 void obj_aff_rotscale(OBJ_AFFINE *oaff, int sx, int sy, u16 alpha) { int ss= lu_sin(alpha), cc= lu_cos(alpha); oaff->pa= cc*sx>>12; oaff->pc= ss*sy>>12; oaff->pb=-ss*sx>>12; oaff->pd= cc*sy>>12; } Wi t ht h ei n f o r ma t i o ni nt h i sc h a p t e r , y o uk n o wmo s to fwh a ty o un e e dt ok n o wa b o u ta f f i n ema t r i c e s , s t a r t i n gwi t hwh yt h e ys h o u l db e r e f e r r e dt oaffine ma t r i c e s , r a t h e rt h a nme r e l yr o t a t i o no rr o t / s c a l eo rt h eo t h e rn a me sy o umi g h ts e ee l s e wh e r e .Yo us h o u l dn o wk n o w wh a tt h et h i n ga c t u a l l yd o e s , a n dh o wy o uc a ns e tu pama t r i xf o rt h ee f f e c t sy o uwa n t .Yo us h o u l da l s ok n o wal i t t l eb i ta b o u tf i x e d p o i n tn u mb e r sa n dl u t s( f o rmo r e , l o o ki nt h ea p p e n d i c e s )a n dwh yt h e y ' r eGo o dTh i n g s ;i fi th a d n ' tb e e nc l e a rb e f o r e , y o us h o u l db e a wa r et h a tt h ec h o i c eo ft h ed a t at y p e sy o uu s ea c t u a l l ymatters, a n dy o us h o u l dn o tj u s tu s et h ef i r s tt h i n gt h a tc o me sa l o n g . Wh a th a sn o tb e e nd i s c u s s e dh e r ei sh o wy o ua c t u a l l ys e t u po b j e c t sa n db a c k g r o u n d st ou s ea f f i n et r a n s f o r ma t i o n , wh i c hi swh a t t h en e x tt woc h a p t e r sa r ef o r .Fo rmo r eo na f f i n et r a n s f o r ma t i o n s , t r ys e a r c h i n gf o r‘ l i n e a ra l g e b r a ’ file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 97/331 28-03-13 Tonc : GBA Programming in rot13 11. Affine sprites Af f i n es p r i t ei n t r o d u c t i o n . Af f i n es p r i t ei n i t i a l i z a t i o n . Gr a p h i c a l a r t i f a c t s . Av e r y( a f ) f i n ed e mo . Of f c e n t e rr e f e r e n c ep o i n t sa n do b j e c tc o mb o ' s . 11.1. Affine sprite introduction Es s e n t i a l l y , affine sprites a r es t i l l s p r i t e s .Th ed i f f e r e n c ewi t hr e g u l a rs p r i t e si st h a ty o uc a np e r f o r ma na f f i n et r a n s f o r ma t i o n( h e n c e t h en a me )o nt h e mb e f o r et h er e n d e r i n gs t a g eb ys e t t i n gt h er i g h tb i t si nt h eo b j e c ta t t r i b u t e sa n df i l l i n gi nt h eP ma t r i x .Yo uc a nr e a d a b o u ta f f i n et r a n s f o r ma t i o n sa n dt h eP ma t r i xh e r e .I ti sr e q u i r e dr e a d i n gf o rt h i ss e c t i o n , a sa r et h es p r i t ea n db a c k g r o u n do v e r v i e wa n d t h er e g u l a rs p r i t ep a g e . Yo uma ywo n d e rwh e t h e rt h i si sr e a l l ywo r t has e p a r a t es e c t i o n .Th es h o r ta n s we ri sy e s .Al o n g e ra n s we ri sy e s , b e c a u s eu s i n g a f f i n es p r i t e si n v o l v e sal o tmo r ema t ht h a nr e g u l a rs p r i t e sa n dId i d n ' twa n tt of r e a ko u tt h e , e r m, ‘ ma t h e ma t i c a l l yc h a l l e n g e d ’ .Th e s e c t i o no nr e g u l a rs p r i t e sc a ns t a n do ni t so wna n dy o uc a nu s ei ti nb l i s s f u l i g n o r a n c eo ft h en a s t yma t ht h a ti tr e q u i r e df o ra f f i n es p r i t e s . I nt h i sc h a p t e rwe ' l l s e eh o wt os e t u po b j e c tt ou s ea f f i n et r a n s f o r ma t i o n s .Th i si ni t s e l fi sr a t h e re a s y .Al s od i s c u s s e da r ean u mb e r o fp o t e n t i a l g r a p h i c a l p r o b l e msy o umi g h tr u ni n t os o o n e ro rl a t e r–o n eo ft h e ma l mo s ti mme d i a t e l y , a c t u a l l y –a n dh o wt oc o r r e c tt h e s p r i t e ' sp o s i t i o nt oma k ei ts e e ml i k et h et r a n s f o r ma t i o n ' so r i g i ni sa ta na r b i t r a r yp o i n t .An d , a su s u a l , t h e r ewi l l b ed e mo c o d ei l l u s t r a t i n g t h ev a r i o u st o p i c sr a i s e di nt h i sc h a p t e r . 11.2. Affine sprite initialization Tot u r nar e g u l a rs p r i t ei n t oa na f f i n es p r i t ey o un e e dt od ot wot h i n g s .Fi r s t , s e tOBJ_ATTR.attr0{ 8 }t oi n d i c a t et h i si saa f f i n e s p r i t e .Se c o n d , p u tan u mb e rb e t we e n0a n d3 1i n t oOBJ_ATTR.attr1{ 8 C} .Th i sn u mb e ri n d i c a t e swh i c ho ft h e3 2Ob j e c tAf f i n e Ma t r i c e s( OBJ _AFFI NEs t r u c t u r e s )s h o u l db eu s e d .I nc a s ey o u ' v ef o r g o t , t h eOBJ_AFFINEl o o k sl i k et h i s : typedef struct OBJ_AFFINE { u16 fill0[3]; s16 pa; u16 fill1[3]; s16 pb; u16 fill2[3]; s16 pc; u16 fill3[3]; s16 pd; } ALIGN4 OBJ_AFFINE; Th esigned 1 6 b i tme mb e r spa, pb, pca n dpda r e8 . 8f i x e dp o i n tn u mb e r st h a tf o r mt h ea c t u a l ma t r i x , wh i c hIwi l l r e f e rt oa sP, i n c o r r e s p o n d e n c ewi t ht h ee l e me n t s ' n a me s .Fo rmo r ei n f o r ma t i o na b o u tt h i sma t r i x , g ot ot h ea f f i n ema t r i xs e c t i o n .Dos on o wi fy o u h a v e n ' ta l r e a d y , b e c a u s eI ' mn o tg o i n gt or e p e a ti th e r e .I fa l l y o ua r ea f t e ri sas i mp l es c a l e t h e n r o t a t ema t r i x , t r yt h i s : f o raz o o mb ys x a n ds o l l o we db yac o u n t e r c l o c k wi s er o t a t i o nb yα, t h ec o r r e c tma t r i xi st h i s : yf P= pa pb c os ( α)/sx −s i n ( α)/sx = pc pd s i n ( α)/sy c os ( α)/sy No t et h a tt h eo r i g i no ft h et r a n s f o r ma t i o ni scenter o ft h es p r i t e , n o tt h et o p l e f tc o r n e r .Th i si swo r t hr e me mb e r i n gi fy o uwa n tt oa l i g n y o u rs p r i t ewi t ho t h e ro b j e c t s , wh i c hwe ' l l d ol a t e r . Essential affine sprite steps Se t u pa no b j e c ta su s u a l : l o a dg r a p h i c sa n dp a l e t t e , s e tREG_DI SPCNT, s e t u pa nOAM e n t r y . Se tb i t8o fa t t r i b u t e0t oe n a b l ea f f i n i t yf o rt h a to b j e c t , a n dc h o o s ea no b j e c ta f f i n ema t r i xt ou s e( a t t r i b u t e1 , b i t s8 1 2 ) . Se tt h a to b j a f f i n ema t r i xt os o me t h i n go t h e rt h a na l l z e r o e s , f o re x a mp l et h ei d e n t i t yma t r i x . 11.3. Graphical artifacts 11.3.1. The clipping and discretization artifacts Th ep r o c e d u r et h a tt h eGBAu s e sf o rd r a wi n gs p r i t e si sa sf o l l o ws : t h es p r i t ef o r msar e c t a n g l eo nt h es c r e e nd e f i n e db yi t ss i z e .To file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 98/331 28-03-13 Tonc : GBA Programming in rot13 p a i n tt h es c r e e np i x e l si nt h a ta r e a( q)u s e st e x t u r e p i x e l p, wh i c hi sc a l c u l a t e dv i a : (11.1) p − p0=P ·( q − q0) , wh e r ep0a n dq0a r et h ec e n t e r so ft h es p r i t ei nt e x t u r ea n ds c r e e ns p a c e , r e s p e c t i v e l y .Th ec o d eb e l o wi se s s e n t i a l l ywh a tt h eh a r d wa r e d o e s ;i ts c a n st h es c r e e n r e c t a n g l eb e t we e np l u sa n dmi n u st h eh a l f wi d t ha n dh a l f h e i g h t( h a l f s i z e sb e c a u s et h ec e n t e ri st h er e f e r e n c e p o i n t ) , c a l c u l a t e st h et e x t u r e p i x e l a n dp l o t st h a tc o l o r . // pseudocode for affine objects hwidth= width/2; // half-width of object screen canvas hheight= hheight/2; // half-height of object screen canvas for(iy=-hheight; iy<hheight; iy++) { for(ix=-hwidth; ix<hwidth; ix++) { px= (pa*ix + pb*iy)>>8; // get x texture coordinate py= (pc*ix + pd*iy)>>8; // get y texture coordinate color= GetPixel(px0+px, py0+py); // get color from (px,py) SetPixel(qx0+ix, qy0+iy, color); // set color to (qx, qy) } } Th i sh a st woma i nc o n s e q u e n c e s , t h ec l i p p i n ga r t i f a c ta n dad i s c r e t i z a t i o na r t i f a c t . Th eclipping artifact i sc a u s e db ys c a n n i n go n l yo v e rt h er e c t a n g l eon-screen.Bu ta l mo s ta l l t r a n s f o r ma t i o n swi l l c a u s et h et e x t u r ep i x e l st oe x c e e dt h a tr e c t a n g l e , a n dt h ep i x e l so u t s i d et h er e c t a n g l e wi l l n o tb er e n d e r e d .Fi g1 1 . 1s h o wst h es c r e e nr e c t( g r e y , b l u eb o r d e r )a n dar o t a t e do b j e c t( i n s i d et h e r e db o r d e r ) .Th ep a r t st h a te x t e n dt h eb l u eb o r d e r l i n e swi l l n o tb ec u to f f . Ast h i si sa no b v i o u sf l a w, t h e r ei so fc o u r s eawa ya r o u n di t : s e tt h es p r i t e ' sa f f i n emo d et odoublesized affine ( ATTR0_AFF_DBL, OBJ_ATTR.attr0{ 8 , 9 } ) .Th i swi l l d o u b l et h es c r e e nr a n g eo fv a l i d qc o o r d i n a t e s , s oy o u ' dh a v e+a n d−t h ewi d t ha n dh e i g h tt op l a ywi t hi n s t e a do ft h eh a l f s i z e s .Th i s d o u b l e( we l l q u a d r u p l e , r e a l l y )a r e ame a n st h a ty o uc a ns a f e l yr o t a t eas p r i t ea st h ema x i mu md i s t a n c e f r o mt h ec e n t e ri s½√2≈0 . 7 0 7 .Ofc o u r s e , y o uc a ns t i l l g e tt h ec l i p p i n ga r t i f a c ti fy o us c a l eu pb e y o n d t h ed o u b l e dr a n g e s .Al s o , n o t et h a tt h es p r i t e s ' o r i g i ni ss h i f t e dt ot h ec e n t e ro ft h i sr e c t a n g l e , s ot h a tq0i s Fig 11.1:apar t i a l l yde f a ng e d me t r o i d , s i n c et h epa r t so ut s i de n o wo n ef u l l s p r i t e s i z ea wa yf r o mt h et o p l e f tc o r n e r . t h e b l u e s q u a r e a r e c l i p p e d o f f . Th ed o u b l e s i z ef l a ga l s oh a sas e c o n du s e .Orp e r h a p sIs h o u l ds a ymi s u s e .I fy o us e ti tf o rar e g u l a r s p r i t e , i twi l l b eh i d d e n .Th i si sa na l t e r n a t i v ewa yt oh i d eu n u s e ds p r i t e s . Th es e c o n da r t i f a c t , i fy o uc a nc a l l i tt h a t , i sadiscretization a r t i f a c t .Th i si samo r es u b t l ep o i n tt h a nt h ec l i p p i n ga r t i f a c ta n dy o umi g h t n o te v e ne v e rn o t i c ei t .Th ep r o b l e mh e r ei st h a tt h et r a n s f o r ma t i o nd o e s n ' ta c t u a l l yt a k ep l a c ea tt h ec e n t e ro ft h eo b j e c t , b u ta tt h e center pixel, r o u n d e du p .Asa ne x a mp l e , l o o ka tf i g1 1 . 2 .He r eweh a v ean u mb e r l i n ef r o m0t o8 ;a n di nb e t we e nt h e m8p i x e l sf r o m 0t o7 .Th en u mb e ra tt h ec e n t e ri s4 , o fc o u r s e .Th ec e n t r a l p i x e l i s4a swe l l , h o we v e ri t sl o c a t i o ni sa c t u a l l yh a l f wa yb e t we e nn u mb e r s 4a n d5 .Th i sc r e a t e sa nu n b a l a n c eb e t we e nt h en u mb e ro fp i x e l so nt h el e f ta n do nt h er i g h t . Th ec e n t e rp i x e l i st h er e f e r e n c ep o i n to ft h et r a n s f o r ma t i o na l g o r i t h m, wh i c hh a si n d i c e s( i x , i y )=( 0 , 0 ) .Fi l l t h a ti n t ot h ee q u a t i o n s a n dy o u ' l l s e et h a tt h i si si n v a r i a n tu n d e rt h et r a n s f o r ma t i o n , e v e nt h o u g hma t h e ma t i c a l l yi ts h o u l dn o t .Th i sh a sc o n s e q u e n c e sf o rt h e o f f s e t s , wh i c ha r ec a l c u l a t e df r o mt h ep i x e l , n o tt h ep o s i t i o n .I nf i g1 1 . 2 , t h e r ea r e4p i x e l so nt h el e f t , b u to n l y3o nt h er i g h t .Ami r r o r i n g o p e r a t i o nt h a two u l dc e n t e ro np i x e l 4wo u l de f f e c t i v e l ymo v et h es p r i t eo n ep i x e l t ot h er i g h t . Fi g1 1 . 3s h o wsh o wt h i sa f f e c t sr o t a t i o n s .I td i s p l a y sl i n e se v e r yg r e yg r i d l i n e se v e r y8p i x e l sa n da1 6 x 1 6s p r i t eo fab o x .No t et h a t a tt h es t a r tt h er i g h ta n dl e f ts i d e sd on o tl i eo nt h eg r i d l i n e s , b e c a u s et h es p r i t e ' swi d t ha n dh e i g h ti s1 6 , n o t1 7 .Th eo t h e rf i g u r e sa r e r o t a t i o n si ni n c r e me n t so f9 0 ° , wh i c hg i v e sn i c er o u n dn u mb e r si nt h ema t r i x .Wh e nr o t a t i n g , t h ec e n t e rp i x e l ( t h er e dd o ti nt h emi d d l e ) s t a y si nt h es a mep o s i t i o n , a n dt h er e s tr o t a t ea r o u n di t , a n dt h i sp r o c e s swi l l c a r r yt h ee d g e so u to ft h ed e s i g n a t e d1 6 x 1 6b o xo ft h e s p r i t e( t h ed a s h e dl i n e s ) . Fig 11.2: pi x e l sa r eb e t we e n , n o to n , c o o r d i n a t e s . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm Fig 11.3: Ro t a t i o n si n9 0 °i n c r e me n t s . 99/331 28-03-13 Tonc : GBA Programming in rot13 The offsets measure distance from the center pixel, not center position. Th eo f f s e t st h a ta r ec a l c u l a t e df r o mt h ea f f i n ema t r i xu s et h ed i s t a n c e sf r o mt h ec e n t e rp i x e l ( w/ 2 , h/ 2 ) , n o tt h ec e n t e rp o i n t . Ass u c h , t h e r ei sah a l fap i x e l d e v i a t i o nf r o mt h ema t h e ma t i c a l t r a n s f o r ma t i o n , wh i c hma yr e s u l ti na±p i x e l o f f s e tf o rt h e s p r i t ea sawh o l ea n dl o s tt e x t u r ee d g e s . 11.3.2. The wrapping artifact Ap a r tf r o mt h ec l i p p i n ga r t i f a c t , t h e r es e e mst ob ea n o t h e r ;o n et h a tIh a v ea c t u a l l yn e v e rs e e nme n t i o n e da n y wh e r e .I t ' swh a tIc a l l t h e wr a p p i n ga r t i f a c t .Asy o uk n o w, t h ep o s i t i o nf o rs p r i t e si sg i v e ni na9 b i txv a l u ea n da n8 b i tyv a l u e , wh i c hv a l u e swr a pa r o u n dt h e s c r e e n .Fo rx, y o uc a nj u s ti n t e r p r e tt h i sa sh a v i n gt h e[ 2 5 6 , 2 5 5 ]r a n g e .Fo ry v a l u e s , y o uc a n ' tr e a l l yd ot h a tb e c a u s et h et o pv a l u ef o r as i g n e d8 b i ti n t e g e ri s1 2 7 , wh i c hwo u l dme a nt h a ty o u ' dn e v e rb ea b l et op u tas p r i t ea tt h eb o t t o m3 2l i n e s .Bu ts i n c et h ev a l u e swr a p a r o u n d , i ta l l wo r k so u ti nt h ee n da n y wa y .Wi t ho n ee x c e p t i o n . Th e r e ' sn e v e ra n yt r o u b l ewi t hr e g u l a rs p r i t e s , a n dh a r d l ya n yf o ra f f i n es p r i t e s ;t h eo n ee x c e p t i o ni swh e ny o uh a v ea6 4 x 6 4o r 3 2 x 6 4a f f i n es p r i t ewi t ht h ed o u b l es i z ef l a gs wi t c h e do n .Su c has p r i t eh a sab o u n d i n gb o xo f1 2 8 x 1 2 8 .No wt h e r ea r et h r e ed i f f e r e n t wa y so fi n t e r p r e t i n gt h eme a n i n go fy >1 2 8 : 1 .Fu l l wr a p : t h et o po ft h es p r i t ewo u l ds h o wa tt h eb o t t o mo ft h es c r e e n , a n dv i c ev e r s a . 2 .Po s i t i v ep r e c e d e n c e : c o n s i d e rt h e[ 1 2 8 , 1 5 9 ]r a n g ea si n d i c a t i v eo ft h eb o t t o mo ft h es c r e e n , a n df o r g e tt h ewr a p . 3 .Ne g a t i v ep r e c e d e n c e : i fy v a l u ewo u l dma k et h es p r i t ea p p e a rp a r t i a l l ya tt h et o p , c o n s i d e ri tt ob en e g a t i v e , a g a i nn e g l e c t i n g t h ewr a p . Asi th a p p e n s , t h eGBAu s e st h et h i r di n t e r p r e t a t i o n .I no t h e rwo r d s , i tu s e s // pseudo code if(oam.y + bbox_height > 256) oam.y -= 256; No t e , b yt h ewa y , t h a tVBAa n dBo y c o t t Ad v a n c eb o t hu s ei n t e r p r e t a t i o n#2 , wh i c hma ys e e mmo r el o g i c a l , b u ti si n c o r r e c t .Asy o uc a n t e l l , i tc a no n l yh a p p e nwi t ha3 2 x 6 4o r6 4 x 6 4 , d o u b l e s i z e ds p r i t e , a n de v e nt h e ny o u ' l l o n l yn o t i c ei tu n d e rv e r ys p e c i f i cc o n d i t i o n s , n a me l yi ft h et r a n s f o r me ds p r i t eh a sv i s i b l ep i x e l si n s i d et h et o p3 2l i n e so ft h eb o u n d i n gb o x .I nt h ec a s et h a ty o uh a v et h i sp r o b l e m, a s f a ra sIc a nt e l l t h eo n l ywa yt og e tt h es p r i t es h o wi n ga tt h eb o t t o mo ft h es c r e e ni si fy o ur e d u c et h eh e i g h tt o3 2f o rt h et i meb e i n g . 11.4. A very (af)fine demo Ih a v ear e a l l yi n t e r e s t i n gd e mof o ry o ut h i st i mec a l l e dobj_aff.I tf e a t u r e san o r ma l ( b o x e d )me t r o i d , wh i c hc a nb es c a l e d , r o t a t e d a n ds c a l e d .Be c a u s et h e s et r a n s f o r ma t i o n sa r ea p p l i e dt ot h ecurrent s t a t eo ft h ema t r i x , y o uc a ne n du pwi t he v e r yk i n do fa f f i n e ma t r i xp o s s i b l eb yc o n c a t e n a t i n gt h ed i f f e r e n tma t r i c e s .Th ec o n t r o l sa r ea sf o l l o ws : t a t e st h es p r i t eCCW a n dCW, r e s p e c t i v e l y . L,R Ro e a r st h es p r i t e . D-pad Sh DMo v e ss p r i t ea r o u n d . pad+Sel p a n d sh o r i z o n t a l l yo rv e r t i c a l l y , r e s p e c t i v e l y . A,B Ex r i n k sh o r i z o n t a l l yo rv e r t i c a l l y , r e s p e c t i v e l y .( Ir a no u to fb u t t o n s , s oh a dt od oi tl i k et h i s ) . A,B+Sel Sh To g g l e sd o u b l e s i z ef l a g .No t et h a ta )t h ec o r n e r so far o t a t e ds p r i t ea r en ol o n g e rc l i p p e da n db )t h ep o s i t i o n Start s h i f t sb y1 / 2s p r i t es i z e . Start+Sel Re s e t sP t on o r ma l . n t r o l b u t t o n( s e eA, Ba n dSt a r t ) . Select Co Th ei n t e r e s t i n gp o i n to fs e e i n gt h et r a n s f o r ma t i o n sb a c kt ob a c ki st h a ty o uc a na c t u a l l ys e et h ed i f f e r e n c eb e t we e n , f o re x a mp l e , a s c a l i n gf o l l o we db yar o t a t i o n( A=S· R) , a n dar o t a t e t h e n s c a l e( A=R· S) .Fi g s1 1 . 4a n d1 1 . 5s h o wt h i sd i f f e r e n c ef o ra4 5 °r o t a t i o na n d a2 ×v e r t i c a l s c a l e .Al s o , n o t et h a tt h ec o r n e r sa r ec u to f fh e r e : t h ec l i p p i n ga r t i f a c ta two r k–e v e nt h o u g hI ' v ea l r e a d ys e tt h ed o u b l e s i z ef l a gh e r e . Fig 11.4: obj_aff, v i aS ( 1 , 2 ) , t h e nR( 4 5 ° ) Fig 11.5: obj_aff, v i aR( 4 5 ° ) , t h e nS ( 1 , 2 ) Th ef u l l s o u r c ec o d ef o rt h eobj_affd e moi sg i v e nb e l o w.I t ' sq u i t el o n g , mo s t l yb e c a u s eo ft h ea mo u n to fc o d en e c e s s a r yf o r ma n a g i n gt h ed i f f e r e n ta f f i n es t a t e st h a tc a nb ea p p l i e d .Th ef u n c t i o n st h a ta c t u a l l yd e a l wi t ha f f i n es p r i t e sa r einit_metr(), file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 100/331 28-03-13 Tonc : GBA Programming in rot13 get_aff_new()a n dp a r to ft h eg a mel o o pi nobjaff_test();t h er e s ti se s s e n t i a l l yf l u f fr e q u i r e dt oma k i n gt h ewh o l et h i n g wo r k . // obj_aff.c #include <tonc.h> #include <stdio.h> #include "metr.h" OBJ_ATTR obj_buffer[128]; OBJ_AFFINE *obj_aff_buffer= (OBJ_AFFINE*)obj_buffer; // affine transformation constants and variables enum eAffState { AFF_NULL=0, AFF_ROTATE, AFF_SCALE_X, AFF_SCALE_Y, AFF_SHEAR_X, AFF_SHEAR_Y, AFF_COUNT }; // 'speeds' of transformations const int aff_diffs[AFF_COUNT]= { 0, 128, 4, 4, 4, 4 }; // keys for transformation direction const int aff_keys[AFF_COUNT]= { 0, KEY_L, KEY_SELECT, KEY_SELECT, KEY_RIGHT, KEY_UP }; int aff_state= AFF_NULL, aff_value= 0; void init_metr() { // Places the tiles of a 4bpp metroid sprite into LOW obj VRAM memcpy32(tile_mem[4], metr_boxTiles, metr_boxTilesLen/4); memcpy32(pal_obj_mem, metrPal, metrPalLen/4); // Set up main metroid obj_set_attr(obj_buffer, ATTR0_SQUARE | ATTR0_AFF, // Square affine sprite ATTR1_SIZE_64 | ATTR1_AFF_ID(0), // 64x64, using obj_aff[0] 0 | 0); // palbank 0, tile 0 obj_set_pos(obj_buffer, 96, 32); obj_aff_identity(&obj_aff_buffer[0]); // Set up shadow metroid obj_set_attr(&obj_buffer[1], ATTR0_SQUARE | ATTR0_AFF, // Square affine sprite ATTR1_SIZE_64 | ATTR1_AFF_ID(31), // 64x64, using obj_aff[0] ATTR2_PALBANK(1) | 0); // palbank 1, tile 0 obj_set_pos(&obj_buffer[1], 96, 32); obj_aff_identity(&obj_aff_buffer[31]); oam_update_all(); } int get_aff_state() { if(key_is_down(KEY_L | KEY_R)) return AFF_ROTATE; if(key_is_down(KEY_A)) return AFF_SCALE_X; if(key_is_down(KEY_B)) return AFF_SCALE_Y; if(key_is_down(KEY_LEFT | KEY_RIGHT)) return AFF_SHEAR_X; if(key_is_down(KEY_UP | KEY_DOWN)) return AFF_SHEAR_Y; return AFF_NULL; } void get_aff_new(OBJ_AFFINE *oa) { int diff= aff_diffs[aff_state]; file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 101/331 28-03-13 Tonc : GBA Programming in rot13 aff_value += (key_is_down(aff_keys[aff_state]) ? diff : -diff); switch(aff_state) { case AFF_ROTATE: // L rotates left, R rotates right aff_value &= SIN_MASK; obj_aff_rotate(oa, aff_value); break; case AFF_SCALE_X: // A scales x, +SELECT scales down obj_aff_scale_inv(oa, (1<<8)-aff_value, 1<<8); break; case AFF_SCALE_Y: // B scales y, +SELECT scales down obj_aff_scale_inv(oa, 1<<8, (1<<8)-aff_value); break; case AFF_SHEAR_X: // shear left and right obj_aff_shearx(oa, aff_value); break; case AFF_SHEAR_Y: // shear up and down obj_aff_sheary(oa, aff_value); break; default: // shouldn't happen obj_aff_identity(oa); } } void objaff_test() { OBJ_ATTR *metr= &obj_buffer[0], *shadow= &obj_buffer[1]; OBJ_AFFINE *oaff_curr= &obj_aff_buffer[0]; OBJ_AFFINE *oaff_base= &obj_aff_buffer[1]; OBJ_AFFINE *oaff_new= &obj_aff_buffer[2]; int x=96, y=32; int new_state; // oaff_curr = oaff_base * oaff_new // oaff_base changes when the aff-state changes // oaff_new is updated when it doesn't obj_aff_identity(oaff_curr); obj_aff_identity(oaff_base); obj_aff_identity(oaff_new); while(1) { key_poll(); // move sprite around if( key_is_down(KEY_SELECT) && key_is_down(KEY_DIR) ) { // move x += 2*key_tri_horz(); y += 2*key_tri_vert(); obj_set_pos(metr, x, y); obj_set_pos(shadow, x, y); new_state= AFF_NULL; } else // or do an affine transformation new_state= get_aff_state(); if(new_state != AFF_NULL) // no change { if(new_state == aff_state) // increase current transformation { get_aff_new(oaff_new); obj_aff_copy(oaff_curr, obj_aff_base, 1); obj_aff_postmul(oaff_curr, oaff_new); } else // switch to different transformation type { obj_aff_copy(oaff_base, oaff_curr, 1); obj_aff_identity(oaff_new); aff_value= 0; file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 102/331 28-03-13 Tonc : GBA Programming in rot13 } aff_state= new_state; } // START: toggles double-size flag // START+SELECT: resets obj_aff to identity if(key_hit(KEY_START)) { if(key_is_down(KEY_SELECT)) { obj_aff_identity(oaff_curr); obj_aff_identity(oaff_base); obj_aff_identity(oaff_new); aff_value= 0; } else { metr->attr0 ^= ATTR0_DBL_BIT; shadow->attr0 ^= ATTR0_DBL_BIT; } } vid_vsync(); // we only have one OBJ_ATTR, so update that obj_copy(obj_mem, obj_buffer, 2); // we have 3 OBJ_AFFINEs, update these separately obj_aff_copy(obj_aff_mem, obj_aff_buffer, 3); // Display the current matrix tte_printf("#{es;P:8,136}P = " "#{y:-7;Ps}| %04X\t%04X#{Pr;x:72}|" "#{Pr;y:12}| %04X\t%04X#{Pr;p:72,12}|", (u16)oaff_curr->pa, (u16)oaff_curr->pb, (u16)oaff_curr->pc, (u16)oaff_curr->pd); } } int main() { REG_DISPCNT= DCNT_BG0 | DCNT_OBJ | DCNT_OBJ_1D; oam_init(obj_buffer, 128); init_metr(); tte_init_chr4_b4_default(0, BG_CBB(2)|BG_SBB(28)); tte_init_con(); tte_set_margins(8, 128, 232, 160); objaff_test(); return 0; } Ma k i n gt h eme t r o i da na f f i n es p r i t ei sa l l d o n ei n s i d einit_metr().Asy o u ' v es e e nh o wb i t sa r es e tan u mb e ro ft i me sb yn o w, i t s h o u l db eu n d e r s t a n d a b l e .Th a ts a i d , d on o t et h a tIa mf i l l i n gt h ef i r s tOBJ _AFFI NE( t h eo n et h a tt h es p r i t eu s e s )t ot h ei d e n t i t yma t r i xI. I fy o uk e e pt h i sf u l l yz e r o e d o u t , y o u ' l l j u s te n du pwi t ha6 4 x 6 4 p i x e l r e c t a n g l eo fu n i f o r mc o l o r .Re me mb e rt h a tP c o n t a i n sp i x e l o f f s e t s ; i ft h e y ' r ea l l z e r o , t h e r ei sn oo f f s e ta n dt h eo r i g i n ' sc o l o ri su s e df o rt h ewh o l et h i n g .I ne s s e n c e , t h es p r i t ei ss c a l e du pt oi n f i n i t y . Tob ef r a n kt h o u g h , c a l l i n gobj_aff_identity()i s n ' tn e c e s s a r ya f t e rac a l l t ooam_init(), a st h a ti n i t i a l i z e st h ema t r i c e s a swe l l .St i l l , y o un e e dt ob ea wa r eo fp o t e n t i a l p r o b l e ms . Th a t ' st h es e t u p , n o wf o rh o wt h ed e mod o e swh a ti td o e s .Ata n yg i v e nt i me , y o uwi l l h a v es o met r a n s f o r ma t i o nma t r i x , P.Byp r e s s i n g ab u t t o n( o rn o t ) , as ma l l t r a n s f o r ma t i o no ft h ec u r r e n ts t a t ewi l l b ep e r f o r me d , v i ama t r i xmu l t i p l i c a t i o n . P new =P ol ·D−1, d wh e r eD i se i t h e ras ma l l r o t a t i o n( R) , s c a l i n g( S)o rs h e a r( H) .Oran o o p( I) .Ho we v e r , t h e r ei sal i t t l eh i t c hh e r e .Th i swo u l dwo r k n i c ei nt h e o r y , b u ti npractice, i two n ' two r kwe l l b e c a u s et h ef i x e dp o i n tma t r i xmu l t i p l i c a t i o n swi l l r e s u l ti nu n a c c e p t a b l er o u n d o f f e r r o r sv e r yv e r yq u i c k l y .Fo r t u n a t e l y , a l l t h e s et r a n s f o r ma t i o n sh a v et h ec o n v e n i e n tp r o p e r t yt h a t D( a ) · D( b )=D( c ) . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 103/331 28-03-13 Tonc : GBA Programming in rot13 Th a ti st os a y , mu l t i p l es ma l l t r a n s f o r ma t i o n swo r ka so n eb i go n e .Al l y o uh a v et od oi sk e e pt r a c ko ft h ec u r r e n tc h o s e nt r a n s f o r ma t i o n ( t h ev a r i a b l eaff_state, i nget_aff_state()) , mo d i f yt h es t a t ev a r i a b l e( aff_value) , t h e nc a l c u l a t ef u l l t r a n s f o r ma t i o n ma t r i x( get_aff_new())a n da p p l yt h a t( wi t hobj_aff_postmul()) .Wh e nad i f f e r e n tt r a n s f o r ma t i o nt y p ei sc h o s e n , t h e c u r r e n tma t r i xi ss a v e d , t h es t a t ev a l u ei sr e s e ta n dt h ewh o l et h i n gc o n t i n u e swi t ht h a ts t a t eu n t i l y e ta n o t h e ri sp i c k e d .Th ema j o r i t yo f t h ec o d ei sf o rk e e p i n gt r a c ko ft h e s ec h a n g e s ;i t ' sn o tp r e t t y , b u ti tg e t st h ej o bd o n e . 11.5. Off-center reference points and object combos Asme n t i o n e de a r l i e r , a f f i n es p r i t e sa l wa y su s et h e i rc e n t e r sa sa f f i n eo r i g i n s , b u tt h e r ea r e t i me swh e no n emi g h twa n tt ou s es o me t h i n ge l s et or o t a t ea r o u n d–t ou s ea n o t h e rp o i n ta s t h er e f e r e n c ep o i n t .No w, y o uc a n ' ta c t u a l l yd ot h i s , b u ty o uc a nma k ei tlook a si fy o uc a n . Tod ot h i s , In e e dt oe x p l a i naf e wt h i n g sa b o u twh a tIl i k et oc a l l a n c h o r i n g .Th eanchor i s t h ep o s i t i o nt h a ti ss u p p o s e dt or e ma i n‘ f i x e d ’ ;t h es p o twh e r et h et e x t u r e( i nt h i sc a s et h e o b j e c t )i sa n c h o r e dt ot h es c r e e n . Fo ra n c h o r i n g , y o ua c t u a l l yn e e do n es e to fc o o r d i n a t e sf o re a c hc o o r d i n a t e s p a c e y o u ' r eu s i n g .I nt h i sc a s e , t h a t ' st wo : t h et e x t u r es p a c ea n dt h es c r e e ns p a c e .Le t ' sc a l l t h e s ep o i n t sp0a n dq0, r e s p e c t i v e l y .Wh e r et h e s ea c t u a l l yp o i n tfrom i sl a r g e l yi mma t e r i a l , Fig 11.6:r o t a t i o no fo b j e c ta r o u n da nof f c e nt e r b u tf o rc o n v e n i e n c e ' s a k el e t ' su s et h es c r e e na n dt e x t u r eo r i g i n sf o rt h i s .Th e s ep o i n t sa r e poi n t . o n l yt h es t a r t .I nt o t a l , t h e r ea r eseven v e c t o r st h a twen e e dt ot a k ei n t oa c c o u n tf o rt h ef u l l p r o c e d u r e , a n dt h e ya r ea l l d e p i c t e di nf i g1 1 . 6 .Th e i rme a n i n g sa r ee x p l a i n e di nt h et a b l eb e l o w. point description c h o r si nt e x t u r ea n ds c r e e ns p a c e . p0, q0 An j e c tc e n t e r si nt e x t u r ea n ds c r e e ns p a c e .Wi t ht h eo b j e c ts i z e s , w, h ) , weh a v ecp=½s a n dcq=ms , wh e r em cp, cq Ob s =( i s½o r1 , d e p e n d i n go nt h ed o u b l e s i z ef l a g . s t a n c e sb e t we e no b j e c tc e n t e r sa n da n c h o r s .Byd e f i n i t i o n , rp, rq Di rp=P· rq De s i r e do b j e c tc o o r d i n a t e s . x Ye s , i ti sawh o l el o to fv e c t o r s , b u tf u n n i l ye n o u g h , mo s ta r ea l r e a d yk n o wn .Th ec e n t e rp o i n t s( cpa n dcq)c a nb ed e r i v e df r o mt h e o b j e c t ss i z ea n dd o u b l e s i z es t a t u s , t h ea n c h o r sa r ek n o wni na d v a n c eb e c a u s et h o s ea r et h ei n p u tv a l u e s , a n drpa n drqf i tt h eg e n e r a l e q u a t i o nf o rt h ea f f i n et r a n s f o r ma t i o n , e q1 , s ot h i sl i n k st h et wos p a c e s .Al l t h a t ' sl e f tn o wi st owr i t ed o wna n ds o l v et h es e to f e q u a t i o n s . x +cq+rq=q0 (11.2) c p+r p =p0 rp =P· rq Th r e ee q u a t i o n swi t ht h r e eu n k n o wn s , me a n si ti ss o l v a b l e .Iwo n ' tp o s tt h ee n t i r ed e r i v a t i o nb e c a u s et h a t ' sn o ta l l t h a td i f f i c u l t ;wh a ty o u s e ei ne q1 1 . 3i st h ee n dr e s u l ti nt h emo s tu s a b l ef o r m. (11.3) x =q0−ms −P −1·( p0−½s) Th er i g h t h a n ds i d eh e r eh a st h r e es e p a r a t ev e c t o r s , t woo fwh i c ha r ep a r to ft h ei n p u t , as c a l i n gf l a gf o rt h ed o u b l e s i z emo d e , a n dt h e i n v e r t e da f f i n ema t r i x .Ye s , Id i ds a yi n v e r t e d .Th i si sh e r eb e c a u s et h et r a n s l a t i o n st op o s i t i o nt h eo b j e c tc o r r e c t l ymo s t l yt a k ep l a c ei n s c r e e n s p a c e .Th ewh o l et e r mu s i n gi ti sme r e l yrq, t h et r a n s f o r me dd i f f e r e n c eb e t we e na n c h o ra n dc e n t e ri nt e x t u r es p a c e , wh i c hy o u n e e df o rt h ef i n a l c o r r e c t i o n . No w, t h i sma t r i xi n v e r s i o nme a n st wot h i n g s .Fi r s t , t h a ty o uwi l l l i k e l yh a v et os e t u ptwo ma t r i c e s : t h ea f f i n ema t r i xi t s e l f , a n di t s i n v e r s e .Fo rg e n e r a l ma t r i c e s , t h i smi g h tt a k eawh i l e , e s p e c i a l l ywh e nc o n s i d e r i n gt h a ti fy o uwa n ts c a l i n g , y o uwi l l h a v et od oad i v i s i o n s o me wh e r e .Se c o n d l y , b e c a u s ey o uo n l yh a v e1 6 b i t sf o rt h ema t r i xe l e me n t s , t h ei n v e r s ewo n ' tb et h eexact i n v e r s e , me a n i n gt h a t a l i g n i n gt h eo b j e c t se x a c t l ywi l l b ed i f f i c u l t , i fn o ta c t u a l l yi mp o s s i b l e .Th i si sp r e t t ymu c hg u a r a n t e e db yt h eh a r d wa r ei t s e l fa n dI ' l l r e t u r n t ot h i sp o i n tl a t e ro n .Fo rn o w, l e t ' sl o o ka taf u n c t i o ni mp l e me n t i n ge q1 1 . 3i nt h ec a s eo fa2 wa ys c a l i n gf o l l o we db yar o t a t i o n . // === in tonc_types.h === // This is the same struct that's used in BgAffineSet, // where it is called BGAffineSource, even though its uses go // beyond just backgrounds. typedef struct tagAFF_SRC_EX { s32 tex_x, tex_y; // vector p0: anchor in texture space (.8f) s16 scr_x, src_y; // vector q0: anchor in screen space (.0f) s16 sx, sy; // scales (Q.8) u16 alpha; // CCW angle ( integer in [0,0xFFFF] ) } AFF_SRC_EX; file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 104/331 28-03-13 Tonc : GBA Programming in rot13 // === in tonc_core.c === // Usage: oam_sizes[shape][size] is (w,h) const u8 oam_sizes[3][4][2]= { { { 8, 8}, {16,16}, {32,32}, {64,64} }, { {16, 8}, {32, 8}, {32,16}, {64,32} }, { { 8,16}, { 8,32}, {16,32}, {32,64} }, }; // === in tonc_obj_affine.c === void obj_rotscale_ex(OBJ_ATTR *obj, OBJ_AFFINE *oa, AFF_SRC_EX *asx) { int sx= asx->sx, sy= asx->sy; int sina= lu_sin(asx->alpha)>>4, cosa= lu_cos(asx->alpha)>>4; // (1) calculate P oa->pa= sx*cosa>>8; oa->pc= sy*sina>>8; oa->pb= -sx*sina>>8; oa->pd= sy*cosa>>8; // (2) set-up and calculate A= P^-1 // sx = 1/sx, sy = 1/sy (.12f) sx= Div(1<<20, sx); if(sx != sy) sy= Div(1<<20, sy); else sy= sx; FIXED aa, ab, ac, ad; // .8f aa= sx*cosa>>12; ab= sy*sina>>12; ac= -sx*sina>>12; ad= sy*cosa>>12; // (3) get object size sx= oam_sizes[obj->attr0>>14][obj->attr1>>14][0]; sy= oam_sizes[obj->attr0>>14][obj->attr1>>14][1]; // (4) calculate dx = q0 - ms - A*(p0-s/2) int dx= asx->src_x, dy= asx->src_y; // .0f if(obj->attr0&ATTR0_DBL_BIT) { dx -= sx; dy -=sy; } else { dx -= sx>>1; dy -= sy>>1; } sx= asx->tex_x - (sx<<7); sy= asx->tex_y - (sy<<7); dx -= (aa*sx + ab*sy)>>16; dy -= (ac*sx + ad*sy)>>16; // .8f // .8f // .0 - (.8f*.8f/.16f) // .0 - (.8f*.8f/.16f) // (5) update OBJ_ATTR obj_set_pos(obj, dx, dy); } Th eAFF_SRC_EXs t r u c ta n doam_sizesa r r a y sa r es u p p o r t i n ge n t i t i e so ft h ef u n c t i o nt h a td o e st h ep o s i t i o n i n g , wh i c hi s obj_rotscale_ex().Th i sc r e a t e st h ea f f i n ema t r i x( pa-pd) , a n dc a r r i e so u ta l l t h en e c e s s a r ys t e p sf o re q1 1 . 3 , n a me l yc r e a t e t h ei n v e r s ema t r i xA ( aa-ad) , c a l c u l a t ea l l t h eo f f s e t sa n dc o r r e c t i n gf o rt h es i z e s , a n df i n a l l yu p d a t i n gt h eOBJ _ATTR.No t et h a tt h e f i x e dp o i n ta c c u r a c yv a r i e sal o t , s oi ti si mp o r t a n tt oc o mme n to f t e no nt h i s AsIs a i d , t h i si sn o tap a r t i c u l a r l yf a s tf u n c t i o n ;i tt a k e sr o u g h l yas c a n l i n ewo r t ho fc y c l e s .I fy o un e e dmo r es p e e d , Ia l s oh a v ea t h u mb a s mv e r s i o nwh i c hi sa b o u t4 0 %f a s t e r . 11.5.1. Affine object combo demo Th ed e mof o rt h i ss e c t i o n , oacombo, wi l l d i s p l a yt h r e ev e r s i o n so fe s s e n t i a l l yt h es a meo b j e c t , n a me l yt h ec i r c l eo f1 1 . 7 . Th ed i f f e r e n c eb e t we e nt h e mi si nh o wt h e ya r ec o n s t r u c t e d 0 .13 2 x 3 2 po b j e c t , f u l l c i r c l e . 1 .23 2 x 1 6 po b j e c t s , t wos e mi c i r c l e s . 2 .41 6 x 1 6 po b j e c t s , f o u rq u a r t e r c i r c l e s . Th ep o i n to ft h i sd e mowi l l b et or o t a t et h e ma n dp o s i t i o nt h ec o mp o n e n t so ft h ec o mb i n e ds p r i t e s( object combos)a si f t h e ywe r eas i n g l es p r i t e .Th i sr e q u i r e so f f c e n t e ra n c h o r sa n dt h e r e f o r et i e si nn i c e l ywi t ht h es u b j e c to ft h i ss e c t i o n .To ma n a g et h ec o mb o s , Ima k eu s eo ft h ef o l l o wi n gs t r u c t . Fig 11.7: o bj e c tf o r oacombo. typedef struct OACOMBO { file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 105/331 28-03-13 Tonc : GBA Programming in rot13 OBJ_ATTR *sub_obj; // obj pointer for sub-objects POINT *sub_pos; // Local sub-object coords (.8f) int sub_count; // Number of sub-objects POINT pos; // Global position (.8f) POINT anchor; // Local anchor (.8f) s16 sx, sy; // scales (.8f) u16 alpha; // CCW angle } OACOMBO; Ea c hc o mb oi sc o mp o s e do fsub_counto b j e c t s ;sub_oei sap o i n t e rt ot h ea r r a ys t o r i n gt h e s eo b j e c t s , a n dsub_posi sap o i n t e rt o t h el i s to f( t o p l e f t )c o o r d i n a t e so ft h e s eo b j e c t s , r e l a t i v et ot h et o p l e f to ft h ef u l l s p r i t e .Th i sg l o b a l p o s i t i o ni si npos.Th ea n c h o r( i n anchor)i sa l s or e l a t i v et ot h i sp o s i t i o n .Th eg l o b a l s c r e e n a n c h o rwo u l db ea tpos+anchor, a n dt h et e x t u r e a n c h o ro fs u b o b j e c tii a tanchor-sub_pos[ii]. Th er o t a t i o nwi l l t a k ep l a c ea r o u n dt h ec e n t e ro ft h ec i r c l e , s ot h a t ' sa na n c h o ro f( 1 6 , 1 6 ) .Or , r a t h e r( 1 6 , 1 6 ) * 2 5 6b e c a u s et h e y ' r e. 8 f i x e dp o i n tn u mb e r s , b u tt h a t ' sn o ti mp o r t a n tr i g h tn o w.Fo rt h ef u l l c i r c l e , t h i swi l l b et h ec e n t e ro ft h eo b j e c t , b u ti t ' l l s t i l l n e e dt ob e c o r r e c t e df o rt h ed o u b l e s i z ef l a g .Fo rt h eo t h e rc o mb o s , t h ea n c h o rwi l l not b ea tt h ec e n t e ro ft h e i rs u b o b j e c t s . Be c a u s et h es u b o b j e c t ss h a r et h es a meP ma t r i x , i t ' db eawa s t et or e c a l c u l a t ei tt h ewh o l et i me , s oI ' mu s i n gamo d i f i e dv e r s i o no f i te s p e c i a l l yt a i l o r e dt oOACOMBOs t r u c t sc a l l e doac_rotscale().Th ec o d ei sb a s i c a l l yt h es a met h o u g h .Th eoacs[]a r r a yf o r ms t h et h r e ec o mb o s , wh i c ha r ei n i t i a l i z e da td e f i n i t i o nb e c a u s et h a tma k e st h i n g ss omu c he a s i e r .Th ef u l l c i r c l ei sa t( 1 6 , 2 0 ) , t h es e mi sa t ( 8 0 , 2 0 )a n dt h eo n ec o mp o s e do fq u a r t e rc i r c l e si sa t( 4 8 , 6 0 ) .Th eobj_data[]a r r a yc o n t a i n st h ed a t af o ro u rs e v e no b j e c t s , a n di s c o p i e dt oobj_bufferi nt h ei n i t i a l i z a t i o nf u n c t i o n .Wh i l ei ti sg e n e r a l l yt r u et h a tma g i cn u mb e r s( s u c ha su s i n gh e xf o rOAM a t t r i b u t e s )a r ee v i l , i ti sa l s ot r u et h a tt h e yr e a l l ya r e n ' tc e n t r a l t ot h i ss t o r ya n dt os p e n dal o to fs p a c eo ni n i t i a l i z i n ga l l o ft h e mi nt h e ‘ p r o p e r ’f a s h i o nma ya c t u a l l yd omo r eh a r mt h a ng o o d… t h i st i me .Ia ms t i l l u s i n g#d e f i n e sf o rt h ea n c h o ra n dar e f e r e n c ep o i n tt h o u g h , b e c a u s et h e ya p p e a rmu l t i p l et i me si nt h er e s to ft h ec o d e . // oacombo.c #include <stdio.h> #include <ton.h> #include "oac_gfx.h" #define AX #define AY #define X0 #define Y0 (16<<8) (16<<8) 120 36 // X-anchor // Y-anchor // base X // base Y // === GLOBALS ======================================================== OBJ_ATTR obj_buffer[128]; OBJ_AFFINE *obj_aff_buffer= (OBJ_AFFINE*)obj_buffer; // Obj templates const OBJ_ATTR obj_data[7]= { // obj[0] , oaff[0]: 1 full 32x32p double-affine circle { 0x0300, 0x8200, 0x0000, 0x0000 }, // obj[1-2], oaff[1]: 2 32x16p double-affine semi-circles { 0x4300, 0x8200, 0x0000, 0x0000 }, { 0x4300, 0x8200, 0x0008, 0x0000 }, // obj[3-7], oaff[1]: 4 16x16p double-affine quarter-circles { 0x0300, 0x4400, 0x0010, 0x0000 }, { 0x0300, 0x4400, 0x0014, 0x0000 }, { 0x0300, 0x4400, 0x0018, 0x0000 }, { 0x0300, 0x4400, 0x001C, 0x0000 }, }; POINT sub_pos[7]= { {0,0}, {0,0},{0,AY}, {0,0},{AX,0}, {0,AY},{AX,AY}, }; OACOMBO oacs[3]= { // full 32x32p double-affine circle { &obj_buffer[0], &sub_pos[0], 1, {(X0-48)<<8, Y0<<8}, {AX, AY}, 256, 256, 0 }, file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 106/331 28-03-13 Tonc : GBA Programming in rot13 // 2 32x16p double-affine semi-circles { &obj_buffer[1], &sub_pos[1], 2, {(X0+16)<<8, Y0<<8}, {AX, AY}, 256, 256, 0 }, // 4 16x16p double-affine quarter-circles { &obj_buffer[3], &sub_pos[3], 4, {(X0-16)<<8, (Y0+40)<<8}, {AX, AY}, 256, 256, 0 }, }; void oac_rotscale(OACOMBO *oac) { int alpha= oac->alpha; int sx= oac->sx, sy= oac->sy; int sina= lu_sin(alpha)>>4, cosa= lu_cos(alpha)>>4; // --- create P --OBJ_AFFINE *oaff= &obj_aff_buffer[BF_GET(oac->sub_obj->attr1, ATTR1_AFF_ID)]; oaff->pa= cosa*sx>>8; oaff->pb= -sina*sx>>8; oaff->pc= sina*sy>>8; oaff->pd= cosa*sy>>8; // --- create A --// sx = 1/sx, sy = 1/sy (.12f) sx= Div(1<<20, sx); if(sx != sy) sy= Div(1<<20, sy); else sy= sx; FIXED aa, ab, ac, ad; aa= sx*cosa>>12; ab= sy*sina>>12; ac= -sx*sina>>12; ad= sy*cosa>>12; // .8f // .8f int ii; OBJ_ATTR *obj= oac->sub_obj; POINT *pt= oac->sub_pos; // --- place each sub-object --for(ii=0; ii<oac->sub_count; ii++) { int dx, dy; // all .8f sx= oam_sizes[obj->attr0>>14][obj->attr1>>14][0]<<7; sy= oam_sizes[obj->attr0>>14][obj->attr1>>14][1]<<7; dx= oac->pos.x+oac->anchor.x - sx; // .8f dy= oac->pos.y+oac->anchor.y - sy; // .8f if(obj->attr0&ATTR0_DBL_BIT) { dx -= sx; dy -= sy; } sx= oac->anchor.x - pt->x - sx; sy= oac->anchor.y - pt->y - sy; dx -= (aa*sx + ab*sy)>>8; // .8f dy -= (ac*sx + ad*sy)>>8; // .8f BF_SET(obj->attr0, dy>>8, ATTR0_Y); BF_SET(obj->attr1, dx>>8, ATTR1_X); obj++; pt++; } } void init_main() { memcpy32(pal_obj_mem, oac_gfxPal, oac_gfxPalLen/4); memcpy32(tile_mem[4], oac_gfxTiles, oac_gfxTilesLen/4); // init objs and obj combos oam_init(); memcpy32(obj_buffer, obj_data, sizeof(obj_data)/4); REG_DISPCNT= DCNT_BG0 | DCNT_OBJ | DCNT_OBJ_1D; tte_init_chr4_b4_default(0, BG_CBB(2)|BG_SBB(28)); tte_init_con(); file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 107/331 28-03-13 Tonc : GBA Programming in rot13 // Some labels tte_printf("#{P:%d,%d}1 full #{P:%d,%d}2 semi #{P:%d,%d}4 quarts", X0-48, Y0-16, X0+20, Y0-16, X0-20, Y0+74); } int main() { init_main(); int ii, alpha=0; while(1) { vid_vsync(); key_poll(); alpha -= 128*key_tri_shoulder(); for(ii=0; ii<3; ii++) { oacs[ii].alpha= alpha; oac_rotscale(&oacs[ii]); } oam_copy(oam_mem, obj_buffer, 128); } return 0; } Fi g1 1 . 8o nt h er i g h ts h o wsas c r e e n s h o to ft h ed e mo .Th e r ea r et h r e ema i nt h i n g st o p o i n to u th e r e .Fi r s t , a l l t h r e eo b j e c t sa r ei n d e e dr o u g h l yt h es a mes h a p e , me a n i n gt h a t t h ef u n c t i o n ( s )wo r k .Bu tt h i swa sn e v e rr e a l l ymu c hi nd o u b ta n y wa y , s i n c ei tj u s t f o l l o wst h ema t h .Th es e c o n dp o i n ti st h a tt h e r ea p p e a rt ob eg a p si nt h es e mi -a n d q u a r t e r c i r c l ec o mb o s .I fy o up l a ywi t ht h ed e moy o u r s e l ff o rawh i l e , y o u ' l l s e et h e s e g a p sa p p e a ra n dd i s a p p e a rs e e mi n g l ya tr a n d o m.Me a n wh i l e , t h ef u l l c i r c l eo b j e c t l o o k sf i n et h r o u g h o u t .We l l mo s t l ya n y wa y . Th ec a u s eo ft h i si sr e l a t e dt ot h et h i r dp o i n t .Co mp a r et h ep i x e l c l u s t e r so fa l l t h r e ec i r c l e s , i np a r t i c u l a rt h es ma l l e rc i r c l e swi t h i ne a c ho ft h e m.No t et h a te v e n t h o u g ht h e yu s et h eexact s a meP ma t r i x , t h e i rf o r ma t i o n sa r ed i f f e r e n t !Th er e a s o n f o rt h i si st h a twh i l ewema yh a v ep o s i t i o n e dt h es u b o b j e c t st oma k et h e mf o r ma Fig 11.8: oacomboi na c t i o n . No t et h eg a ps . b i g g e ro b j e c t , t h ep i x e l ma p p i n gf o re a c ho ft h e mstill s t a r t sa tt h e i rc e n t e r s .Th i s me a n st h a tt h ec u mu l a t i v eo f f s e t st h a td e t e r mi n ewh i c hs o u r c ep i x e l i su s e df o rag i v e ns c r e e np i x e l wi l l b ed i f f e r e n ta n dh e n c ey o u ' l l g e t ad i f f e r e n tp i c t u r e , wh i c hi se s p e c i a l l yv i s i b l ea tt h es e a ms . I ft h i si sal i t t l eh a r dt ov i s u a l i z e , t r yt h i s : o p e nab i t ma pe d i t o ra n dd r a was i n g l e wi d t hd i a g o n a l l i n e .No wd u p l i c a t et h i swi t ha( 1 , 1 ) p i x e l o f f s e t .I n s t e a do fas i n g l et h i c kl i n e , y o u ' l l h a v et wot h i no n e swi t has l i ti nb e t we e n .Th es a met h i n gh a p p e n sh e r e . Th ep o i n ti st h a tg e t t i n ga f f i n eo b j e c t st oa l i g np e r f e c t l ya tt h es e a mswi l l b ep r e t t ymu c hi mp o s s i b l e .Al r i g h t , Is u p p o s ei ns o mes i mp l e c a s e sy o umi g h tg e ta wa ywi t hi t , a n dy o uc o u l ds p e n dt i mewr i t i n gc o d et h a tc o r r e c t st h et e x t u r e st oa l i g np r o p e r l y , b u tg e n e r a l l y s p e a k i n gy o us h o u l de x p e c tah a r d wa r e c a u s e du n c e r t a i n t yo fa b o u tap i x e l .Th i swi l l b ean o t i c e a b l ee f f e c ta tt h eo f f c e n t e rr e f e r e n c e p o i n t , wh i c hwi l l t e n dt owo b b l eab i t , o ra tt h es e a mso fa f f i n eo b j e c tc o mb o s , wh e r ey o u ' l l s e eg a p s .As i mp l es o l u t i o nt ot h ef o r me r wo u l db et or e a r r a n g et h eo b j e c t ' st i l e ss ot h a tt h er e f p o i n ti sn o to f f c e n t e r( s o u n d sc h e a pIk n o w, b u two r k sb e a u t i f u l l y ) , o rt oh a v e t r a n s p a r e n tp i x e l st h e r e–y o uc a n ' tn o t i c es o me t h i n gwo b b l i n gi fi t ' si n v i s i b l e , a f t e ra l l .Th i swo u l da l s owo r kf o rt h ec o mb o , wh i c hmi g h t a l s ob e n e f i tf r o mh a v i n gt h eo b j e c t so v e r l a ps l i g h t l y , a l t h o u g hIh a v e n ' tt r i e dt h a ty e t .I tmay b ep o s s i b l et og a i ns o mea c c u r a c yb ya d d i n g r o u n d i n gt e r mst ot h ec a l c u l a t i o n s , b u tIh a v eah u n c ht h a ti two n ' td ot h a tmu c h .Fe e l f r e et ot r yt h o u g h . Do n ' tl e ta l l t h i st a l ko ft h ep i t f a l l so fa f f i n eo b j e c t sg e tt oy o ut o omu c h , I ' mj u s tp o i n t i n go u tt h a ti tmi g h tn o tb eq u i t ea ss i mp l ea sy o u mi g h th a v eh o p e d .Sot h e yc o mewi t haf e ws t r i n g s , t h e y ' r es t i l l p r e t t yc o o l e f f e c t s .Wh e nd e s i g n i n gag a met h a tu s e st h e m, t a k et h e i s s u e sr a i s e di nt h i sc h a p t e rt oh e a r ta n dma k es u r ey o u rma t hi si no r d e r , i tmi g h ts a v ey o ual o to fwo r kl a t e ro n . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 108/331 28-03-13 Tonc : GBA Programming in rot13 12. Affine backgrounds I n t r o d u c t i o n . Af f i n eb a c k g r o u n dr e g i s t e r s . Po s i t i o n i n ga n dt r a n s f o r mi n ga f f i n eb a c k g r o u n d s . Ma p p i n gf o r ma t . sbb_affd e mo . 12.1. Introduction Th i ss e c t i o nc o v e r saffine backgrounds: t h eo n e so nwh i c hy o uc a np e r f o r ma na f f i n et r a n s f o r ma t i o nv i at h eP ma t r i x .An dt h a t ' sa l l i t d o e s .I fy o uh a v e n ' tr e a d–a n du n d e r s t o o d !–t h es p r i t e / b go v e r v i e wa n dt h es e c t i o n so nr e g u l a rb a c k g r o u n d sa n dt h ea f f i n e t r a n s f o r ma t i o nma t r i x , d os ob e f o r ec o n t i n u i n g . I fy o uk n o wh o wt ob u i l dar e g u l a rb a c k g r o u n da n dh a v eu n d e r s t o o dt h ec o n c e p t sb e h i n dt h ea f f i n ema t r i x , y o us h o u l dh a v el i t t l e p r o b l e msh e r e .Th et h e o r yb e h i n da na f f i n eb a c k g r o u n d sa r et h es a mea sf o rr e g u l a ro n e s , t h ep r a c t i c ec a nb ed i f f e r e n ta tan u mb e ro f v e r yc r u c i a l p o i n t s .Fo re x a mp l e , y o uu s ed i f f e r e n tr e g i s t e r sf o rp o s i t i o n i n ga n db o t ht h ema p l a y o u ta n dt h e i rf o r ma ta r ed i f f e r e n t . Oft h ef o u rb a c k g r o u n d st h eGBAh a s , o n l yt h el a s tt woc a nb eu s e da sa f f i n eb a c k g r o u n d s , a n do n l yi ns p e c i f i cv i d e omo d e s( s e e t a b l e1 2 . 1 ) .Th es i z e sa r ea l s od i f f e r e n tf o ra f f i n eb a c k g r o u n d s .Yo uc a nf i n dal i s to fs i z e si nt a b l e1 2 . 2 . mode 0 1 2 bg0 r e gr e g - bg1 r e gr e g - bg2 r e ga f fa f f bg3 r e g -a f f Table 12.1: v i d e omo d e s a n db a c k g r o u n dt ype Sz define (tiles) (pixels) 0 0 BG_AFF_1 6 x 1 6 1 6 x 1 6 1 2 8 x 1 2 8 0 1 BG_AFF_3 2 x 3 2 3 2 x 3 2 2 5 6 x 2 5 6 1 0 BG_AFF_6 4 x 6 4 6 4 x 6 4 5 1 2 x 5 1 2 1 1BG_AFF_1 2 8 x 1 2 81 2 8 x 1 2 81 0 2 4 x 1 0 2 4 Table 12.2: a f f i n eb gs i z e s 12.2. Affine background registers Li k et h e i rr e g u l a rc o u n t e r p a r t s , t h ep r i ma r yc o n t r o l f o ra f f i n eb a c k g r o u n d si sREG_BGxCNT.I fy o u ' v ef o r g o t t e nwh a ti td o e s , y o uc a n r e a dad e s c r i p t i o nh e r e .Th ed i f f e r e n c e swi t hr e g u l a rb a c k g r o u n d sa r et h es i z e s , a n dt h a tBG_WRAPa c t u a l l yd o e ss o me t h i n gn o w.Th e o t h e ri mp o r t a n tr e g i s t e r sa r et h edisplacement vector dx ( REG_BGxXa n dREG_BGxY) , a n dt h eaffine matrix P ( REG_BGxPAREG_BGxPD) .Yo uc a nf i n dt h e i ra d d r e s s e si nt a b l e1 2 . 3 . Register length REG_BGxCNT 2 address 0 4 0 0 : 0 0 0 8 h+2 · x REG_BGxPA-PD 2 0 4 0 0 : 0 0 2 0 h+1 0 h · ( x 2 ) REG_BGxX 4 0 4 0 0 : 0 0 2 8 h+1 0 h · ( x 2 ) REG_BGxY 4 0 4 0 0 : 0 0 2 c h+1 0 h · ( x 2 ) Table 12.3: Af f i n eb a c k g r o u n dr e g i s t e ra d d r e s s e s . No t et ha tx i s2o r3o n l y! Th e r ea r eac o u p l eo ft h i n g st ot a k en o t eo fwh e ni tc o me st od i s p l a c e me n ta n dt r a n s f o r ma t i o no fa f f i n eb a c k g r o u n d s .Fi r s t , t h e d i s p l a c e me n tdx u s e sd i f f e r e n tr e g i s t e r st h a nr e g u l a rb a c k g r o u n d s : REG_BGxXa n dREG_BGxYi n s t e a do fREG_BGxHOFSa n d REG_BGxVOFS.As e c o n dp o i n th e r ei st h a tt h e ya r e2 4 . 8f i x e dn u mb e r sr a t h e rt h a np i x e l o f f s e t s .( Ac t u a l l y , t h e ya r e2 0 . 8f i x e d n u mb e r sb u tt h a t ' sn o ti mp o r t a n tr i g h tn o w. ) Iu s u a l l yu s et h ea f f i n ep a r a me t e r sv i aBG_AFFI NEs t r u c ti n s t e a do fREG_BGxPA, e t c .Th eme mo r yma pi ntonc_memmap.h c o n t a i n saREG_BG_AFFINEf o rt h i sp u r p o s e .Se t t i n gt h er e g i s t e r st h i swa yi sa d v a n t a g e o u sa tt i me sb e c a u s ey o u ' l l u s u a l l yh a v ea BG_AFFI NEs t r u c ts e tu pa l r e a d y , wh i c hy o uc a nt h e nc o p yt ot h er e g i s t e r swi t has i n g l ea s s i g n me n t .Ane x a mp l eo ft h i si sg i v e n b e l o w. Th ee l e me n t so ft h ea f f i n et r a n s f o r ma t i o nma t r i xP wo r k se x a c t l yl i k et h e yd of o ra f f i n es p r i t e s : 8 . 8f i x e dp o i n tn u mb e r st h a t d e s c r i b et h et r a n s f o r ma t i o nf r o ms c r e e nt ot e x t u r es p a c e .Ho we v e rf o ra f f i n eb a c k g r o u n d st h e ya r es t o r e dc o n s e c u t i v e l y( 2b y t eo f f s e t ) , wh e r e a st h o s eo fs p r i t e sa r ea ta n8b y t eo f f s e t .Yo uc a nu s et h ebg_aff_foof u n c t i o n sf r o mtonc_bg_affine.ct os e tt h e mt o t h et r a n s f o r ma t i o ny o uwa n t . typedef struct tagBG_AFFINE { s16 pa, pb; s16 pc, pd; file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 109/331 28-03-13 Tonc : GBA Programming in rot13 s32 dx, dy } ALIGN4 BG_AFFINE; //! BG affine params array #define REG_BG_AFFINE ((BG_AFFINE*)(REG_BASE+0x0000)) // Default BG_AFFINE data (tonc_core.c) const BG_AFFINE bg_aff_default= { 256, 0, 0, 256, 0, 0 }; // Initialize affine registers for bg 2 REG_BG_AFFINE[2] = bg_aff_default; Regular vs affine tilemap scrolling Af f i n et i l e ma p su s edifferent s c r o l l i n gr e g i s t e r s !I n s t e a do fREG_BGxHOFSa n dREG_BGxVOFS, t h e yu s eREG_BGxX a n dREG_BGxY.Al s o , t h e s ea r e3 2 b i tf i x e dp o i n tn u mb e r s , n o th a l f wo r d s . 12.3. Positioning and transforming affine backgrounds No wt h a twek n o wwh a tt h ed i s p l a c e me n ta n dt r a n s f o r ma t i o nr e g i s t e r sa r e , n o wl e t ' sl o o ka twh a tt h e yd o .Th i si sa c t u a l l yal o tt r i c k i e r s u b j e c tt h a ty o umi g h tt h i n k , s op a ya t t e n t i o n .Wa r n i n g : t h i si sg o n n ag e tma t h e ma t i c a l a g a i n . Th ed i s p l a c e me n tv e c t o rdx wo r k st h es a mea sf o rr e g u l a rb a c k g r o u n d s : dx c o n t a i n st h eb a c k g r o u n d c o o r d i n a t e st h a ta r ema p p e dt ot h e s c r e e no r i g i n .( An dnot t h eo t h e rwa ya r o u n d ! )Ho we v e r , t h i st i medx i si nf i x e dn u mb e rn o t a t i o n .Li k e wi s e , t h ea f f i n et r a n s f o r ma t i o n ma t r i xP wo r k st h es a mea sf o ra f f i n es p r i t e s : Pd e s c r i b e st h et r a n s f o r ma t i o nf r o ms c r e e ns p a c et ot e x t u r es p a c e .Top u ti t ma t h e ma t i c a l l y , i fwed e f i n e (12.1a) T( dx) p := p +dx −1 T ( dx)=T( −dx) (12.1b) P = A−1 t h e n (12.2a) T( dx) q =p (12.2b) P · q =p wh e r e sap o i n ti nt e x t u r es p a c e , pi i s a p o i n t i n s c r e e n s p a c e , q st h ed i s p l a c e me n tv e c t o r( REG_BGxXa n dREG_BGxY) . dx i st h et r a n s f o r ma t i o nf r o mt e x t u r et os c r e e ns p a c e , Ai st h et r a n s f o r ma t i o ns c r e e nf r o mt ot e x t u r es p a c e , ( REG_BGxPAREG_BGxPD) . Pi Th ep r o b l e mwi t he q1 2 . 2i st h a tt h e s eo n l yd e s c r i b ewh a th a p p e n si fy o uu s ee i t h e rad i s p l a c e me n to rat r a n s f o r ma t i o n .Sowh a t h a p p e n si fy o uwa n tt ou s eb o t h ?Th i si sa ni mp o r t a n tq u e s t i o nb e c a u s et h eo r d e ro ft r a n s f o r ma t i o nma t t e r s( l i k eweh a v es e e ni nt h e a f f i n es p r i t ed e mo ) , a n dt h i si st r u ef o rt h eo r d e ro ft r a n s f o r ma t i o na n dd i s p l a c e me n ta swe l l .Asi th a p p e n s , t r a n s l a t i o ng o e sf i r s t : q =A ·T( −dx)p (12.3) T ( dx)P ·q =p dx +P ·q =p An o t h e rwa yt os a yt h i si st h a tt h et r a n s f o r ma t i o na l wa y su s e st h et o pl e f to ft h es c r e e na si t so r i g i na n dt h ed i s p l a c e me n tt e l l swh i c h b a c k g r o u n dp i x e l si sp u tt h e r e .Ofc o u r s e , t h i sa r r a n g e me n td o e s n ' th e l pv e r ymu c hi fy o uwa n tt o , s a y , r o t a t ea r o u n ds o meo t h e rp o i n t o nt h es c r e e n .Tod ot h a ty o u ' l l h a v et op u l l af e wt r i c k s .Toc o v e rt h e ma l l i no n es wo o p , we ' l l c o mb i n ee q1 2 . 3a n dt h eg e n e r a l c o o r d i n a t et r a n s f o r ma t i o ne q u a t i o n : dx + P · q =p P ·( q − q0)=p − p0 − (12.4) dx + P · q0 =p0 dx =p0−P · q0 file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 110/331 28-03-13 Tonc : GBA Programming in rot13 Sowh a tt h eh e l l d o e sthat me a n ?I tme a n st h a ti fy o uu s et h i sdx f o ry o u rd i s p l a c e me n tv e c t o r , y o up e r f o r my o u rt r a n s f o r ma t i o na r o u n d t e x t u r ep o i n tp0, wh i c ht h e ne n d su pa ts c r e e np o i n tq0;t h eP· q0t e r mi st h ec o r r e c t i o ni nt e x t u r e s p a c ey o uh a v et op e r f o r mt oh a v et h e r o t a t i o np o i n ta tq0i n s t e a do f( 0 , 0 ) .Sowh a tt h eh e l l d o e sthat me a n ?I tme a n st h a tb e f o r ey o ut r yt ou s et h i ss t u f fy o us h o u l dt h i n ka b o u t wh i c he f f e c ty o ua r ea c t u a l l yt r y i n gt op u l l o f fa n dt h a ty o uh a v etwo c o o r d i n a t es y s t e mst owo r kwi t h , n o to n e .Wh e ny o ud o , t h e me a n i n go fe q1 2 . 4wi l l b e c o mea p p a r e n t .I na n yc a s e , t h ef u n c t i o nIu s ef o rt h i si sbg_rotscale_ex(), wh i c hb a s i c a l l yl o o k sl i k e t h i s : typedef struct tagAFF_SRC_EX { s32 tex_x, tex_y; // vector p0: origin in texture space (24.8f) s16 scr_x, scr_y; // vector q0: origin in screen space (16.0f) s16 sx, sy; // scales (8.8f) u16 alpha; // CCW angle ( integer in [0,0xFFFF] ) } ALIGN4 AFF_SRC_EX; void bg_rotscale_ex(BG_AFFINE *bgaff, const AFF_SRC_EX *asx) { int sx= asx->sx, sy= asx->sy; int sina= lu_sin(asx->alpha), cosa= lu_cos(asx->alpha); FIXED pa, pb, pc, pd; pa= sx*cosa>>12; pb=-sx*sina>>12; pc= sy*sina>>12; pd= sy*cosa>>12; // .8f // .8f bgaff->pa= pa; bgaff->pb= pb; bgaff->pc= pc; bgaff->pd= pd; bgaff->dx= asx->tex_x - (pa*asx->scr_x + pb*asx->scr_y); bgaff->dy= asx->tex_y - (pc*asx->scr_x + pd*asx->scr_y); } Th i si sv e r ys i mi l a rt ot h eobj_rotscale_ex()f u n c t i o nc o v e r e di nt h eo f f c e n t e ro b j e c tt r a n s f o r ma t i o ns e c t i o n .Th ema t hi s i d e n t i c a l , b u tt h et e r msh a v eb e e nr e s h u f f l e dab i t .Th eb a c k g r o u n dv e r s i o ni sa c t u a l l ys i mp l e rb e c a u s et h ea f f i n eo f f s e tc o r r e c t i o nc a n b ed o n ei nt e x t u r es p a c ei n s t e a do fs c r e e ns p a c e , wh i c hme a n sn ome s s i n ga b o u twi t hP' si n v e r s ema t r i x .Orwi t hs p r i t e s i z ec o r r e c t i o n s , t h a n kI PU.Fo rt h er e c o r d , y e sy o uc a na p p l yt h ef u n c t i o nd i r e c t l yt oREG_BG_AFFINE. 12.3.1. Internal reference point registers Th e r e ' so n emo r ei mp o r t a n tt h i n gl e f tt ome n t i o na b o u tt h ed i s p l a c e me n ta n dt r a n s f o r ma t i o nr e g i s t e r s .Qu o t i n gd i r e c t l yf r o mGBATe k ( e x c e p tt h eb r a c k e t e dp a r t s ) : Th ea b o v er e f e r e n c ep o i n t s[ t h ed i s p l a c e me n tr e g i s t e r s ]a r ea u t o ma t i c a l l yc o p i e dt oi n t e r n a l r e g i s t e r sd u r i n ge a c hv b l a n k , s p e c i f y i n gt h eo r i g i nf o rt h ef i r s ts c a n l i n e .Th ei n t e r n a l r e g i s t e r sa r et h e ni n c r e me n t e db yd mx[ REG_BGxPB]a n dd my [ REG_BGxPD]a f t e re a c hs c a n l i n e . Ca u t i o n : Wr i t i n gt oar e f e r e n c ep o i n tr e g i s t e rb ys o f t wa r eo u t s i d eo ft h eVb l a n kp e r i o dd o e si mme d i a t e l yc o p yt h en e w v a l u et ot h ec o r r e s p o n d i n gi n t e r n a l r e g i s t e r , t h a tme a n s : i nt h ec u r r e n tf r a me , t h en e wv a l u es p e c i f i e st h eo r i g i no ft h e current s c a n l i n e( i n s t e a do ft h et o p mo s ts c a n l i n e ) . No r ma l l yt h i swo n ' tma t t e rt oy o u , b u ti fy o ut r yt owr i t et oREG_BGxYd u r i n ga nHBl a n kt h i n g s , mi g h tn o tg oa se x p e c t e d .AsIl e a r n e d t h eh a r dwa ywh e nIt r i e dt og e tmyMo d e7s t u f fwo r k i n g .Th i so n l ya f f e c t sa f f i n eb a c k g r o u n d s , t h o u g h ;r e g u l a ro n e su s eo t h e r r e g i s t e r s . 12.4. Mapping format Bo t ht h ema pl a y o u ta n ds c r e e ne n t r i e sf o ra f f i n eb a c k g r o u n d sa r ev e r yd i f f e r e n tf r o mt h o s eo fr e g u l a rb a c k g r o u n d s .I r o n i c a l l y , t h e ya r e a l s oal o ts i mp l e r .Wh i l er e g u l a rb a c k g r o u n d sd i v i d et h ef u l l ma pi n t oq u a d r a n t s( e a c hu s i n go n ef u l l s c r e e n b l o c k ) , t h ea f f i n eb a c k g r o u n d s u s eaf l a tma p , me a n i n gt h a tt h en o r ma l e q u a t i o nf o rg e t t i n gas c r e e n e n t r y n u mb e rn wo r k s , ma k i n gt h i n g sawh o l el o te a s i e r . (12.5) n =tx +ty· tw Th es c r e e ne n t r i e st h e ms e l v e sa r ea l s od i f f e r e n tf r o mt h o s eo fr e g u l a rb a c k g r o u n d sa swe l l .I na f f i n ema p s , t h e ya r e1 byte long a n d o n l yc o n t a i nt h ei n d e xo ft h et i l et ou s e .Ad d i t i o n a l l y , y o uc a nonly u s e2 5 6c o l o rt i l e s .Th i sg i v e sy o ua c c e s st oa l l t h et i l e si nt h eb a s e c h a r b l o c k , b u tn o tt h eo n e ( s )a f t e ri t . An dt h a t ' sa b o u ti t , r e a l l y .No , wa i tt h e r e ' so n emo r ei s s u e : y o uh a v et ob ec a r e f u l wh e nf i l l i n go rc h a n g i n gt h ema pb e c a u s eVRAM can only be accessed 16 or 32 bits at a time.Soi fy o uh a v ey o u rma ps t o r e di na na r r a yo fb y t e s , y o u ' l l h a v et oc a s ti tt ou16o ru32 f i r s t .Oru s eDMA.OK, n o wI ' md o n e . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 111/331 28-03-13 Tonc : GBA Programming in rot13 Regular vs affine tilemap mapping differences Th e r ea r et woi mp o r t a n td i f f e r e n c e sb e t we e nr e g u l a ra n da f f i n ema pf o r ma t s .Fi r s t , a f f i n es c r e e ne n t r i e sa r eme r e l yo n e b y t e t i l ei n d i c e s .Se c o n d l y , t h ema p su s eal i n e a rl a y o u t , r a t h e rt h a nt h ed i v i s i o ni n t o3 2 x 3 2 tma p st h a tb i g g e rr e g u l a rma p su s e . 12.5. sbb_affdemo sbb_affi st oa f f i n eb a c k g r o u n d swh a tsbb_regwa st or e g u l a ro n e s , wi t han u mb e ro f e x t r a s .Th ed e mou s e sa6 4 x 6 4t i l ea f f i n eb a c k g r o u n d , s h o wni nf i g1 2 . 1 .Th i si sd i v i d e di n t o 1 6p a r t so f2 5 6b y t e s , e a c ho fwh i c hi sf i l l e dwi t ht i l e so fo n ec o l o ra n dt h en u mb e ro ft h a t p a r to ni t .No w, i ft h ema p l a y o u tf o ra f f i n eb a c k g r o u n d swa st h es a mea sr e g u l a ro n e s , e a c hp a r two u l df o r ma1 6 x 1 6 ts q u a r e .I fi ti saf l a tme mo r yl a y o u t , e a c hp a r two u l db ea 6 4 x 1 6 ts t r i p .Asy o uc a ns e ei nf i g1 2 . 1 , i ti st h el a t t e r .Yo uc a na l s os e et h a t , u n l i k er e g u l a r b a c k g r o u n d s , t h i sma pd o e s n ' twr a pa r o u n da u t o ma t i c a l l ya tt h ee d g e s . Th emo s ti n t e r e s t i n gt h i n ga b o u tt h ed e moa r et h el i t t l eb l a c ka n dwh i t ec r o s s h a i r s .Th e wh i t ec r o s s h a i r si n d i c a t e st h er o t a t i o np o i n t( t h ea n c h o r ) .AsIs a i de a r l i e r , y o uc a n n o t s i mp l yp i c kama p p o i n tp0a n ds a yt h a tt h a ti s‘ t h e ’r o t a t i o np o i n t .We l l y o uc o u l d , b u ti t Fig 12.1: sbb_affd e mo . wo u l d n ' tg i v et h ed e s i r e de f f e c t .Si mp l yu s i n gama p p o i n twi l l g i v ey o uar o t a t i n gma p a r o u n dt h a tp o i n t , b u to ns c r e e ni t ' l l a l wa y sb ei nt h et o p l e f tc o r n e r .Tomo v et h ema pa n c h o rt oas p e c i f i cl o c a t i o no nt h es c r e e n , y o u n e e da na n c h o rt h e r ea swe l l .Th i si sq0.Fi l l b o t hi n t oe q1 2 . 4t of i n dt h ed i s p l a c e me n tv e c t o ry o un e e d : dx =p0−P·q0.Th i sdx i sg o i n g t ob eq u i t ed i f f e r e n tf r o mb o t hp0a n dq0.I t sp a t hi si n d i c a t e db yt h eb l a c kc r o s s h a i r s . Th ed e mol e t sy o uc o n t r o l b o t hp0a n dq0.An dr o t a t i o na n ds c a l i n g , o fc o u r s e .Th ef u l l l i s to fc o n t r o l si s . D-pad mo v ema pr o t a t i o np o i n t , p0 D-pad + A mo v es c r e e nr o t a t i o np o i n t , q0 r o t a t et h eb a c k g r o u n d . L,R c a l eu pa n dd o wn . B(+Se) s To g g l ewr a p p i n gf l a g . St St+Se Re s e ta n c h o r sa n dP #include <stdio.h> #include <tonc.h> #include "nums.h" #define MAP_AFF_SIZE 0x0100 // -------------------------------------------------------------------// GLOBALS // -------------------------------------------------------------------OBJ_ATTR *obj_cross= &oam_mem[0]; OBJ_ATTR *obj_disp= &oam_mem[1]; BG_AFFINE bgaff; // -------------------------------------------------------------------// FUNCTIONS // -------------------------------------------------------------------void win_textbox(int bgnr, int left, int top, int right, int bottom, int bldy) { REG_WIN0H= left<<8 | right; REG_WIN0V= top<<8 | bottom; REG_WIN0CNT= WIN_ALL | WIN_BLD; REG_WINOUTCNT= WIN_ALL; REG_BLDCNT= (BLD_ALL&~BIT(bgnr)) | BLD_BLACK; REG_BLDY= bldy; REG_DISPCNT |= DCNT_WIN0; tte_set_margins(left, top, right, bottom); } void init_cross() { TILE cross= {{ file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 112/331 28-03-13 Tonc : GBA Programming in rot13 0x00011100, 0x00100010, 0x01022201, 0x01021201, 0x01022201, 0x00100010, 0x00011100, 0x00000000, }}; tile_mem[4][1]= cross; pal_obj_mem[0x01]= pal_obj_mem[0x12]= CLR_WHITE; pal_obj_mem[0x02]= pal_obj_mem[0x11]= CLR_BLACK; obj_cross->attr2= 0x0001; obj_disp->attr2= 0x1001; } void init_map() { int ii; memcpy32(&tile8_mem[0][1], nums8Tiles, nums8TilesLen/4); memcpy32(pal_bg_mem, numsPal, numsPalLen/4); REG_BG2CNT= BG_CBB(0) | BG_SBB(8) | BG_AFF_64x64; bgaff= bg_aff_default; // fill per 256 screen entries (=32x4 bands) u32 *pse= (u32*)se_mem[8]; u32 ses= 0x01010101; for(ii=0; ii<16; ii++) { memset32(pse, ses, MAP_AFF_SIZE/4); pse += MAP_AFF_SIZE/4; ses += 0x01010101; } } void sbb_aff() { AFF_SRC_EX asx= { 32<<8, 64<<8, 120, 80, 0x0100, 0x0100, 0 }; // Map coords. // Screen coords. // Scales and angle. const int DX=256; FIXED ss= 0x0100; while(1) { vid_vsync(); key_poll(); // dir + A : move map in screen coords if(key_is_down(KEY_A)) { asx.scr_x += key_tri_horz(); asx.scr_y += key_tri_vert(); } else // dir : move map in map coords { asx.tex_x -= DX*key_tri_horz(); asx.tex_y -= DX*key_tri_vert(); } // rotate asx.alpha -= 128*key_tri_shoulder(); // B: scale up ; B+Se : scale down if(key_is_down(KEY_B)) ss += (key_is_down(KEY_SELECT) ? -1 : 1); // St+Se : reset // St : toggle wrapping flag. if(key_hit(KEY_START)) { file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 113/331 28-03-13 Tonc : GBA Programming in rot13 if(key_is_down(KEY_SELECT)) { asx.tex_x= asx.tex_y= 0; asx.scr_x= asx.scr_y= 0; asx.alpha= 0; ss= 1<<8; } else REG_BG2CNT ^= BG_WRAP; } asx.sx= asx.sy= (1<<16)/ss; bg_rotscale_ex(&bgaff, &asx); REG_BG_AFFINE[2]= bgaff; // the cross indicates the rotation point // (== p in map-space; q in screen-space) obj_set_pos(obj_cross, asx.scr_x-3, (asx.scr_y-3)); obj_set_pos(obj_disp, (bgaff.dx>>8)-3, (bgaff.dy>>8)-3); tte_printf("#{es;P}p0\t: (%d, %d)\nq0\t: (%d, %d)\ndx\t: (%d, %d)", asx.tex_x>>8, asx.tex_y>>8, asx.scr_x, asx.scr_y, bgaff.dx>>8, bgaff.dy>>8); } } int main() { init_map(); init_cross(); REG_DISPCNT= DCNT_MODE1 | DCNT_BG0 | DCNT_BG2 | DCNT_OBJ; tte_init_chr4_b4_default(0, BG_CBB(2)|BG_SBB(28)); tte_init_con(); win_textbox(0, 8, 120, 232, 156, 8); sbb_aff(); return 0; } file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 114/331 28-03-13 Tonc : GBA Programming in rot13 13. Graphic Effects Mo s a i c . Bl e n d i n g . Wi n d o wi n g . Co n c l u s i o n s . Soy o uk n o wh o wt op u ts p r i t e sa n db a c k g r o u n d so ns c r e e n , d oy a ?No w, h o wa b o u ts o mee x t r ae f f e c t st ol i v e nu pt h ep l a c e ?Wh e n d i s c u s s i n gs p r i t e sa n db a c k g r o u n d s , wel e f ts o mef l a g su n t o u c h e d , n a me l yt h emo s a i ca n db l e n d i n gf l a g s .Th e r ewi l l b ec o v e r e dh e r e . We ' l l a l s ob el o o k i n gi n t owi n d o wi n g , wi t hwh i c hy o uc a nc r e a t er e g i o n st oma s ko u tb a c k g r o u n d so rs p r i t e s . 13.1. Mosaic Th eb e s td e s c r i p t i o no fmo s a i ci st h a ti tma k e ss p r i t e so rt i l e sl o o kb l o c k y .Amo s a i cwo r k si nt wod i me n s i o n swi t hp a r a me t e r swm a n d h m.Th e s en u mb e r sd i v i d ey o u rs p r i t eo rb a c k g r o u n di n t ob l o c k so fwm ×h m p i x e l s .Th et o p l e f tp i x e l o fe a c hb l o c ki su s e dt of i l l t h er e s t o ft h a tb l o c k , wh i c hma k e si tb l o c k y .Fi g1 3 . 1s h o wsa1 x 4mo s a i cf o rame t r o i ds p r i t e .Th eb l u el i n e si n d i c a t et h ev e r t i c a l b l o c k b o u n d a r i e s .Th ef i r s tl i n eo fe a c hb l o c ki sc o p i e dt ot h er e s to ft h eb l o c k , j u s tl i k eIs a i d .Ot h e re x a mp l e so ft h emo s a i ce f f e c ta r e Ze l d a : LTTPwh e ny o uh i ta ne l e c t r i cb a d d i e , o rMe t r o i dFu s i o nwh e na nXc h a n g e ss h a p e . Fig 13.1: a1 ×4mo s a i c e dme t r o i d . 13.1.1. Using mosaic: sprite/bg flags and REG_MOSAIC Tou s emo s a i cy o umu s td ot wot h i n g s .Fi r s t , y o un e e dt oe n a b l emo s a i c .Fo ri n d i v i d u a l s p r i t e s , s e tOBJ_ATTR.attr0{ C} .Fo r b a c k g r o u n d s , s e tREG_BGxCNT{ 7 } .Th es e tt h emo s a i cl e v e l st h r o u g hREG_MOSAIC, wh i c hl o o k sl i k et h i s : REG_MOSAIC @ 0400:004Ch FEDC BA98 7654 3210 Ov Oh Bv bits name define 0 3 Bh MOS_BH# Ho r i z o n t a l BGs t r e t c h . 4 7 Bv MOS_BV# Ve r t i c a l BGs t r e t c h . 8 B Oh MOS_OH# Ho r i z o n t a l o b j e c ts t r e t c h . CF Ov Bh description MOS_OV# Ve r t i c a l o b j e c ts t r e t c h . Th estretch i sa c r o s sh o wma n yp i x e l st h eb a s e p i x e l i ss t r e t c h e d .Th i sc o r r e s p o n d st owm−1o rh m−1 .Wi t han y b b l ef o re a c he f f e c t , y o u h a v es t r e t c h e sb e t we e n0a n d1 5 , g i v i n gmo s a i cwi d t h sa n dh e i g h t sb e t we e n1a n d1 6 . Enabling mosaic Fo rb a c k g r o u n d s , s e tb i t7o fREG_BGx CNT.Fo rs p r i t e s , s e tb i t1 2i na t t r i b u t e0 .Th e ns e tt h emo s a i cl e v e l si n REG_MOSAI C. 13.1.2. A small mosaic demo Th e r ei sad e moc a l l e dmos_demot h a ti l l u s t r a t e st h eu s eo fmo s a i cf o rb o t ho b j e c t sa n db a c k g r o u n d s . // mos_demo.c // bg 0, cbb 0, sbb 31, pb 0: text file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 115/331 28-03-13 // // Tonc : GBA Programming in rot13 bg 1, cbb 1, sbb 30, pb 1: bg metroid oam 0: tile 0-63: obj metroid #include <stdio.h> #include <tonc.h> #include "metr.h" void test_mosaic() { tte_printf("#{P:48,8}obj#{P:168,8}bg"); tte_set_margins(4, 130, 128, 156); POINT pt_obj={0,0}, pt_bg={0,0}; POINT *ppt= &pt_obj; while(1) { vid_vsync(); // control the mosaic key_poll(); // switch between bg or obj mosaic ppt= key_is_down(KEY_A) ? &pt_bg : &pt_obj; ppt->x += key_tri_horz(); // inc/dec h-mosaic ppt->y -= key_tri_vert(); // inc/dec v-mosaic ppt->x= clamp(ppt->x, 0, 0x80); ppt->y= clamp(ppt->y, 0, 0x80); REG_MOSAIC= MOS_BUILD(pt_bg.x>>3, pt_bg.y>>3, pt_obj.x>>3, pt_obj.y>>3); tte_printf("#{es;P}obj h,v: %2d,%2d\n bg h,v: %2d,%2d", pt_obj.x>>3, pt_obj.y>>3, pt_bg.x>>3, pt_bg.y>>3); } } void load_metr() { int ix, iy; memcpy32(&tile_mem[1][0], metrTiles, metrTilesLen/4); memcpy32(&tile_mem[4][0], metrTiles, metrTilesLen/4); memcpy32(pal_obj_mem, metrPal, metrPalLen/4); // create object: oe0 OBJ_ATTR *metr= &oam_mem[0]; obj_set_attr(metr, ATTR0_SQUARE | ATTR0_MOSAIC, ATTR1_SIZE_64, 0); obj_set_pos(metr, 32, 24); // left-center // create bg map: bg1, cbb1, sbb 31 for(ix=1; ix<16; ix++) pal_bg_mem[ix+16]= pal_obj_mem[ix] ^ CLR_WHITE; SCR_ENTRY *pse= &se_mem[30][3*32+18]; // right-center for(iy=0; iy<8; iy++) for(ix=0; ix<8; ix++) pse[iy*32+ix]= (iy*8+ix) | SE_PALBANK(1); REG_BG1CNT= BG_CBB(1) | BG_SBB(30) | BG_MOSAIC; } int main() { // setup sprite oam_init(oam_mem, 128); load_metr(); REG_DISPCNT= DCNT_BG0 | DCNT_BG1 | DCNT_OBJ | DCNT_OBJ_1D; // set-up text: bg0, cbb0, sbb31 tte_init_chr4_b4_default(0, BG_CBB(2)|BG_SBB(31)); tte_init_con(); file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 116/331 28-03-13 Tonc : GBA Programming in rot13 test_mosaic(); return 0; } Iu s et wome t r o i d si nt h i sd e mo .Th es p r i t eme t r o i di so nt h el e f t , a n dt h eb a c k g r o u n d me t r o i dwi t hi n v e r t e dc o l o r si so nt h er i g h t .I ' v es h o wnh o wt os e t u ps p r i t e sa n d b a c k g r o u n d sb e f o r e , s oy o us h o u l db ea b l et of o l l o wt h es t e p sh e r eb e c a u s ei t ' sn o t h i n g n e w.We l l , e x c e p ts e t t i n gt h emo s a i cf l a g si nOBJ_ATTR.attr0a n dREG_BG0 CNT, wh i c hI ' v ep u ti nb o l dh e r e . Th emo s a i ce f f e c ti sr e g u l a t e di n s i d et h etest_mosaic().Iu s et wo2 dp o i n t st o k e e pt r a c ko ft h ec u r r e n tl e v e l o fmo s a i c .Th eDp a di su s e dt oi n c r e a s eo rd e c r e a s et h e mo s a i cl e v e l s ;j u s tt h eDp a ds e t st h eo b j e c t ' smo s a i ca n dh o l d i n gd o wnAs e t st h a to ft h e b a c k g r o u n d . Onac o d ed e s i g nn o t e , Ic o u l dh a v eu s e dt woi f b l o c k sh e r e , o n ef o ro b j e c t sa n do n ef o r Fig 13.2:mos_demo. t h eb a c k g r o u n d , b u tIc a na l s os wi t c ht h emo s a i cc o n t e x tv i aap o i n t e r , wh i c hs a v e sme s o mec o d e .Hu r r a yf o rp o i n t e r s .Al s o , t h ec o o r d i n a t e sa r ei n. 3f i x e dp o i n tf o r ma t , wh i c hi sh o wIs l o wd o wnt h ec h a n g e si nt h emo s a i c l e v e l s .Ag a i n , Ic o u l dh a v eu s e dt i me rv a r i a b l e sa n dmo r ec h e c k st os e ei ft h e yh a dr e a c h e dt h e i rt h r e s h o l d s , b u tf i x e d p o i n tt i me r sa r e mu c he a s i e ra n di nmyv i e wc l e a n e rt o o . Yo us h o u l dr e a l l ys e et h ed e moo nh a r d wa r e , b yt h ewa y .So me h o wb o t hVBAa n dn o $ g b aa r eb o t hf l a we dwh e ni tc o me st o mo s a i c .Af t e rVBA1 . 7 . 2 , i th a sap r o b l e mwi t hh o r i z o n t a l s p r i t emo s a i c .Id ob e l i e v eI ' v es e e ni n c o n s i s t e n c i e sb e t we e nh a r d wa r ea n d s c r o l l i n gmo s a i c e db a c k g r o u n d s , b u tc a n ' tr e me mb e rwh e r eIs a wi t .Asf o rn o $ g b a , v e r t i c a l mo s a i ca p p e a r st ob ed i s a b l e df o rb o t h s p r i t e sa n db a c k g r o u n d s . Emulators and mosaic VBAa n dn o $ g b a , t h emo s tp o p u l a rGBAe mu l a t o r sb o t hh a v ep r o b l e mswi t hmo s a i c .Wa t c hy o u rs t e p . 13.2. Blending I fy o u ' r en o tc o mp l e t e l yn e wt og a mi n go rg r a p h i c s , y o uma yh a v eh e a r do falpha blending.I ta l l o wsy o ut oc o mb i n et h ec o l o rv a l u e s t woo v e r l a p p i n gl a y e r s , t h u sc r e a t i n gt r a n s p a r e n c y( a l s ok n o wna ss e mi t r a n s p a r e n c y , b e c a u s es o me t h i n gt h a t ' scompletely t r a n s p a r e n t i si n v i s i b l e ) .So meb i t ma pt y p e sa l s oc o mewi t ha na l p h ac h a n n e l , wh i c hi n d i c a t e se i t h e rt h et r a n s p a r e n c yo ro p a c i t yo ft h ep i x e l i n q u e s t i o n . Th eb a s i ci d e ab e h i n db l e n d i n gi st h i s .Yo uh a v et wol a y e r s , Aa n dB, t h a to v e r l a pe a c ho t h e r .Co n s i d e rAt ob eo nt o po fB.Th e c o l o r v a l u eo ft h eap i x e l i nt h i sr e g i o ni sd e f i n e da s (13.1) C=wA· A+wB· B, wh e r ewA a n dwB a r et h eweights o ft h el a y e r s .Th ewe i g h t sa r eg e n e r a l l yn o r ma l i s e d( b e t we e n0a n d1 ) , wi t h0b e i n gf u l l yt r a n s p a r e n t a n d1b e i n gf u l l yv i s i b l e .I ti sa l s oc o n v e n i e n tt ot h i n ko fc o l o r c o mp o n e n t si nt h i swa y .He r e ' saf e wt h i n g sy o uc a nd owi t ht h e m: wA 1 0 α wB effect 0 l a y e rAf u l l yv i s i b l e( h i d e sB;s t a n d a r d ) 1 l a y e rBf u l l yv i s i b l e( o rAi si n v i s i b l e ) 1 −αAl p h ab l e n d i n g .αi so p a c i t yi nt h i sc a s e . No t et h a ti nt h e s ee x a mp l e st h es u mo ft h ewe i g h t si s1 , s ot h a tt h ef i n a l c o l o rC i sb e t we e n0( b l a c k )a n d1( wh i t e )a swe l l .Aswe ' l l s e e , t h e r ea r ei n s t a n c e swh e r ey o uc a nd r o po u to ft h e s er a n g e s ;i ft h i sh a p p e n st h ev a l u e swi l l b ec l i p p e dt ot h es t a n d a r dr a n g e . 13.2.1. GBA Blending Ba c k g r o u n d sa r ea l wa y se n a b l e df o rb l e n d i n g .Toe n a b l es p r i t e b l e n d i n g , s e tOBJ_ATTR.attr0{ a } .Th e r ea r et h r e er e g i s t e r st h a t c o n t r o l b l e n d i n g , wh i c hu n f o r t u n a t e l yg ob yma n yd i f f e r e n tn a me s .Th eo n e sIu s ea r eREG_BLDCNT, REG_BLDALPHAa n d REG_BLDY.Ot h e rn a me sa r eREG_BLDMOD, REG_COLEVa n dREG_COLEY, a n ds o me t i me st h e‘ E’i nt h el a s tt woi sr e mo v e d .Be wa r n e d .An y wa y , t h ef i r s ts a y sh o wa n do nwh i c hl a y e r st h eb l e n ds h o u l db ep e r f o r me d , t h el a s tt woc o n t a i nt h ewe i g h t s .Oh , s i n c et h e GBAd o e s n ' td of l o a t i n gp o i n t , t h ewe i g h t sa r ef i x e d p o i n tn u mb e r si n1 . 4f o r ma t .St i l l l i mi t e db y0a n d1 , o fc o u r s e , s ot h e r ea r e1 7b l e n d l e v e l s . REG_BLDCNT (REG_BLDMOD) @ 0400:0050h FE D C B A 9 8 76 5 4 3 2 1 0 - bBD bOBJ bBG3 bBG2 bBG1 bBG0 BM aBD aObj aBG3 aBG2 aBG1 aBG0 bits name define 0 5 a BG0 - BLD_TOP# a BD file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm description Th eA( t o p )l a y e r s .BD, b yt h ewa y , i st h eback drop , as o l i dp l a n eo fc o l o r0 .Se tt h e b i t st oma k et h a tl a y e ru s et h eAwe i g h t s .No t et h a tt h e s el a y e r smu s ta c t u a l l yb ei n 117/331 28-03-13 Tonc : GBA Programming in rot13 6 7 8 D f r o n to ft h eBl a y e r s , o rt h eb l e n dwi l l f a i l . Blending mode . BM BLD_OFF, BLD_STD, BLD_WHI TE, BLD_BLACK, BLD_MODE# b BG0 - BLD_BOT# b BD 00: b l e n d i n gi so f f . 01: n o r ma l b l e n du s i n gt h ewe i g h t sf r o mREG_ALPHA. 10: b l e n dAwi t hwh i t e( f a d et owh i t e )u s i n gt h ewe i g h tf r o mREG_BLDY 11: b l e n dAwi t hb l a c k( f a d et ob l a c k )u s i n gt h ewe i g h tf r o mREG_BLDY Th eB( b o t t o m)l a y e r s .Us et h eBwe i g h t s .No t et h a tt h e s el a y e r smu s ta c t u a l l yl i e b e h i n dt h eAl a y e r s , o rt h eb l e n dwi l l n o two r k . Th eREG_BLDALPHAa n dREG_BLDYr e g i s t e r sh o l dt h eb l e n d i n gwe i g h t si nt h ef o r mo feva, evb a n dey, a l l i n1 . 4f i x e d p o i n tf o r ma t . An dn o , Id on o tk n o wwh yt h e ya r ec a l l e dt h a t ;t h e yj u s ta r e . REG_BLDALPHA (REG_COLEV) @ 0400:0052h FED CBA98 765 43210 - evb - eva bits name define description 0 4 e v a BLD_EVA# Top blend weight.On l yu s e df o rn o r ma l b l e n d i n g 8 C e v b BLD_EVB# Bottom blend weight.On l yu s e df o rn o r ma l b l e n d i n g REG_BLDY (REG_COLEY) @ 0400:0054h FEDCBA98765 43210 - bits name define 0 4 e y ey description BLDY# Top blend fade .Us e df o rwh i t ea n db l a c kf a d e s . 13.2.2. Blending caveats Bl e n d i n gi san i c ef e a t u r et oh a v e , b u tk e e pt h e s ep o i n t si nmi n d . Th eAl a y e r smust b ei nf r o n to ft h eBl a y e r s .On l yt h e nwi l l t h eb l e n da c t u a l l yo c c u r .Sowa t c hy o u rp r i o r i t i e s . I nt h ea l p h a b l e n dmo d e( mo d e1 )t h eb l e n dwi l l o n l yt a k ep l a c eo nt h eoverlapping, non-transparent p i x e l so fl a y e rAa n d l a y e rB.No n o v e r l a p p i n gp i x e l swi l l s t i l l h a v et h e i rn o r ma l c o l o r s . Sp r i t e sa r ea f f e c t e dd i f f e r e n t l yt h a nb a c k g r o u n d s .I np a r t i c u l a r , t h eb l e n dmo d es p e c i f i e db yREG_BLDCNT{ 6 , 7 }i sa p p l i e d o n l yt ot h enono v e r l a p p i n gs e c t i o n s( s ot h a te f f e c t i v e l yo n l yf a d i n gwo r k s ) .Fo rt h eo v e r l a p p i n gp i x e l s , t h es t a n d a r db l e n di s always i ne f f e c t , r e g a r d l e s so ft h ec u r r e n tb l e n d mo d e . I fy o ua r eu s i n gwi n d o ws , y o un e e dt os e tt h eb i t s5a n d / o r1 3i nREG_WI NI No rREG_WI NOUTf o rt h eb l e n d i n gt owo r k . 13.2.3. The obligatory demo // bld_demo.c // // // // bg 0, cbb 0, sbb 31, pb 15: text bg 1, cbb 2, sbb 30, pb 1: metroid bg 2, cbb 2, sbb 29, pb 0: fence oam 0: tile 0-63: obj metroid #include <stdio.h> #include <tonc.h> #include "../gfx/metr.h" void test_blend() { tte_printf("#{P:48,8}obj#{P:168,8}bg"); tte_set_margins(16, SCR_H-4-4*12, SCR_W-4, SCR_H-4); u32 mode=0; // eva, evb and ey are .4 fixeds // eva is full, evb and ey are empty u32 eva=0x80, evb= 0, ey=0; REG_BLDCNT= BLD_BUILD( file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 118/331 28-03-13 Tonc : GBA Programming in rot13 BLD_OBJ | BLD_BG0, // Top layers BLD_BG1, // Bottom layers mode); // Mode while(1) { vid_vsync(); key_poll(); // Interactive blend weights eva += key_tri_horz(); evb -= key_tri_vert(); ey += key_tri_fire(); mode += bit_tribool(key_hit(-1), KI_R, KI_L); // Clamp to allowable ranges eva = clamp(eva, 0, 0x81); evb = clamp(evb, 0, 0x81); ey = clamp(ey, 0, 0x81); mode= clamp(mode, 0, 4); tte_printf("#{es;P}mode :\t%2d\neva :\t%2d\nevb :\t%2d\ney :\t%2d", mode, eva/8, evb/8, ey/8); // Update blend mode BFN_SET(REG_BLDCNT, mode, BLD_MODE); // Update blend weights REG_BLDALPHA= BLDA_BUILD(eva/8, evb/8); REG_BLDY= BLDY_BUILD(ey/8); } } void load_metr() { // copy sprite and bg tiles, and the sprite palette memcpy32(&tile_mem[2][0], metrTiles, metrTilesLen/4); memcpy32(&tile_mem[4][0], metrTiles, metrTilesLen/4); memcpy32(pal_obj_mem, metrPal, metrPalLen/4); // set the metroid sprite OBJ_ATTR *metr= &oam_mem[0]; // use the first sprite obj_set_attr(metr, ATTR0_SQUARE | ATTR0_BLEND, ATTR1_SIZE_64, 0); obj_set_pos(metr, 32, 24); // mid-center // create the metroid bg // using inverted palette for bg-metroid int ix, iy; for(ix=0; ix<16; ix++) pal_bg_mem[ix+16]= pal_obj_mem[ix] ^ CLR_WHITE; SCR_ENTRY *pse= &se_mem[30][3*32+18]; // right-center for(iy=0; iy<8; iy++) for(ix=0; ix<8; ix++) pse[iy*32+ix]= iy*8+ix + SE_PALBANK(1); REG_BG0CNT= BG_CBB(0) | BG_SBB(30); } // set-up the fence background void load_fence() { // tile 0 / ' ' will be a fence tile const TILE fence= {{ 0x00012000, 0x00012000, 0x00022200, 0x22220222, 0x11122211, 0x00112000, 0x00012000, 0x00012000, }}; tile_mem[2][64]= fence; se_fill(se_mem[29], 64); file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 119/331 28-03-13 Tonc : GBA Programming in rot13 pal_bg_mem[0]= RGB15(16, 10, 20); pal_bg_mem[1]= RGB15( 0, 0, 31); pal_bg_mem[2]= RGB15(16, 16, 16); REG_BG2CNT= BG_CBB(2) | BG_SBB(29); } int main() { oam_init(oam_mem, 128); load_metr(); load_fence(); tte_init_chr4_b4_default(0, BG_CBB(0)|BG_SBB(31)); tte_init_con(); REG_DISPCNT= DCNT_MODE0 | DCNT_BG0 | DCNT_BG1 | DCNT_BG2 | DCNT_OBJ | DCNT_OBJ_1D; test_blend(); return 0; } Asa l wa y s , t h e r e ' sad e mot h a tg o e swi t ha l l t h i ss t u f f .bld_demof e a t u r e s2me t r o i d s( t h e l e f to n ei sas p r i t e , t h er i g h to n e( p a l e t t ei n v e r t e d )i so nb a c k g r o u n d0 )o naf e n c e l i k e b a c k g r o u n d( b g1t ob ep r e c i s e )a n dl e t sy o umo d i f yt h emo d e , a n dt h e3we i g h t s i n d e p e n d e n t l y .Th emo d e , b yt h ewa y , i sg i v e ni nt h et o pl e f tc o r n e r .Th ec o n t r o l sa r e : h a n g e seva.No t et h a tevai sa tma x i mu mi n i t i a l l y . left, right c h a n g e sevb. down,up c C h a n g e sey B,A a n g e smo d e . L,R Ch Fig 13.3: b l e n dd e mo ; mo d e =2 , e v a =0 ,e v b=0 , Th ef u n c t i o no fi n t e r e s ti stest_blend().Th i si swh e r et h ek e yh a n d l i n gt a k e sp l a c e e y = 1 0 . a n dwh e r et h eb l e n ds e t t i n g sa r ea l t e r e d .Si mi l a rt omos_demo, . 3f i x e d sa r eu s e df o rt h e b l e n dwe i g h tv a r i a b l e st os l o wt h er a t eo fc h a n g et omo r ec o mf o r t a b l el e v e l s .Tos e tt h e b l e n dr e g i s t e r st h e ms e l v e sI ' mu s i n gBUILD()ma c r o sa n dBF_SET(), wh i c hwo r kwe l l e n o u g hf o rt h e s ep u r p o s e s .I two u l db e t r i v i a l l ye a s yt owr i t ewr a p p e rf u n c t i o n sh e r eo fc o u r s e .Mo s to ft h ec o d ei sp r e t t ys t a n d a r d ;j u s tp l a ya r o u n dwi t ht h eb l e n dmo d e sa n d we i g h t sa n ds e ewh a th a p p e n s . Dot a k en o t eo fh o w, l i k eIs a i de a r l i e r , t h es p r i t eme t r o i di sa f f e c t e dd i f f e r e n t l yt h a nt h eb g me t r o i d .Th eb a c k g r o u n d b a c k g r o u n d b l e n db e h a v e se x a c t l ya st h emo d es a y si ts h o u l d ;t h es p r i t e , o nt h eo t h e rh a n d , a l wa y sh a sab l e n di ft h e yo v e r l a pwi t ht h ef e n c e ' sp i x e l s , a n dt h er e s to b e y st h emo d e , wh i c hi swh a tIt o l dy o ui nt h ec a v e a t s . 13.3. Windowing Wi n d o wi n ga l l o wsy o ut od i v i d et h es c r e e ni n t or e c t a n g u l a ra r e a sk n o wna s , we l l , wi n d o ws .Th e r ea r et wob a s i cwi n d o ws : win0 a n d win1.Th e r e ' sa l s oat h i r dt y p eo fwi n d o w, t h eobject wi n d o w.Th i sc r e a t e sawi n d o wo u to ft h ev i s i b l ep i x e l so ft h es p r i t e s .Yo uc a n e n a b l et h ewi n d o wsb ys e t t i n gREG_DISPCNT{ d , e , f } , r e s p e c t i v e l y . Ar e c t a n g u l a rwi n d o wi sd e f i n e db yi t sleft, right, top a n dbottom s i d e s .Un l e s sy o u ' r eo n eo fthose p e o p l e , wh ot h i n ki t ' sf u n n yt o s a yt h a tar e c t a n g l eh a so n l yt wos i d e s : a ni n s i d ea n da no u t s i d e .I nf a c t , t h i si st r u e rt h a ny o ut h i n k .Th eu n i o no fwi n 0a n dwi n 1i st h e inside wi n d o w.Th e r e ' sa l s ot h eoutside wi n d o w, wh i c hi se v e r y t h i n ge l s e .I no t h e rwo r d s : wi n I n=wi n 0| wi n 1 wi n Ou t=~( wi n I n ) Fig 13.4a: s h o wi n gwi n 0 , wi n 1a n dwi n _ o u t wi n do ws . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm Fig 13.4b: wi n 0i nr e d , wi n 1i ng r e e n , wi n I ni swi n 0| wi n 1( b l u ee d g e ) , wi n Outi ng r e y. 120/331 28-03-13 Tonc : GBA Programming in rot13 13.3.1. Window boundaries Bo t hwi n 0a n dwi n 1h a v e2r e g i s t e r st h a td e f i n et h e i rb o u n d a r i e s .I no r d e rt h e s ea r eREG_WIN0H( 0400:0040h) , REG_WIN1H ( 0400:0042h) , REG_WIN0V( 0400:0044h)a n dREG_WIN1V( 0400:0046h) , wh i c hh a v et h ef o l l o wi n gl a y o u t : REG_WINxH and REG_WINxV @ 0400:00400400:0047h reg FEDCBA98 76543210 REG_WINxH left right REG_WINxV top bottom bits name description 0 7 r i g h t Ri g h ts i d eo fwi n d o w( e x c l u s i v e ) 8 F l e f t Le f ts i d eo fwi n d o w( i n c l u s i v e ) 0 7 b o t t o m Bo t t o ms i d eo fwi n d o w( e x c l u s i v e ) 8 F t o p To ps i d eo fwi n d o w( i n c l u s i v e ) Soy o uh a v eo n eb y t ef o re a c hv a l u e .Th a t ' sb y t e sa si nunsigned c h a r s .Th ec o n t e n t so fawi n d o wa r ed r a wnf r o ms t a r t i n ga tt h et o p l e f tu pt o , b u tn o ti n c l u d i n g , t h eb o t t o mr i g h t .Wh a ty o uh a v et or e a l i z ei st h a tt h i si sa l s ot r u ewh e n , s a y , t h er i g h tv a l u ei sl o we rt h a nt h e l e f tv a l u e .I ns u c hac a s e , t h e r e ' sawr a p a r o u n da n de v e r y t h i n go nt h a tl i n ei si n s i d et h ewi n d o w, e x c e p tt h ep i x e l sb e t we e nRa n dL.I f b o t hR <L a n dB <T t h e ny o ug e tawi n d o wi nt h es h a p eo fac r o s s . 13.3.2. Window content Th ep o s s i b l ec o n t e n tf o rt h ewi n d o wsa r eb a c k g r o u n d s0 3a n do b j e c t s .Nos u p r i s et h e r e , r i g h t ?I nt o t a l , weh a v er e g i o n s : wi n 0 , wi n 1 , wi n Ou ta n dwi n Ob j .REG_WININ( 0400:0048h)c o n t r o l swi n 0a n dwi n 1 , REG_WINOUT( 0400:004ah)t a k e sc a r eo fwi n Ou ta n d wi n Ob j .Th e r e ' so n eb i tf o re a c hc o n t e n t t y p e , p l u so n ef o rb l e n d i n g , wh i c hy o uwi l l n e e di fy o ui n t e n dt ou s eb l e n d i n go nt h ec o n t e n t so f t h a tp a r t i c u l a rwi n d o w. register bits REG_WININ FE D C B A 9 8 76 5 4 3 2 1 0 - Bld Obj BG3 BG2 BG1 BG0 - Bld Obj BG3 BG2 BG1 BG0 - REG_WINOUT - define win1 - win0 winObj - winOut bits name 0 5 BGx , WI N_BGx , Windowing flags .Tob eu s e dwi t ha l l b y t e si nREG_WI NI Na n dREG_WI NOUT. Ob j , Bl d WI N_OBJ , WI N_BLD, WIN_LAYER# description Th e r ei sl i t t l ei nt h ewa yo fma c r o so rb i t d e f i n e sh e r eb e c a u s et h e y ' r en o tr e a l l yn e c e s s a r y .Doh a v et h e s ei ntonc_memdef.h t h o u g h : #define WIN_BUILD(low, high) ( ((high)<<8) | (low) ) \ #define WININ_BUILD(win0, win1) WIN_BUILD(win0, win1) #define WINOUT_BUILD(out, obj) WIN_BUILD(out, obj) Th e r ea r es t i l l af e wt h i n g sy o us h o u l dk n o wa b o u twi n d o ws .Fi r s to fa l l , wh e ny o ut u r no nwi n d o wi n gi nREG_DISPCNT, n o t h i n gwi l l s h o wu p .Th e r ea r et wor e a s o n sf o rt h i s .Fi r s t l y , t h eb o u n d a r yr e g i s t e r sa r ea l l 0 , s ot h ewh o l es c r e e ni sb a s i c a l l ywi n Ou t .Se c o n d l y , a n d t h i si sr e a l l yi mp o r t a n t : ab a c k g r o u n do ro b j e c twi l l o n l ys h o wu pi nt h ewi n d o wsi nwh i c hi ti se n a b l e d !Th i sme a n st h a tu n l e s sa tl e a s t some b i t sh a v eb e e ns e ti nREG_WININo rREG_WINOUTn o t h i n gwi l l s h o w.Th i sp r e s e n t sy o uwi t ha ne f f e c t i v ewa yo fh i d i n gs t u f f , a swe ' l l s e ei nt h ed e mo .Th e r ei sat h i r dt h i n gt h a ty o umu s tr e me mb e r , n a me l yt h a twi n 0t a k e sp r e c e d e n c eo v e rwi n 1 , wh i c hi nt u r n t a k e sp r e c e d e n c eo v e rwi n Ou t .I ' mn o ts u r eh o wwi n Ob j f i t si n t ot h i sy e t . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 121/331 28-03-13 Tonc : GBA Programming in rot13 Windowing necessities Toma k ewi n d o wi n gwo r kf o ry o u , y o un e e dt od ot h ef o l l o wi n gt h i n g s : En a b l ewi n d o wsi nREG_DISPCNT I n d i c a t ei nwh i c hwi n d o wy o uwa n tt h i n g st os h o wu pb ys e t t i n gt h ea p p r o p r i a t eb i t si nREG_WININa n d REG_WINOUT.Yo umust s e ta tl e a s ts o meb i t sh e r ei fy o uh a v ewi n d o wi n ge n a b l e d , o rn o t h i n gwi l l s h o wu pa t a l l ! Se tt h ed e s i r e dwi n d o ws i z e si nREG_WINxH/V.I fy o ud o n ' t , e v e r y t h i n gwi l l b ec o n s i d e r e di nt h eOu t wi n d o w. 13.3.3. Caveats Th e r e ' ss o me t h i n gr e a l l ywe i r dg o i n go nwh e ne i t h e rt h et o po rb o t t o mi so u t s i d eo ft h es c r e e n .Mu l t i p l es o me t h i n g si nf a c t , s e et h ed e mo on hardware! f o rd e t a i l s . I ft h et o pi si nt h e[ 2 9 , 0 r a n g e( i . e . , [ 2 2 7 , 2 5 5 ] ) , t h ewi n d o wwi l l not b er e n d e r e da ta l l .Li k e wi s e , i ft h eb o t t o mi si n s i d et h i s r a n g e , t h ewi n d o wc o v e r st h eh e i g h to ft h es c r e e n .Ic a n n o ts a ye x a c t l ywh a tt h ec a u s ei s , b u ts i n c et h eVCo u n ta l s os t o p sa t 2 2 7 , t h a tmi g h th a v es o me t h i n gt od owi t hi t . Al s o , i fy o umo v et h eb o t t o mf r o m, 1 6 1t o1 6 0 , t h ewi n d o wwi l l a l s oc o v e rt h ewh o l el e n g t h , b u to n l yf o raf r a meo rs o . Th ep o i n t sme n t i o n e da b o v ea s s u meT<B.I ft h et o pi sb i g g e r , t h e nt h ee f f e c ti sr e v e r s e d . Windowing weirdness not on emulators Th i sb e h a v i o u rd o e snot a p p e a ro nt h ee mu l a t o r sI ' v et e s t e do n . VBAc l i p st h ewi n d o ws , l i k ec o mmo ns e n s ewo u l dl e a dy o ut ob e l i e v e .( Ofc o u r s e , c o mmo ns e n s ea l s ot e l l sy o ut h a tt h eSu n o r b i t st h eEa r t ho rt h a tt h es t a r sa r ep i n p r i c k so nal a r g eb l a c kc a n v a s .Co mmo ns e n s ei sh a r d l yc o mmo n ) . Ma p p y VM a n dBo y c o t t Ad v a n c es i mp l yr e mo v et h ewi n d o wi fa n yo ft h eb o u n d a r i e sg o e so f ft h es c r e e n . 13.3.4. Demo: there's a rocket in my pocket I nc a s ey o uh a d n ' tn o t i c e dy e t , Il i k et h eMe t r o i ds e r i e s .Ir e a l l yl i k et h eMe t r o i ds e r i e s .I fy o uh a v ee v e rp l a y e dSu p e rMe t r o i dc h a n c e s a r et h a ty o u ' v eu s e dt h eXr a ys c o p e , wh i c hl e t ' sy o us e et h r o u g ht h el a y e r sa n df i n di t e msa n ds e c r e tp a s s a g e swi t hmu c hmo r ee a s e . Gu e s sh o wt h a twa sd o n e ?Yu p , wi n d o wi n g .Th ewi n d o wi n gd e mowin_demoe s s e n t i a l l yd o e st h es a met h i n g .Th e r e ' sar o c k e t i t e m h i d d e nb e h i n dt h eb a c k g r o u n dl a y e r sa n dy o uh a v ea nXr a yr e c t a n g l ewh i c hy o uc a nmo v ea r o u n dt h es c r e e ns oy o uc a nf i n di t . Th ec o n t r o l sa r es i mp l e : u s et h eDp a dt omo v et h ewi n d o wa r o u n d ;STARTr e p o s i t i o n st h er o c k e t .I ' v ea l s oa d d e df i n e rmo v e me n t ( A+Dp a d )s oy o uc a ns e et h es t r a n g eb e h a v i o u rt h ewi n d o wss e e mt oe x h i b i ta tc e r t a i np o s i t i o n s . v e st h er e c t a n g l e . dir Mo v er e c t a n g l eb yt a p p i n gf o rf i n e rc o n t r o l . A + dir Mo n d o ml yc h a n g et h ep o s i t i o no ft h er o c k e t . start Ra Wh a tf o l l o wsb e l o wi st h ema j o r i t yo ft h ed e mo ' sc o d e .Ih a v er e mo v e dt h ef u n c t i o n st h a ts e tu pt h eb a c k g r o u n d sa n ds p r i t eb e c a u s e t h e r e ' sn o t h i n gi nt h e mt h a ty o uh a v e n ' ts e e nb e f o r ea l r e a d y .Th ee a r l i e rf i g1 3 . 4 ai sas c r e e n s h o to ft h ed e moi na c t i o n . // win_demo.c // // // bg 0, cbb 0, sbb 2, pb 0: numbered forground bg 1, cbb 0, sbb 3, pb 0: fenced background oam 0: tile 0-3: rocket // // // win 0: objects win 1: bg 0 win out : bg 1 #include <tonc.h> #include "nums.h" #include "rocket.h" typedef struct tagRECT_U8 { u8 ll, tt, rr, bb; } ALIGN4 RECT_U8; // window rectangle regs are write only, so buffers are necessary // Objects in win0, BG 0 in win1 RECT_U8 win[2]= { { 36, 20, 76, 60 }, // win0: 40x40 rect { 12, 12 ,228, 148 } // win1: screen minus 12 margin. }; // gfx loaders omitted for clarity void init_front_map(); // numbers tiles file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 122/331 28-03-13 Tonc : GBA Programming in rot13 void init_back_map(); void init_rocket(); // fence // rocket void win_copy() { REG_WIN0H= win[0].ll<<8 | win[0].rr; REG_WIN1H= win[1].ll<<8 | win[1].rr; REG_WIN0V= win[0].tt<<8 | win[0].bb; REG_WIN1V= win[1].tt<<8 | win[1].bb; } void test_win() { win_copy(); while(1) { key_poll(); vid_vsync(); // key_hit() or key_is_down() 'switch' // A depressed: move on direction press (std movement) // A pressed : moves on direction hit (fine movement) int keys= key_curr_state(); if(key_is_down(KEY_A)) keys &= ~key_prev_state(); if(keys & KEY_RIGHT) { win[0].ll++; win[0].rr++; else if(keys & KEY_LEFT ) { win[0].ll--; win[0].rr--; if(keys & KEY_DOWN) { win[0].tt++; win[0].bb++; else if(keys & KEY_UP ) { win[0].tt--; win[0].bb--; } } } } // (1) randomize rocket position if(key_hit(KEY_START)) obj_set_pos(&oam_mem[0], qran_range(0, 232), qran_range(0, 152)); win_copy(); } } int main() { // obvious inits oam_init(); init_front_map(); init_back_map(); init_rocket(); // (2) windowing inits REG_DISPCNT= DCNT_BG0 | DCNT_BG1 | DCNT_OBJ | DCNT_OBJ_1D | DCNT_WIN0 | // Enable win 0 DCNT_WIN1; // Enable win 1 REG_WININ= WININ_BUILD(WIN_OBJ, (WIN_BG0); REG_WINOUT= WINOUT_BUILD(WIN_BG1, 0); win_copy(); // Initialize window rects test_win(); return 0; } I n i t i a l i z i n gt h ewi n d o wsi sd o n ea tp o i n t2 : b o t hwi n 0a n dwi n 1i nREG_DISPCNT, o b j e c t si nwi n0 , b g0i nwi n1a n db g 1i nwi n Ou t .Th e wi n d o ws ' s i z e sa r es e tu s i n gwin_copy()i ne a c hf r a me .Ia mu s i n gt wor e c t a n g l ev a r i a b l e st ok e e pt r a c ko fwh e r et h ewi n d o wsa r e , b e c a u s et h ewi n d o wr e c t a n g l er e g i s t e r st h e ms e l v e sa r ewr i t eo n l y .Se ef i g1 3 . 4a g a i nf o rt h er e s u l t . Us u a l l y , o b j e c t sa r es h o wni nf r o n to fb a c k g r o u n d s .Ho we v e r , b e c a u s eo b j e c t sa r en o wo n l ys e tt oa p p e a ri n s i d ewi n0 , t h e ya r e file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 123/331 28-03-13 Tonc : GBA Programming in rot13 e f f e c t i v e l yh i d d e ne v e r y wh e r ee l s e : y o uwi l l o n l ys e et h er o c k e to rp a r t so fi ti ft h er o c k e ta n dwi n0 ' sr e c t a n g l eo v e r l a p .Fu r t h e r mo r e , y o uwi l l n o t i c et h a tb e c a u s eonly o b j e c t sa r es e tf o rwi n0 , t h ewi n d o wi t s e l fi sc o mp l e t e l yb l a c k . Th er e s to ft h ed e moi sr a t h e ru n e v e n t f u l .Ic o u l de x p l a i nt h a tt h ewa yma s kt h ev a r i a b l ekeyswi t ht h ep r e v i o u sk e y s t a t ewh e nA i s h e l dd o wnl e t smes wi t c hb e t we e nt h ekey_hit()a n dkey_is_down()f u n c t i o n s , g i v i n gmet h ef u n c t i o n a l i t yIr e q u i r et os wi t c h b e t we e nd i r e c ta n df i n emo t i o nf o rt h eXr a ywi n d o w, b u ti t ' sn o ta l l t h a ti n t e r e s t i n ga n dq u i t eb e s i d e st h ep o i n to ft h ed e mo .Wh a t ' sa l s o b e s i d et h ep o i n to ft h ed e mo , b u tis i n t e r e s t i n gt ome n t i o n , i st h er a n d o mi z a t i o no ft h er o c k e t ' sp o s i t i o n . Random numbers Ra n d o mn u mb e r so nac o mp u t e ri sas o me wh a tq u a i n tn o t i o n .Th ewh o l ep o i n to fac o mp u t e ri st oh a v ear e l i a b l ec a l c u l a t o r , a n dr a n d o m n u mb e r sa r ep r e t t ymu c ht h ea n t i t h e s i so ft h a t .Co mp u t e rg e n e r a t e dr a n d o mn u mb e r sa r ea l s oc a l l e dpseudo-random, b e c a u s et h e y a r e n ' ti n t r i n s i c a l l yr a n d o m, j u s td e t e r mi n i s t i c a l l yg e n e r a t e dt oseem t h a twa y .Th e r ea r es t a t i s t i c a l t e s t st os e ei fag i v e nr o u t i n ei s s u f f i c i e n t l yr a n d o m.Ho we v e r , t h i si s n ' tn u c l e a rp h y s i c swe ' r et a l k i n ga b o u t , t h i si sg a mep r o g r a mmi n g .Wemo s t l yn e e ds o me t h i n gt h a t c a n , s a y , ma k ea ne n e myz i go rz a gwi t h o u ta n yd i s c e r n a b l ep a t t e r n ;t h a ti tc a nk i l l aMo n t eCa r l os i mu l a t i o ni st o t a l l yi r r e l e v a n t . On ec l a s so fg e n e r a t o r sa r elinear congruential generators, wh i c hf o l l o wt h ep a t t e r nNi+1=( a·Ni+c) %m, wi t hNi∈[ 0 , m .Wi t h p r o p e r l yp i c k e dp a r a me t e r sa, ca n dm, t h er o u t i n ec a nb eq u i t ea d e q u a t e .I fy o ue v e re n c o u n t e rarand()f u n c t i o ni na n yk i n do f s t a n d a r dl i b r a r y , c h a n c e sa r ei t ' so n eo ft h e s e .No to n l ya r et h e s ee a s yt oi mp l e me n t , t h e ya r el i k e l yt ob ef a s ta swe l l . Th ef o l l o wi n gr o u t i n eqran()i st a k e nf r o mmyn u me r i c a l me t h o d sb o o k , Nu me r i c a l Re c i p e s , p p2 7 5 , wh e r ei ti sl a b e l l e daq u i c k 3 2 a n dd i r t yg e n e r a t o r , b u ta na d e q u a t eo n e .Co n s i s t i n go fo n ea d d i t i o na n do n emu l t i p l y( m=2 , s od o n ea u t o ma t i c a l l y ) , i ti svery f a s t .Th e a c t u a l n u mb e rr e t u r n e da r et h et o p1 5b i t sf r o mN, b e c a u s et h eu p p e rb i t sa r ea p p a r e n t l ymo r er a n d o mt h a nt h el o we r , a n da l s ob e c a u s e 1 5g i v e sa[ 0 , 3 2 7 6 7 ]r a n g e , wh i c hi ss o me t h i n go fa nu n o f f i c i a l s t a n d a r d , AFAI K.No t et h a tt h e r ei sas e c o n df u n c t i o n , sqran()u s e d t oseed t h eg e n e r a t o r .Si n c et h ep r o c e s si t s e l fi ss t i l l d e t e r mi n i s t i c , y o un e e das e e dt oe n s u r et h a ty o ud o n ' tg e tt h es a mes e q u e n c ee v e r y t i me .Un l e s s , t h a ti s , y o ua c t u a l l ywant t h a tt oh a p p e n .Th i si s n ' ts u c has t r a n g ei d e ai fy o ut h i n ka b o u ti t : y o uc o u l du s ei tt og e n e r a t e ma p s , f o re x a mp l e .I n s t e a do fs t o r i n gt h ewh o l ema ps ot h a ti tl o o k st h es a mee v e r yt i mey o ul o a di t , y o uj u s ts t o r et h es e e da n dy o u ' r e d o n e .Th i si sh o wt h ep l a n e t a r yt e r r a i n si nSt a rCo n t r o l 2a r ema d e ;Iv e r ymu c hd o u b ti two u l dh a v eb e e np o s s i b l et os t o r eb i t ma p so fa l l t h e1 0 0 0 +p l a n e t si th a d .Th i si swh ysqran()a l s or e t u r n st h ec u r r e n tN, s oy o uc a nr e s e ti tl a t e ri fn e c e s s a r y . // from tonc_core.h/.c // A Quick (and dirty) random number generator and its seeder int __qran_seed= 42; // Seed / rnd holder // Seed routine int sqran(int seed) { int old= __qran_seed; __qran_seed= seed; return old; } //! Quick (and very dirty) pseudo-random number generator /*! \return random in range [0,8000h> */ INLINE int qran() { __qran_seed= 1664525*__qran_seed+1013904223; return (__qran_seed>>16) & 0x7FFF; } I ' l l s a ya g a i n , t h i si sn o tav e r ya d v a n c e dr a n d o mg e n e r a t o r , b u ti t ' l l b ee n o u g hf o rwh a tIn e e d .I fy o uwa n tab e t t e r( b u ts l o we r )o n e , t r y t h eMe r s e n n eTwi s t e r .Yo uc a nf i n dan i c ei mp l e me n t a t i o no ni to nPERN' sn e ws p r i t ep a g e . Ranged random numbers Ge t t i n gar a n d o mn u mb e ri so n et h i n g ;g e t t i n gar a n d o mn u mb e ri nap a r t i c u l a rr a n g ei sa n o t h e r .I ts e e mss i mp l ee n o u g h , o fc o u r s e : f o ra n u mb e rb e t we e n , s a y , 0a n d2 4 0y o u ' du s emo d u l o2 4 0 .Ho we v e r , a st h eGBAd o e s n ' th a v eah a r d wa r ed i v i d e , i t ' l l c o s tq u i t ean u mb e r o fc y c l e s .Fo r t u n a t e l y , t h e r ei sas i mp l ewa yo u to fi t . Is a i dt h a tqran(), l i k es t d l i b ' srand()h a sar a n g eb e t we e n0a n d0 x 8 0 0 0 .Yo uc a na l s os e et h i sa sar a n g eb e t we e n0a n d1 , i f y o ui n t e r p r e tt h e ma s. 1 5f i x e dp o i n tn u mb e r s .Bymu l t i p l y i n gwi t h2 4 0 , y o u ' l l h a v et h ed e s i r e dr a n g e dr a n d o mn u mb e r , a n di to n l yc o s t sa mu l t i p l i c a t i o na n das h i f t .Th i st e c h n i q u ewo r k sf o re v e r yr a n d o mn u mb e rg e n e r a t o r , a sl o n ga sy o up a ya t t e n t i o nt oi t sma x i mu mr a n g e a n di n t e g e ro v e r f l o w( wh i c hy o us h o u l dp a ya t t e n t i o nt oa n y wa y ) .To n c l i b ' sv e r s i o no ft h i si sc a l l e dqran_range(). //! Ranged random number /*! \return random in range [\a min, \a max> * \note (max-min) must be lower than 8000h */ INLINE int qran_range(int min, int max) { return (qran()*(max-min)>>15)+min; } file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 124/331 28-03-13 Tonc : GBA Programming in rot13 I nt h ed e mo , I ' mu s i n gqran_range()t wi c et ok e e pt h es p r i t ep o s i t i o ni n s i d et h es c r e e na ta l l t i me s .Wh i l et h ep o s i t i o ni t s e l fc o u l db e p r e d i c t e db e f o r e h a n dwi t hs o mei n v e s t i g a t i o n , Id o n ' tt h i n ki t ' l l b et h a te a s y .An di fy o ur e a l l yp u tt h a tk i n do fe f f o r ti ni t , I ' ds a yy o u wo u l dd e s e r v es o me t h i n gf o ry o u rt r o u b l e s .I fy o ur e l o a dt h ed e moaf e wt i me s , y o uwi l l n o t i c et h a tt h es e q u e n c eo fp o s i t i o n si sa l wa y s t h es a me .Th i si swh yt h e y ' r ec a l l e dpseudor a n d o m.Tog e tad i f f e r e n ts e q u e n c e , t h es e e dv a l u es h o u l db ed i f f e r e n t .Ih a v e n ' te v e n s e e d e di to n c eh e r eb e c a u s ei t ' sn o tr e a l l yi mp o r t a n tf o rt h i s , b u tt h eu s u a l t r i c kt os e e di twi t hs o me t h i n gi n v o l v i n gt i me : f o re x a mp l e , t h e n u mb e ro ff r a me so rc y c l e sb e f o r eo n ea c t u a l l ys t a r t sag a me , c o u n t e df r o mt h ev a r i o u si n t r os c r e e n st h a tma yp r e c e d ei t .Ev e nas ma l l d i f f e r e n c ei nt h es e e dc a np r o d u c ewi l d l yv a r y i n gs e q u e n c e s . 13.4. Conclusions Te c h n i c a l l ys p e a k i n gy o up r o b a b l ywo n ' tr e a l l yn e e dmo s a i c , b l e n d i n go rwi n d o wi n gi ng a me s , b u tt h e y ' r eg r e a tf o rs u b t l ee f f e c t s , l i k ea ‘ s h o c k h i t ’o rs p o t l i g h t s .Th e y ' r ea l s oo fg r e a tu s ef o rv a r i o u st y p e so fs c e n et r a n s i t i o n s ;af a d et ob l a c kc a nb ee a s i l yi mp l e me n t e du s i n g t h eb l e n dr e g i s t e r s .Al s of u na r ev a r i o u sHBl a n ke f f e c t su s i n gwi n d o ws , c h a n g i n gt h er e c t a n g l e se v e r yHBl a n kt og i v eb e a ms , s i d e wa y wi p e so rc i r c l u l a rwi n d o ws .Ho we v e r , t ob ea b l et od ot h a ty o un e e dt ok n o wh o wt owo r kwi t hi n t e r r u p t s .Oras p e c i a l c a s eo fDMA k n o wna sHDMA, wh i c hj u s th a p p e n st ob eu pn e x t . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 125/331 28-03-13 Tonc : GBA Programming in rot13 14. Direct Memory Access DMA… q u e ? . DMAr e g i s t e r s . So meDMAr o u t i n e s . DMAd e mo: c i r c u l a rwi n d o ws . 14.1. DMA … que? Direct Memory Access ( DMA)i sf a s twa yo fc o p y i n gd a t af r o mo n ep l a c et oa n o t h e r .Or , r a t h e r , awa yo ftransferring d a t af a s t ;a si t c a nb eu s e df o rc o p y i n gd a t a , b u ta l s of i l l i n gme mo r y .Wh e ny o ua c t i v a t eDMAt h es o c a l l e dDMA controller t a k e so v e rt h eh a r d wa r e ( t h eCPUi sa c t u a l l yh a l t e d ) , d o e st h ed e s i r e dt r a n s f e ra n dh a n d sc o n t r o l b a c kt ot h eCPUb e f o r ey o ue v e nk n e wi twa smi s s i n g . Th e r ea r ef o u rDMAc h a n n e l s .Ch a n n e l 0h a st h eh i g h e s tp r i o r i t y ;i ti su s e df o rt i me c r i t i c a l o p e r a t i o n sa n dc a no n l yb eu s e dwi t h i n t e r n a l RAM.Ch a n n e l s1a n d2a r eu s e dt ot r a n s f e rs o u n dd a t at ot h er i g h ts o u n db u f f e r sf o rp l a y b a c k .Th el o we s tp r i o r i t yc h a n n e l , c h a n n e l 3 , i sf o rg e n e r a l p u r p o s ec o p i e s .On eo ft h ep r i ma r yu s e sf o rt h i sc h a n n e l i sl o a d i n gi nn e wb i t ma po rt i l ed a t a . 14.2. DMA registers Ev e r yk i n do ft r a n s f e rr o u t i n en e e d s3t h i n g s : as o u r c e , ad e s t i n a t i o na n dt h ea mo u n to fd a t at oc o p y .Th ewhence, whither a n dhow much.Fo rDMA, t h es o u r c ea d d r e s si sp u ti n t oREG_DMAxSADa n dd e s t i n a t i o na d d r e s si n t oREG_DMAxDAD.At h i r dr e g i s t e r , REG_DMAxCNT, n o to n l yi n d i c a t e st h ea mo u n tt ot r a n s f e r , b u ta l s oc o n t r o l so t h e rf e a t u r e sp o s s i b l ef o rDMA, l i k ewh e ni ts h o u l ds t a r t t h et r a n s f e r , c h u n k s i z e , a n dh o wt h es o u r c ea n dd e s t i n a t i o na d d r e s s e ss h o u l db eu p d a t e da f t e re a c hi n d i v i d u a l c h u n ko fd a t a .Al l t h e DMAr e g i s t e r sa r e3 2 b i t si nl e n g t h , t h o u g ht h e yc a nb ed i v i d e di n t ot wo1 6 b i tr e g i s t e r si fs od e s i r e d .Th o s eo fc h a n n e l 0s t a r t a t 0400:00B0h;s u b s e q u e n tc h a n n e l ss t a r ta ta no f f s e to f1 2( s e et a b l e1 ) . reg function address REG_DMAxSADs o u r c e 0400:00B0h + 0Ch· x REG_DMAxDADd e s t i n a t i o n0400:00B4h + 0Ch· x REG_DMAxCNTc o n t r o l 0400:00B8h + 0Ch· x Table 14.1: DMAr e g i s t e ra d d r e s s e s 14.2.1. DMA controls Th eu s eo ft h es o u r c ea n dd e s t i n a t i o nr e g i s t e r ss h o u l db eo b v i o u s .Th ec o n t r o l r e g i s t e rn e e d ss o mee x p l a i n i n g .Al t h o u g ht h e REG_DMAxCNTr e g i s t e r st h e ms e l v e sa r e3 2 b i t s , t h e ya r eo f t e ns p l i ti n t ot wos e p a r a t er e g i s t e r s : o n ef o rt h ec o u n t , a n do n ef o rt h ea c t u a l c o n t r o l b i t s . REG_DMAxCNT @ 0400:00B8+12x 1F 1E 1D 1C 1B 1A 19 18 17 16 15 14 13 12 11 10 F E D C B A 9 8 7 6 5 4 3 2 1 0 En I TM - CS R DA - N bits name 0 0 0 F N 1 5 1 6 DA DMA_DST_I NC, Destination adjustment. DMA_DST_DEC, 00: i n c r e me n ta f t e re a c ht r a n s f e r( d e f a u l t ) DMA_DST_FI XED, 01: d e c r e me n ta f t e re a c ht r a n s f e r DMA_DST_RELOAD 10: n o n e ;a d d r e s si sf i x e d 11: i n c r e me n tt h ed e s t i n a t i o nd u r i n gt h et r a n s f e r , a n dr e s e ti ts o t h a tr e p e a tDMAwi l l a l wa y ss t a r ta tt h es a med e s t i n a t i o n . 1 7 1 8 SA DMA_SRC_I NC, r k sj u s tl i k et h et wob i t sf o rt h ed e s t i n a t i o n .No t e Source Adjustment.Wo DMA_SRC_DEC, t h a tt h e r ei sn oDMA_SRC_RESET;c o d e3f o rs o u r c ei sf o r b i d d e n . DMA_SRC_FI XED, 1 9 R DMA_REPEAT 1 A CS DMA_1 6 , DMA_3 2 Chunk Size .Se t sDMAt oc o p yb yh a l f wo r d( i fc l e a r )o rwo r d( i fs e t ) . 1 C1 D TM define SA description ft r a n s f e r s . Number o Re p e a t st h ec o p ya te a c hVBl a n ko rHBl a n ki ft h eDMAt i mi n gh a sb e e ns e t t ot h o s emo d e s . DMA_NOW, e c i f i e swh e nt h et r a n s f e rs h o u l ds t a r t . Timing Mode .Sp DMA_AT_VBLANK, 00: s t a r ti mme d i a t e l y . DMA_AT_HBLANK, 01: s t a r ta tVBl a n k . DMA_AT_REFRESH file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 126/331 28-03-13 Tonc : GBA Programming in rot13 10: s t a r ta tHBl a n k . 11: Ne v e ru s e di ts of a r , b u th e r e ' sh o wIg a t h e ri two r k s .Fo r DMA1a n dDMA2i t ' l l r e f i l l t h eFI FOwh e ni th a sb e e ne mp t i e d . Co u n ta n ds i z ea r ef o r c e dt o1a n d3 2 b i t , r e s p e c t i v e l y .Fo rDMA3 i twi l l s t a r tt h ec o p ya tt h es t a r to fe a c hr e n d e r i n gl i n e , b u twi t ha2 s c a n l i n ed e l a y . 1 E I DMA_I RQ i s ea ni n t e r r u p twh e nf i n i s h e d . Interrupt request.Ra 1 F En DMA_ENABLE h eDMAt r a n s f e rf o rt h i sc h a n n e l . Enable t 14.2.2. Source and destination addresses Th er e g i s t e r sf o rs o u r c ea n dd e s t i n a t i o na d d r e s s e swo r kj u s ta sy o u ' de x p e c t : j u s tp u ti nt h ep r o p e ra d d r e s s e s .Oh , Is h o u l dt e l l y o ut h a t t h es i z e sf o rt h es o u r c ea n dd e s t i n a t i o na d d r e s s e sa r e2 8a n d2 7b i t swi d e , r e s p e c t i v e l y , a n dn o tt h ef u l l 3 2 .Th i si sn o t h i n gt owo r r ya b o u t t h o u g h , y o uc a n ' ta c c e s sa d d r e s s e sa b o v e1000:0000ha n y wa y .Fo rd e s t i n a t i o na d d r e s s e s , y o uc a n ' tu s et h es e c t i o na b o v e 0800:0000h.Bu tt h e n , b e i n ga b l et oc o p yt oROM wo u l db ek i n do fs t r a n g e , wo u l d n ' ti t ? 14.2.3. DMA flags Th eREG_DMAxCNTr e g i s t e r sc a nb es p l i ti nt wop a r t s : o n ewi t ha c t u a l f l a g s , a n do n ef o rt h en u mb e ro fc o p i e st od o .Ei t h e rwa ywi l l wo r kb u ty o umu s tb ec a r e f u l h o wt h ef l a g sa r ed e f i n e d : u s i n g3 2 b i t#d e f i n e sf o r1 6 b i tr e g i s t e r so rv i c ev e r s ai sn o tag o o di d e a . Th e r ea r eo p t i o n st oc o n t r o l wh a twi l l b et h en e x ts o u r c ea n dd e s t i n a t i o na d d r e s s e swh e no n ec h u n kh a sb e e nt r a n s f e r r e d .By d e f a u l t , b o t hwi l l i n c r e me n ts ot h a ti two r k sa sac o p i e r .Bu ty o uc o u l da l s ok e e pt h es o u r c ec o n s t a n ts ot h a ti t ' dwo r kmo r ea same mo r y f i l l . Wh a tg o e si n t ot h el o we rh a l fo fREG_DMAxCNTi st h en u mb e ro ft r a n s f e r s .Th i si st h en u mb e ro fchunks, n o tb y t e s !Be s tb e c a r e f u l wh e nu s i n gsizeof()o rs o me t h i n gs i mi l a rh e r e , mi s s i n gaf a c t o r2o r4i sv e r ye a s y .Ac h u n kc a nb ee i t h e r1 6o r3 2b i t , d e p e n d i n go nb i t2 6 . 14.2.4. More on DMA timing Wh a tt h ei mme d i a t eDMAd o e si se a s yt oi ma g i n e , i two r k sa ss o o na sy o ue n a b l et h eDMA.We l l actually i tt a k e s2c y c l e sb e f o r ei t ' l l s e ti n , b u ti t ' sc l o s ee n o u g h .Th eo t h e rt i mi n gs e t t i n g sa r e n ' tt h a tmo r ed i f f i c u l tc o n c e p t u a l l y , b u tt h e r ei so n ep o i n to fc o n f u s i o n . Co n s i d e rt h ef o l l o wi n gs i t u a t i o n : y o uwa n tt od os o me t h i n gc o o l t oy o u ro t h e r wi s es t a n d a r db a c k g r o u n d s ;s p e c i f i c a l l y , y o uwa n tt od o s o me t h i n gt h a tr e q u i r e st h eb a c k g r o u n dr e g i s t e r st ob eu p d a t e de v e r ys c a n l i n e .Ij u s ts a i dt h a ty o uc a nc o p yd a t aa te v e r yHBl a n k( v i a t h eDMA_AT_HBLANKt i mi n gf l a g ) , wh i c hs e e msp e r f e c tf o rt h ej o b .I fy o ut h i n ka b o u ti tf o rami n u t e , h o we v e r , y o uma ya s ky o u r s e l f t h ef o l l o wi n gq u e s t i o n : Wh e ny o us e tt h et i mi n gt o , s a y , DMA_AT_HBLANK, d o e si td oall t h eN c o p i e sa tt h en e x tHBl a n k , o ro n ec o p ya te a c h HBl a n ku n t i l t h el i s ti sd o n e ? Th e r ei sac r u c i a l d i f f e r e n c eb e t we e nt h et wo .Th ef i r s to p t i o ns e e msp o i n t l e s sb e c a u s ea l l c o p i e dwo u l db ed o n ea to n c e ;i fy o u r d e s t i n a t i o ni sf i x e d( l i k et h e ya r ef o rb a c k g r o u n dr e g i s t e r s ) , a l l c o p i e se x c e p tt h el a s two u l db el o s t .I nt h ec a s eo ft h es e c o n do n e , h o w wo u l dy o ud omo r et h a no n ec o p yp e rHBl a n k ?Cl e a r l y , s o me t h i n g ' sa mi s sh e r e .Th e r ei s , o nt woc o u n t s . Fo rt h er e c o r d , I ' mn o t1 0 0 %s u r ea b o u twh a tI ' mg o i n gt os a yh e r e , b u tIt h i n ki t ' sp r e t t yc l o s et owh a t ' sa c t u a l l yg o i n go n .Th ema i n t h i n gt or e a l i z ei st h a ta sl o n ga st h ec h a n n e l i sn o te n a b l e d( REG_DMAxCNT{ 1 f }i sc l e a r e d ) , t h a tc h a n n e l wo n ' td os q u a t ;o n l ya f t e r REG_DMAxCNT{ 1 f }h a sb e e ns e twi l l t h eDMAp r o c e s sb ei n i t i a t e d .Att h ea p p r o p r i a t et i me( d e t e r mi n e db yt h et i mi n gb i t s ) , DMAwi l l d oa l l Nc o p i e sa n dt h e ns h u ti t s e l fo f fa g a i n . Un l e s s , t h a ti s , t h er e p e a t b i t( REG_DMAxCNT{ 1 9 } )i ss e t .I nt h a tc a s e , i twi l l k e e pd o i n gt h ec o p i e sa tt h er i g h tt i meu n t i l y o u d i s a b l et h ec h a n n e l y o u r s e l f . 14.3. Some DMA routines Wh i l ei t ' sn o tt h a tmu c ht r o u b l et os e tt h et h r e er e g i s t e r sma n u a l l y , i ti sp r e f e r a b l et oh i d et h ed i r e c ti n t e r a c t i o ni ns u b r o u t i n e s .No w, i n o l d e rc o d e , y o umi g h tc o mea c r o s ss o me t h i n gl i k et h i s : // Don't do this. Please. void dma_copy(int ch, void* src, void* dest, uint count, u32 mode) { switch(ch) { case 0: // set DMA 0 case 1: // set DMA 1 ... // etc } } file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 127/331 28-03-13 Tonc : GBA Programming in rot13 Th i swi l l wo r k , b u ti t ' sn o tan i c ewa yo fd o i n gt h i n g s .I fy o u rs wi t c h c a s e sd i f f e rb yas i n g l en u mb e r , y o uc a nu s u a l l yr e p l a c ei tb ya s i mp el o o k u p .Th e r ea r ean u mb e ro fwa y so ff i x i n gt h i s , b u tt h ee a s i e s ti sb yma p p i n gas t r u c ta r r a yo v e rt h eDMAr e g i s t e r s , s i mi l a rt o wh a tId i df o rt i l eme mo r y .Af t e rt h a t , y o uc a nj u s ts e l e c tt h ec h a n n e l wi t ht h ec h a n n e l v a r i a b l ea n ds i mp l yf i l l i nt h ea d d r e s s e sa n df l a g s . typedef struct DMA_REC { const void *src; void *dst; u32 cnt; } DMA_REC; #define REG_DMA ((volatile DMA_REC*)0x040000B0) Th ef o l l o wi n ga r emyt h r e eo fmyDMAr o u t i n e s .Fi r s tt h eDMA_TRANSER()ma c r o , wh i c hi st h eo v e r a l l ma c r ot h a tc a nb eu s e df o r a n y t h i n g .Th e nt wor o u t i n e sf o rg e n e r a l me mo r yc o p i e sa n df i l l su s i n g3 2 b i tt r a n s f e r swi t hDMA3 . // in tonc_core.h //! General DMA transfer macro #define DMA_TRANSFER(_dst, _src, count, ch, mode) do { \ REG_DMA[ch].cnt= 0; \ REG_DMA[ch].src= (const void*)(_src); \ REG_DMA[ch].dst= (void*)(_dst); \ REG_DMA[ch].cnt= (count) | (mode); \ } while(0) \ //! General DMA copier INLINE void dma_cpy(void *dst, const void *src, uint count, int ch, u32 mode) { REG_DMA[3].cnt = 0; // shut off any previous transfer REG_DMA[3].src = src; REG_DMA[3].dst = dst; REG_DMA[3].cnt = count; } //! General DMA full routine INLINE void dma_fill(void *dst, volatile u32 src, uint count, int ch, u32 mode) { REG_DMA[3].cnt = 0; // shut off any previous transfer REG_DMA[3].src = (const void*)&src; REG_DMA[3].dst = dst; REG_DMA[3].cnt = count | DMA_SRC_FIXED; } //! Word copy using DMA 3 INLINE void dma3_cpy(void *dst, const void *src, u32 size) { dma_cpy(dst, src, size/4, 3, DMA_CPY32); } //! Word fill using DMA 3 INLINE void dma3_fill(void *dst, const void *src, u32 size) { dma_fill(dst, src, size/4, 3, DMA_CPY32); } I na l l c a s e s , Id i s a b l ea n yp r e v i o u s l yo p e r a t i n gt r a n s f e r sf i r s t .Th i sma ys e e mr e d u n d a n ti fDMAs t o p st h eCPU, b u tr e me mb e rt h a t DMAt r a n s f e r sc a nb et i me da swe l l –y o uwo u l d n ' twa n ti tt os t a r ti nt h emi d d l eo fs e t t i n gt h er e g i s t e r s .Af t e rt h a t , i t ' ss i mp l yama t t e r o ff i l l i n gt h er e g i s t e r s .No w, i ts oh a p p e n st h a tt h e r ei sa2 c y c l ed e l a yb e f o r ea n yt r a n s f e rr e a l l yb e g i n s .Th i sme a n st h a ty o uc o u l dl o s e at r a n s f e ri fy o ua s kf o rt r a n s f e r si ni mme d i a t es u c c e s s i o n .I ' mn o ts u r ei ft h i si sv e r yl i k e l yt h o u g h : me mo r ywa i t s t a t e st h e ms e l v e s a l r e a d yt a k et h a tmu c ht i mes oy o ushould b es a f e . Ot h e rn o t e so nt h e s er o u t i n e s : t h eDMA_TRANSFER()ma c r o ' sc o d es i t sb e t we e nado {} while(0);l o o p .Th ep r o b l e mwi t h ma c r o si st h a twh e ne x p a n d e dmu l t i p l es t a t e me n t smi g h tb r e a kn e s t i n g b l o c k s .Fo re x a mp l e , c a l l i n gi ti nt h eb o d yo fa nifwi t h o u t b r a c e sa r o u n di two u l dh a v et h ef i r s tl i n ea st h eb o d y , b u tt h er e s tf a l l o u t s i d ei t .Th i sk i n do fl o o pi so n eo ft h ewa y so fp r e v e n t i n gt h a t . An o t h e rp r o b l e mwi t hma c r o si st h a ti ft h ea r g u me n t s ' n a me sma yh i d eo t h e rp a r t so ft h ema c r o ' sc o d e .Li k et h esrca n ddst me mb e r so ft h eDMA_RECs t r u c t ;wh i c hi swh yt h e y ' r eu n d e r s c o r e d .Th ef i l l r o u t i n e sa l s oh a v es o me t h i n gr e ma r k a b l eg o i n go n , wh i c h y o uc a nr e a da b o u ti nt h en e x ts u b s e c t i o n .La s t l y , t h edma3i n l i n e su s ewo r d t r a n s f e r sa n dt a k et h eb y t e s i z ea st h e i rl a s ta r g u me n t s , ma k i n gt h e mv e r ys i mi l a rt ot h es t a n d a r dmemcpy()a n dmemset(). Iu s e dt oh a v et h ef o l l o wi n gma c r of o rmyt r a n s f e r s .I tu s e so n eo ft h emo r ee x o t i cc a p a b i l i t i e so ft h ep r e p r o c e s s o r : t h eme r g i n g o p e r a t o r‘ ##’ , wh i c ha l l o wsy o ut oc r e a t es y mb o l n a me sa tc o mp i l e t i me .I t ' ss c a r y , t o t a l l yu n s a f ea n dg e n e r a l l yu n r u l y , b u ti td o e swo r k . Th eo t h e rma c r oIg a v ei sb e t t e r , b u tIs t i l l l i k et h i st h i n gt o o . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 128/331 28-03-13 Tonc : GBA Programming in rot13 #define DMA_TRANSFER(_dst, _src, _count, _ch, _mode) REG_DMA##_ch##SAD = (u32)(_src), REG_DMA##_ch##DAD = (u32)(_dst), REG_DMA##_ch##CNT = (_count) | (_mode) \ \ \ \ Asl o n ga sy o ua r eu s i n gal i t e r a l n u mb e rf o r_chi t ' l l f o r mt h ec o r r e c tr e g i s t e rn a me s .An dy e s , t h o s ec o mmao p e r a t o r sb e t we e nt h e s t a t e me n t sa c t u a l l ywo r k .Th e yk e e pt h es t a t e me n t ss e p a r a t e , a n da l s og u a r da g a i n s twr o n g f u l n e s t i n gj u s tl i k et h edo{} while(0) c o n s t r u c td o e s . 14.3.1. On DMA fills DMAc a nb eu s e dt of i l l me mo r y , b u tt h e r ea r et wop r o b l e mst h a ty o un e e dt ob ea wa r eo fb e f o r ey o ut r yi t .Th ef i r s tc a nb ec a u g h tb y s i mp l yp a y i n ga t t e n t i o n .DMAf i l l sd o n ' two r kquite i nt h es a mewa ya smemset()d o e s .Wh a ty o up u ti n t oREG_DMAxSADi s n ' tt h e v a l u ey o uwa n tt of i l l wi t h , b u ti t saddress! “ Ve r ywe l l , I ' l l p u tt h ev a l u ei nav a r i a b l ea n du s ei t sa d d r e s s . ”Ye s , a n dt h a tb r i n g su st oo u rs e c o n dp r o b l e m, ab u gwh i c hi sa l mo s t i mp o s s i b l et of i n d .I fy o ut r yt h i s , y o u ' l l f i n dt h a ti td o e s n ' two r k .We l l i tf i l l swi t hsomething, b u tu s u a l l yn o twh a ty o uwa n t e dt of i l l wi t h . Th ef u l l e x p l a n a t i o ni ss o me wh a tt e c h n i c a l , b u tb a s i c a l l yb e c a u s ey o u ' r ep r o b a b l yo n l yu s i n gt h ev a r i a b l e ' sa d d r e s sa n dn o ti t svalue, t h e o p t i mi z e rd o e s n ' te v e ri n i t i a l i z ei t .Th e r ei sas i mp l es o l u t i o n , o n et h a twe ' v es e e nb e f o r e , ma k ei tv o l a t i l e .Ory o uc a nu s ea( i n l i n e ) f u n c t i o nl i k edma_fill(), wh i c hh a si t ss o u r c ea r g u me n ts e ta sv o l a t i l es oy o uc a nj u s ti n s e r tan u mb e rj u s ta sy o u ' de x p e c t .No t e t h a ti fy o ur e mo v et h ev o l a t i l ek e y wo r dt h e r e , i t ' l l f a i l a g a i n . I ns h o r t : DMAf i l l sn e e da d d r e s s e s , n o td i r e c tv a l u e s .Gl o b a l swi l l a l wa y swo r k , b u ti fy o uu s el o c a l v a r i a b l e so ra r g u me n t sy o u ' l l n e e dt o ma k et h e mv o l a t i l e .No t et h a tt h es a met h i n gh o l d st r u ef o rt h eBI OSc a l l Cp u Fa s t Se t ( ) . 14.3.2. DMA; don't wear it out DMAi sf a s t , t h e r e ' sn oq u e s t i o na b o u tt h a t .I tc a nb eu pt ot e nt i me sa sf a s ta sa r r a yc o p i e s .Ho we v e r , t h i n kt wi c ea b o u tu s i n gi tf o r e v e r yc o p y .Wh i l ei ti sf a s t , i td o e s n ' tq u i t eb l o we v e r yo t h e rt r a n s f e rr o u t i n eo u to ft h ewa t e r .Cp u Fa s t Se t ( )c o me swi t h i n1 0 %o fi tf o r c o p i e sa n di sa c t u a l l y1 0 %f a s t e rf o rf i l l s .Th es p e e dg a i ni s n ' tt h a tb i gad e a l .An o t h e rp r o b l e mi st h a ti ts t o p st h eCPU, wh i c hc a n s c r e wu pi n t e r r u p t s , c a u s i n gs e e mi n g l yr a n d o mb u g s .I td o e sh a v ei t ss p e c i f i cu s e s , u s u a l l yi nc o n j u n c t i o nwi t ht i me r so ri n t e r r u p t s , b u t f o rg e n e r a l c o p i e s , y o umi g h tc o n s i d e ro t h e rt h i n g sa swe l l .Cp u Fa s t Se t ( )i sag o o dr o u t i n e , b u tt o n c l i ba l s oc o me swi t h memcpy16()/32()a n dmemset16()/32()r o u t i n e st h a ta r es a f e rt h a nt h a t , a n dl e s sr e s t r i c t i o n s .Th e ya r ea s s e mb l yr o u t i n e s , t h o u g h , s oy o u ' l l n e e dt ok n o wh o wt oa s s e mb l eo ru s el i b r a r i e s . 14.4. DMA demo : circular windows Th ed e mof o rt h i sc h a p t e rma ys e e mal i t t l ec o mp l i c a t e d , b u tt h ee f f e c ti swo r t hi t .Th e b a s i cu s eo fDMAt r a n s f e r si ss oe a s yt h a ti t ' sh a r d l ywo r t hh a v i n gad e moo f .Us eo f triggered DMAi sa n o t h e rma t t e r .I nt h i sc a s e , we ' l l l o o ka tHBl a n k t r i g g e r e dDMA, o r HDMAf o rs h o r t .We ' l l u s ei tt ou p d a t et h ewi n d o ws i z e di n s i d et h eHBl a n kt og i v ea c i r c u l a rwi n d o we f f e c t . Th i si se a s i e rs a i dt h a nd o n e , o fc o u r s e .Th ef i r s ts t e pi nt h ed e s i g ni sh o wt ou s e HDMAf o rt h i si nt h ef i r s tp l a c e .Be c a u s ewen e e dt oc o p yt oREG_WIN0He a c hHBl a n k , wen e e dt ok e e pt h ed e s t i n a t i o nf i x e d .Te c h n i c a l l y , i tn e e d st ob ereset t ot h eo r i g i n a l d e s t i n a t i o n , b u twi t ho n l yo n eh a l f wo r dt oc o p yt h i sme a n st h es a met h i n g .Fo rt h es o u r c e , we ' l l k e e pt r a c ko ft h ed a t at h a tn e e d st ob ec o p i e dt h e r ei na na r r a ywi t ho n ee n t r yf o r Fig 14.1: pa l e t t ef o rdma_demo. e a c hs c a n l i n e , a n dwe ' l l p r o g r e s st h r o u g ht h ea r r a yo n es c a n l i n ea tat i me( i . e , i n c r e me n t i n g s o u r c e ) .An do fc o u r s e , t h et r a n s f e rh a st oo c c u ra teach s c a n l i n e , s owes e ti tt or e p e a t .s ob a s i c a l l ywen e e dt h i s : #define DMA_HDMA (DMA_ENABLE | DMA_REPEAT | DMA_AT_HBLANK | DMA_DST_RELOAD) Asf o rt h ec i r c l e , wen e e dar o u t i n et h a tc a nc a l c u l a t et h el e f ta n dr i g h te d g e so fac i r c l e .Th e r ea r eac o u p l eo fa l g o r i t h msa r o u n dt h a t c a nd r a wc i r c l e s , f o re x a mp l eBr e s e n h a m' sv e r s i o n .We ' l l u s eamo d i f i e dv e r s i o no fi tb e c a u s eweo n l yn e e dt os t o r et h el e f ta n dr i g h t p o i n t si n s t e a do fd r a wi n gap i x e l t h e r e .Wh yl e f t r i g h ta n dn o tt o p b o t t o m?Be c a u s et h ea r r a yi ss c a n l i n e b a s e d , s ot h a ti n d i c a t e st h eyv a l u e sa l r e a d y . I td o e s n ' tr e a l l yma t t e rwh a ty o uu s ea c t u a l l y , a sl o n ga sy o uc a nf i n dt h ee d g e s .On c ey o uh a v e , a l l y o un e e dt od oi ss e t u pt h e DMAi nt h eVBl a n ka n dy o u ' r ed o n e . Th ee n dr e s u l twi l l s h o ws o me t h i n gl i k ef i g1 4 . 1 .I t ' st h eBr i n s t a rb a c k g r o u n d( a g a i n )i n s i d et h ewi n d o w, a n das t r i p e db go u t s i d e .Th e t e x ti n d i c a t e st h ep o s i t i o na n dr a d i u so ft h ewi n d o w, wh i c hc a nb emo v e dwi t ht h eDp a da n ds c a l e db yAa n dB. #include <stdio.h> #include <tonc.h> #include "brin.h" // From tonc_math.h file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 129/331 28-03-13 Tonc : GBA Programming in rot13 //#define IN_RANGE(x, min, max) ( (x) >= (min) && (x) < (max) ) // The source array u16 g_winh[SCREEN_HEIGHT+1]; //! Create an array of horizontal offsets for a circular window. /*! The offsets are to be copied to REG_WINxH each HBlank, either * by HDMA or HBlank isr. Offsets provided by modified * Bresenham's circle routine (of course); the clipping code is not * optional. * \param winh Pointer to array to receive the offsets. * \param x0 X-coord of circle origin. * \param y0 Y-coord of circle origin. * \param rr Circle radius. */ void win_circle(u16 winh[], int x0, int y0, int rr) { int x=0, y= rr, d= 1-rr; u32 tmp; // clear the whole array first. memset16(winh, 0, SCREEN_HEIGHT+1); while(y >= x) { // Side octs tmp = clamp(x0+y, 0, SCREEN_WIDTH); tmp += clamp(x0-y, 0, SCREEN_WIDTH)<<8; if(IN_RANGE(y0-x, 0, SCREEN_HEIGHT)) winh[y0-x]= tmp; if(IN_RANGE(y0+x, 0, SCREEN_HEIGHT)) winh[y0+x]= tmp; // o4, o7 // o0, o3 // Change in y: top/bottom octs if(d >= 0) { tmp = clamp(x0+x, 0, SCREEN_WIDTH); tmp += clamp(x0-x, 0, SCREEN_WIDTH)<<8; if(IN_RANGE(y0-y, 0, SCREEN_HEIGHT)) winh[y0-y]= tmp; if(IN_RANGE(y0+y, 0, SCREEN_HEIGHT)) winh[y0+y]= tmp; // o5, o6 // o1, o2 d -= 2*(--y); } d += 2*(x++)+3; } winh[SCREEN_HEIGHT]= winh[0]; } void init_main() { // Init BG 2 (basic bg) dma3_cpy(pal_bg_mem, brinPal, brinPalLen); dma3_cpy(tile_mem[0], brinTiles, brinTilesLen); dma3_cpy(se_mem[30], brinMap, brinMapLen); REG_BG2CNT= BG_CBB(0)|BG_SBB(30); // Init BG 1 (mask) const TILE tile= {{ 0xF2F3F2F3, 0x3F2F3F2F, 0xF3F2F3F2, 0x2F3F2F3F, 0xF2F3F2F3, 0x3F2F3F2F, 0xF3F2F3F2, 0x2F3F2F3F }}; tile_mem[0][32]= tile; pal_bg_bank[4][ 2]= RGB15(12,12,12); pal_bg_bank[4][ 3]= RGB15( 8, 8, 8); pal_bg_bank[4][15]= RGB15( 0, 0, 0); se_fill(se_mem[29], 0x4020); file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 130/331 28-03-13 Tonc : GBA Programming in rot13 REG_BG1CNT= BG_CBB(0)|BG_SBB(29); tte_init_chr4_b4_default(0, BG_CBB(2)|BG_SBB(28)); tte_init_con(); tte_set_margins(8, 8, 232, 40); // Init window REG_WIN0H= SCREEN_WIDTH; REG_WIN0V= SCREEN_HEIGHT; // Enable stuff REG_DISPCNT= DCNT_MODE0 | DCNT_BG0 | DCNT_BG1 | DCNT_BG2 | DCNT_WIN0; REG_WININ= WIN_BUILD(WIN_BG0|WIN_BG2, 0); REG_WINOUT= WIN_BUILD(WIN_BG0|WIN_BG1, 0); } int main() { int rr=40, x0=128, y0=120; init_main(); while(1) { vid_vsync(); key_poll(); rr += key_tri_shoulder(); x0 += key_tri_horz(); y0 += key_tri_vert(); // size with B/A // move left/right // move up/down if(rr<0) rr= 0; // Fill circle array win_circle(g_winh, x0, y0, rr); // Init win-circle HDMA DMA_TRANSFER(®_WIN0H, &g_winh[1], 1, 3, DMA_HDMA); tte_printf("#{es;P}(%d,%d) | %d", x0, y0, rr); } return 0; } Th ei n i t i a l i z a t i o nf u n c t i o ni smo s t l yj u s tf l u f f .Mo s t l y , b e c a u s et h e r ei so n et h i n go fi n t e r e s t : t h ec a l l st odma_cpyt oc o p yt h eBr i n s t a r p a l e t t e , t i l e sa n dma p .As i d ef r o mt h a t , n o t h i n gt os e eh e r e . Th ema i nf u n c t i o ni t s e l fi sa l s op r e t t ys t a n d a r d .Ofi n t e r e s th e r ea r et h ec a l l t owin_circle(), wh i c hs e t su pt h es o u r c e a r r a y , a n dt oDMA_TRANSFER()t oi n i t i a l i z et h eHDMA.No t et h a tI ' ma c t u a l l yma k i n gi ts t a r ta tg_winh[1]i n s t e a do fj u s tg_winh[0]. Th er e a s o nf o rt h i si st h a tt h eHBl a n ko c c u r safter ag i v e ns c a n l i n e , n o tb e f o r ei t , s owe ' l l l a go n eo t h e r wi s e .Th eg_winha r r a yi s a c t u a l l y1 6 0 +1l o n g , a n db o t he n t r y0a n d1 6 0d e s c r i b et h ed a t af o rs c a n l i n e0 .Wh a t ' sa l s oi mp o r t a n t , b u tn o te x a c t l yv i s i b l eh e r e , i st h a t HDMAo n l yo c c u r so nt h evisible HBl a n k s , n o tt h eo n e si nt h eVBl a n k .Th i ss a v e su pawh o l el o to ft r o u b l ed e t e r mi n i n gh o wma n y s c a n l i n e st oc o r r e c tf o rwh e ns e t t i n gi tu p . An dt h e nt h e r e ' swin_circle().I fy o u ' r ea wa r eo fh o wt h eBr e s e n h a mc i r c l ea l g o r i t h mwo r k , y o uk n o wi tc a l c u l a t e sa no f f s e t f o ro n eo c t a n ta n dt h e nu s e si tf o rt h e7o t h e r sv i as y mme t r yr u l e s .Th i sh a p p e n sh e r ea swe l l .Wh a td o e s n ' th a p p e ni nt h eo r i g i n a l p r o b a b l yi sa l l t h ec l i p p i n g( t h eclamp()sa n dIN_RANGE()s) .Ho we v e r , t h e s es t e p sa r ea b s o l u t e l yv i t a l h e r e .Go i n go u to fb o u n d s h o r i z o n t a l l ywo u l dme a nwr o n gwi n d o wi n go f f s e t swh i c hwo u l dma k et h ewi n d o wt u r ni no ni t s e l f .Go i n go u to fb o u n d sv e r t i c a l l yme a n s g o i n gOOBo ng_winhf o ra l l k i n do fh o r r i b l e .Tr u s tme , t h e ya r en e c e s s a r y . Al s o , n o t i c et h a tIwi p et h ewh o l ea r r a yc l e a nf i r s t ;t h i sc a nb ed o n ei n s i d et h el o o p , b u ts o me t i me si t ' sj u s tf a s t e rt of i l l t h ewh o l e t h i n gf i r s ta n dt h e no n l yu p d a t et h ep a r t sy o un e e d .La s t l y , a sme n t i o n e db e f o r e , t h ef i r s ts c a n l i n e ' sd a t ai sc o p i e dt ot h ef i n a l e n t r yo ft h e a r r a yt oa c c o u n tf o rt h ewa yHBl a n k sh a p p e n . An dh e r ee n d st h ec h a p t e ro nDMA.Th eu s eo fHDMAi nt h i sma n n e ri sg r e a tf o ra l l k i n d so fe f f e c t s , n o tj u s tc i r c u l a rwi n d o ws .Al l y o un e e di sa na r r a yc o n t a i n i n gs c a n l i n e d a t aa n daf u n c t i o nt h a ts e t si tu pb e f o r e h a n d .Bec a r e f u l y o ud o n ' tg e ty o u rc h a n n e l smi x e du p , t h o u g h . DMAi st h ef a s t e s tme t h o do fc o p y i n g , b u ta sy o ub l o c ki n t e r r u p t su s i n gmemcpy32()i sp r o b a b l ys a f e r .Th es p e e dd i f f e r e n c ei s o n l y1 0 %a n y wa y .DMAi sa l s ou s e df o rs o u n dFI FO, i nc o n j u n c t i o nwi t ht i me r s .Ic a n ' tr e a l l ys h o wy o uh o wt ou s ei tf o rs o u n d , b u tI file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 131/331 28-03-13 Tonc : GBA Programming in rot13 c a nt e l l y o uh o wt i me r swo r k , a n dwi l l d os oi nt h en e x tc h a p t e r . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 132/331 28-03-13 Tonc : GBA Programming in rot13 15. Timers Ti mi n gi se v e r y t h i n g GBATi me r s Ti me rd e mo: l i k ec l o c k wo r k 15.1. Timing is everything Th i n ko fe v e r yt i mey o u ' v eh e a r daj o k er u i n e db e c a u s et h ep u n c hl i n ec a met o ol a t eo rt o oe a r l y ;t h i n ko fa l l t h ef a i l e dj u mp si nSu p e r Ma r i oBr o s( o ra n yo t h e rp l a t f o r mg a me ) ; a l l t h eo c c a s i o n st h a ty o us k i d d e da tt h es t a r to faMa r i oKa r tr a c ef o rr e v v i n gt o os o o n ;t h a t y o u ri n v i n c i b i l i t ywo r eo f fj u s tbefore y o ug o tar e ds h e l l u py o u ra [ c e n s o r e d ] s ;t h a ty o ud i d n ' tq u i t ed o d g et h a th a i l o fb u l l e t si no l d s k o o l s h o o t e r sb e c a u s eo fas u d d e ns l o wd o wn .Th i n ko fa l l t h i sa n ds i t u a t i o n sl i k et h e ma n dy o u ' l l a g r e et h a ti ng a me s , a si nl i f e , Ti mi n gI s Ev e r y t h i n g . I r o n i c a l l y , t i mers a r eo fl e s si mp o r t a n c e .Th r o u g h o u tv i d e o g a meh i s t o r yp r o g r a mme r sh a v eb u i l tt h e i rg a me sa r o u n do n et i mi n g me c h a n i s m: t h ev e r t i c a l r e f r e s hr a t eo ft h es c r e e n .I no t h e rwo r d s , t h eVBl a n k .Th i si sama c h i n e o r i e n t e dt i me r( y o uc o u n tf r a me s ) r a t h e rt h a nah u ma n o r i e n t e do n e( wh e r ey o u ' dc o u n ts e c o n d s ) .Fo rc o n s o l e s , t h i swo r k sv e r ywe l l a st h eh a r d wa r ei sa l wa y st h es a me . ( Ex c e p t , o fc o u r s e , t h a ts o mec o u n t r i e su s eNTSCt e l e v i s i o n s( @6 0Hz )a n do t h e r su s ePALTVs( @5 0Hz ) .Ev e r y o n el i v i n gi nt h e l a t t e rc a t e g o r ya n dh a sa c c e s st ob o t hk i n d sk n o wst h ed i f f e r e n c ea n dc u r s e st h ef a c tt h a ti t ' st h eNTSCc o u n t r i e st h a tmo s tg a me ss t e m f r o m. )Wh i l et h eVBl a n kt i me ri sp e r v a s i v e , i ti sn o tt h eo n l yo n e .Th eGBAh a sf o u rc l o c kt i me r sa ty o u rd i s p o s a l .Th i ss e c t i o nc o v e r s t h e s et i me r s . 15.2. GBA Timers Al l c o n c e i v a b l et i me r swo r ki np r e t t ymu c ht h es a mewa y .Yo uh a v es o me t h i n gt h a to s c i l l a t e swi t hac e r t a i nf i x e df r e q u e n c y( l i k ea CPUc l o c ko rt h es wi n go fap e n d u l u m) .Af t e re v e r yf u l l p e r i o d , ac o u n t e ri si n c r e me n t e da n dy o uh a v ey o u r s e l fat i me r .Ea s y , i n n i t ? 24 Th eb a s i cf r e q u e n c yo ft h eGBAt i me r si st h eCPUf r e q u e n c y , wh i c hi s2 ≈1 6 . 7 8Mh z .I no t h e rwo r d s , o n eclock cycle o ft h e −2 4≈5 CPUt a k e s2 9 . 6n s .Si n c et h i si sav e r yl o u s yt i me s c a l ef o ru sh u ma n s , t h eGBAa l l o wsf o r4d i f f e r e n tf r e q u e n c i e s( o r , r a t h e r p e r i o d s ) : 1 , 6 4 , 2 5 6a n d1 0 2 4c y c l e s .So med e t a i l so ft h e s ef r e q u e n c i e sa r es h o wni nt a b l e 1 5 . 1 .Byc l e v e ru s eo ft h et i me rr e g i s t e r s , y o u c a na c t u a l l yc r e a t et i me r so fa n yf r e q u e n c y , b u tmo r eo nt h a tl a t e r .I ts h o u l db en o t e dt h a tt h es c r e e nr e f r e s h e se v e r y2 8 0 , 8 9 6c y c l e s , e x a c t l y . #cycles frequency period 1 1 6 . 7 8MHz5 9 . 5 9n s 6 4 2 6 2 . 2 1k Hz3 . 8 1 5μs 2 5 6 6 5 . 5 3 6k Hz1 5 . 2 6μs 1 0 2 4 1 6 . 3 8 4k Hz6 1 . 0 4μs Table 15.1: Ti me rf r e q u e n c i e s 15.2.1. Timer registers Th eGBAh a sf o u rt i me r s , t i me r s0t o3 .Ea c ho ft h e s eh a st wor e g i s t e r s : ad a t ar e g i s t e r( REG_TMxD)a n dac o n t r o l r e g i s t e r ( REG_TMxCNT) .Th ea d d r e s s e sc a nb ef o u n di nt a b l e1 5 . 2 . reg function address REG_TMxD d a t a 0400:0100h + 04h· x REG_TMxCNTc o n t r o l 0400:0102h + 04h· x Table 15.2: Ti me rr e g i s t e ra d d r e s s e s 15.2.2. REG_TMxCNT REG_TMxCNT @ 0400:0102 + 4x FEDCBA98 7 6 543 2 10 - En I - CM Fr bits name define 0 1 Fr TM_FREQ_y Ti me rfrequency.0 3f o r1 , 6 4 , 2 5 6 , o r1 0 2 4c y c l e s , r e s p e c t i v e l y .yi nt h ed e f i n ei st h e n u mb e ro fc y c l e s . 2 CM TM_CASCADECascade mode .Wh e nt h ec o u n t e ro ft h epreceding ( )t i me ro v e r f l o ws x−1 ( REG_TM(x-1)D= 0xffff) , t h i so n ewi l l b ei n c r e me n t e dt o o .At i me rt h a th a st h i s b i ts e td o e snot c o u n to ni t so wn , t h o u g hy o us t i l l h a v et oe n a b l ei t .Ob v i o u s l y , t h i s wo n ' two r kf o rt i me r0 .I fy o up l a no nu s i n gi tma k es u r ey o uu n d e r s t a n de x a c t l ywh a t file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm description 133/331 28-03-13 Tonc : GBA Programming in rot13 Ij u s ts a i d ;t h i sp l a c ei sad e a t h t r a pf o rt h eu n wa r y . Ra i s ea ni n t e r r u p to no v e r f l o w. 6 I TM_I RQ 7 En TM_ENABLE En a b l et h et i me r . 15.2.3. REG_TMxD Th ed a t ar e g i s t e rREG_TMxDi sa1 6 b i tn u mb e rt h a two r k sal i t t l eb i td i f f e r e n t l yt h a ny o umi g h te x p e c ta tf i r s t , b u ti nt h ee n di tma k e s s e n s e .Th en u mb e rt h a ty o uread f r o mt h er e g i s t e ri st h ecurrent t i me r c o u n t .Sof a r , s og o o d .Ho we v e r , t h en u mb e rt h a ty o uwrite t o REG_TMxDi st h einitial value t h a tt h ec o u n t e rb e g i n sa twh e nt h et i me ri se i t h e re n a b l e d( v i aTM_ENABLE)o ro v e r f l o ws .Th i sh a s n u mb e ro f‘ i n t e r e s t i n g ’c o n s e q u e n c e s .Toma k et h i n g sal i t t l ee a s i e r , d e f i n ev a r i a b l e sn o ft h ei n i t i a l v a l u e( t h ewr i t e n u mb e r )a n dc f o r t h ec u r r e n tc o u n t( t h er e a dn u mb e r ) . Fi r s to fa l l , wh e ny o us e ta nn ( o f , s a y , c000h)l i k et h i s : REG_TM2D= 0xc000; y o uwi l l not h a v es e tt h ec u r r e n tt i me r c o u n tc t on ( =c000h) .I nf a c t , i ft h et i me ri sd i s a b l e d , t h e nc=0 .Ho we v e r , a ss o o na sy o ud o e n a b l et h ec o u n t e r , t h e nc = n a n dp r o c e e d sf r o mt h e r e .An dwh e nt h et i me ro v e r f l o ws , i twi l l r e s e tt ot h i sv a l u ea swe l l .Byt h ewa y , b e c a u s en i so n l yt h es t a r t i n gv a l u ei ti si mp o r t a n tt os e tn f i r s t , a n de n a b l et h et i me ra f t e r wa r d s . Se c o n d l y , a s ky o u r s e l ft h i s : wh a th a p p e n swh e ny o ud i s a b l et h et i me ra g a i n ?We l l , t h ec o u n t e rr e t a i n si t sc u r r e n tv a l u e .Ho we v e r , wh e ny o uenable i ta f t e r wa r d s , c wi l l r e s e tt on a g a i n .Th i si sab i to fad r a gi fy o uwa n tt od i s a b l et h et i me rf o rawh i l e( d u r i n gag a me p a u s ef o ri n s t a n c e )a n dt h e np i c ku pwh e r ei tl e f to f .We l l , y e a h , b u tt h e r ei sawa yt oma k ei th a p p e n .Ho w?Byt u r n i n gi ti n t oa c a s c a d et i me rv i aTM_CASCADE!Ha v i n gt h a tb i ts e ti nt h eREG_TMxCNTwi l l c a u s et h et i me rt ob ei n c r e a s e do n l ywh e nt h e p r e c e d i n go n eo v e r f l o ws .I fy o up r e v e n tt h a tf r o me v e rh a p p e n i n g( i fi t ' sd i s a b l e df o ri n s t a n c e )t h e ny o uwi l l h a v ee f f e c t i v e l yd i s a b l e d y o u rt i me r . La s t l y , g i v e nac e r t a i nn, t h e nt h et i me rwi l l o v e r f l o wa f t e rT=10000h−n i n c r e me n t s .Or , t h a n k st ot h ewo n d e r so ft wo ' s c o mp l e me n t , j u s tT=−n.Co mb i n e dwi t hac a s c a d et i me r( o ri n t e r r u p t s )y o uc a nb u i l dt i me r so fa n yf r e q u e n c y , wh i c hi swh a ty o uwa n t f r o mat i me r . Writing to REG_TMxD is weird Wr i t i n gi n t oREG_TMx Dma yn o td owh a ty o ut h i n ki td o e s .I td o e snot s e tt h et i me rv a l u e .Ra t h e r , i ts e t st h einitial v a l u e f o rt h en e x tt i me rr u n . 15.3. Timer demo : like clockwork I nt o d a y ' sd e mo , I ' mg o i n gt os h o wh o wt oma k eas i mp l ed i g i t a l c l o c kwi t ht h et i me r s .Tod ot h i s , we ' l l n e e da1Hzt i me r .Ast h a t ' sn o t a v a i l a b l ed i r e c t l y , I ' mg o i n gt os e tu pac a s c a d i n gt i me rs y s t e mwi t ht i me r s2a n d3 .Ti me r3wi l l b es e tt oc a s c a d emo d e , wh i c hi s u p d a t e dwh e nt i me r2o v e r f l o ws .I ti sp o s s i b l et os e tt h eo v e r f l o wt oh a p p e na taf r e q u e n c yo fe x a c t l yo n eHe r t z .Th ec l o c kf r e q u e n c yi s 24 2 , o r1 0 2 4 * 0 x 4 0 0 0 .Bys e t t i n gt i me r2t oTM_FREQ_1024a n dt os t a r ta t−0 x 4 0 0 0 , t h ec a s c a d i n gt i me r3wi l l e f f e c t i v e l yb ea1Hz c o u n t e r . Wh e n e v e rt i me r3i su p d a t e d , t h ed e mot u r n st h en u mb e ro fs e c o n d si n t oh o u r s , mi n u t e sa n d s e c o n d sa n dp r i n t st h a to ns c r e e n( s e ef i g1 5 . 1 ) .Ye s , Ia mu s i n gd i v i s i o n sa n dmo d u l i h e r e b e c a u s ei ti st h es i mp l e s tp r o c e d u r ea n dIc a ns p a r et h ec y c l e si nt h i sp a r t i c u l a rd e mo . Th ed e moc a nb e( u n ) p a u s e dwi t hSe l e c ta n dSt a r t .St a r td i s a b l e st i me r2 , a n dt h u s t i me r3t o o .Se l e c tt u r n st i me r2i n t oac a s c a d et i me ra swe l l , a n ds i n c et i me r1i sd i s a b l e d , d o i n gt h i sa l s os t o p st i me r2( a n d3 ) .Th ed i f f e r e n c ei swh a th a p p e n swh e ny o uu n p a u s e .By d i s a b l i n gat i me r , i twi l l s t a r ta g a i na tt h ei n i t i a l v a l u e ;b u ts t o p p i n gi twi t hac a s c a d ea c t u a l l y k e e p st h et i me ra c t i v ea n di twi l l s i mp l yr e s u mec o u n t i n go n c et h ec a s c a d ei sr e mo v e d .Th e d i f f e r e n c ei sas u b t l eo n e , b u tt h el a t t e ri smo r ea p p r o p r i a t e . // Using a the "Berk" font from headspins font collection. Fig 15.1: tmr_demo. #include <stdio.h> #include <tonc.h> #include "berk.h" void tmr_test() { // Overflow every ~1 second: // 0x4000 ticks @ FREQ_1024 REG_TM2D= -0x4000; REG_TM2CNT= TM_FREQ_1024; // 0x4000 ticks till overflow // we're using the 1024 cycle timer // cascade into tm3 file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 134/331 28-03-13 Tonc : GBA Programming in rot13 REG_TM3CNT= TM_ENABLE | TM_CASCADE; u32 sec= -1; while(1) { vid_vsync(); key_poll(); if(REG_TM3D != sec) { sec= REG_TM3D; tte_printf("#{es;P:24,60}%02d:%02d:%02d", sec/3600, (sec%3600)/60, sec%60); } if(key_hit(KEY_START)) // pause by disabling timer REG_TM2CNT ^= TM_ENABLE; if(key_hit(KEY_SELECT)) // pause by enabling cascade REG_TM2CNT ^= TM_CASCADE; } } int main() { // set-up berk font tte_init_se(0, BG_CBB(0)|BG_SBB(31), 1, 0, 0, &berkFont, se_drawg); tte_init_con(); memcpy16(pal_bg_mem, berkPal, berkPalLen/4); REG_DISPCNT= DCNT_MODE0 | DCNT_BG0; tmr_test(); return 0; } Th i swa sar a t h e rs i mp l eu s eo ft i me r s .Ofc o u r s e , Ic o u l dh a v ej u s ta se a s i l yu s e dt h eVBl a n kt ok e e pt r a c ko ft h es e c o n d s , wh i c hi s h o wi t ' su s u a l l yd o n ea n y wa y .Th eh a r d wa r et i me r sa r eu s u a l l yr e s e r v e df o rt i me dDMA' s , wh i c ha r eu s e di ns o u n dmi x e r s , n o tf o r g a met i me r s .Th e r ei so n eo t h e ru s et h a tc o me st omi n d , t h o u g h , n a me l yp r o f i l i n g : e x a mi n i n gh o wf a s ty o u rf u n c t i o n sa r e .On eo ft h et e x t s y s t e md e mo su s e st h a tt oc h e c kt h es p e e d so faf e wc o p y i n gr o u t i n e s . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 135/331 28-03-13 Tonc : GBA Programming in rot13 16. Interrupts I n t r o d u c t i o n I n t e r r u p t sr e g i s t e r s I n t e r r u p tSe r v i c eRo u t i n e s Cr e a t i n ga ni n t e r r u p ts wi t c h b o a r d Fi n a l l y , a ni n t e r r u p td e mo ! 16.1. Introduction Un d e rc e r t a i nc o n d i t i o n s , y o uc a nma k et h eCPUd r o pwh a t e v e ri t ' sd o i n g , g or u na n o t h e rf u n c t i o ni n s t e a d , a n dc o n t i n u ewi t ht h eo r i g i n a l p r o c e s sa f t e r wa r d s .Th i sp r o c e s si sk n o wna sa ninterrupt ( t wo‘ r ’ s , p l e a s e ) .Th ef u n c t i o nt h a th a n d l e st h ei n t e r r u p ti sa ninterrupt service routine, o rj u s ti n t e r r u p t ;t r i g g e r i n go n ei sc a l l e draising a ni n t e r r u p t . I n t e r r u p t sa r eo f t e na t t a c h e dt oc e r t a i nh a r d wa r ee v e n t s : p r e s s i n gak e yo naPCk e y b o a r d , f o re x a mp l e , r a i s e so n e .An o t h e rPC e x a mp l ei st h eVBl a n k( y e s , PCsh a v et h e mt o o ) .Th eGBAh a ss i mi l a ri n t e r r u p t sa n do t h e r sf o rt h eHBl a n k , DMAa n dmo r e .Th i sl a s t o n ei np a r t i c u l a rc a nb eu s e df o rag r e a td e a l o fn i f t ye f f e c t s .I ' l l g i v eaf u l l l i s to fi n t e r r u p t ss h o r t l y . I n t e r r u p t sh a l tt h ec u r r e n tp r o c e s s , q u i c k l yd o‘ s o me t h i n g ’ , a n dp a s sc o n t r o l b a c ka g a i n .St r e s st h ewo r d“ q u i c k l y ” : i n t e r r u p t sa r e s u p p o s e dt ob es h o r tr o u t i n e s . 16.2. Interrupts registers Th e r ea r et h r e er e g i s t e r ss p e c i f i c a l l yf o ri n t e r r u p t s : REG_IE( 0400:0200h) , REG_IF( 0400:0202h)a n dREG_IME ( 0400:0208h) .REG_IMEi st h ema s t e ri n t e r r u p tc o n t r o l ;u n l e s st h i si ss e tt o‘ 1 ’ , i n t e r r u p t swi l l b ei g n o r e dc o mp l e t e l y .Toe n a b l ea s p e c i f i ci n t e r r u p ty o un e e dt os e tt h ea p p r o p r i a t eb i ti nREG_IE.Wh e na ni n t e r r u p to c c u r s , t h ec o r r e s p o n d i n gb i ti nREG_IFwi l l b es e t . Toa c k n o wl e d g et h a ty o u ' v eh a n d l e da ni n t e r r u p t , t h eb i tn e e d st ob ec l e a r e da g a i n , b u tt h ewa yt od ot h a ti sal i t t l ec o u n t e r i n t u i t i v et o s a yt h el e a s t .Toa c k n o wl e d g et h ei n t e r r u p t , y o ua c t u a l l yh a v et oset t h eb i ta g a i n .Th a t ' sr i g h t , y o uh a v et owr i t e1t ot h a tb i t( wh i c hi s a l r e a d y1 )i no r d e rt oc l e a ri t . Ap a r tf r o ms e t t i n gt h eb i t si nREG_IE, y o ua l s on e e dt os e tab i ti no t h e rr e g i s t e r st h a td e a l wi t ht h es u b j e c t .Fo re x a mp l e , t h e HBl a n ki n t e r r u p ta l s or e q u i r e sab i ti nREG_DISPSTAT.It h i n k( b u tp l e a s ec o r r e c tmei fI ' mwr o n g )t h a ty o un e e db o t has e n d e ra n d r e c e i v e ro fi n t e r r u p t s ;REG_IEc o n t r o l st h er e c e i v e ra n dr e g i s t e r sl i k eREG_DISPSTATc o n t r o l t h es e n d e r .Wi t ht h a ti nmi n d , l e t ' s c h e c ko u tt h eb i tl a y o u tf o rREG_IEa n dREG_IF. REG_IE @ 0400:0200 and REG_IF @ 0400:0202 FE D C BA98 - CK Dma 7 6543 Com Tm 2 1 0 Vct Hbl Vbl bits name define description 0 Vb l I RQ_VBLANK VBlank i n t e r r u p t .Al s or e q u i r e sREG_DISPSTAT{ 3 } 1 Hb l I RQ_HBLANK HBlank i n t e r r u p t .Al s or e q u i r e sREG_DISPSTAT{ 4 }Oc c u r safter t h eHDr a w, s o t h a tt h i n g sd o n eh e r et a k ee f f e c ti nt h en e x tl i n e . 2 Vc t I RQ_VCOUNT VCount i n t e r r u p t .Al s or e q u i r e sREG_DISPSTAT{ 5 } .Th eh i g hb y t eo f REG_DISPSTATg i v e st h eVCo u n ta twh i c ht or a i s et h ei n t e r r u p t .Oc c u r sa tt h e beginning o fas c a n l i n e . 3 6 Tm I RQ_TI MERx n t e r r u p t , 1b i tp e rt i me r .Al s or e q u i r e sREG_TMxCNT{ 6 } .Th ei n t e r r u p twi l l Timer i b er a i s e dwh e nt h et i me ro v e r f l o ws . 7 Co m I RQ_COM n t e r r u p t .Ap p a r e n t l y , a l s or e q u i r e sREG_SCCNT{ E} .Tob e Serial communication i r a i s e dwh e nt h et r a n s f e ri sc o mp l e t e .Ors oI ' mt o l d , Ir e a l l yd o n ' tk n o ws q u a ta b o u t s e r i a l c o mmu n i c a t i o n . 8 B Dma I RQ_DMAx n t e r r u p t , 1b i tp e rc h a n n e l .Al s or e q u i r e sREG_DMAxCNT{ 1 E} .I n t e r r u p twi l l DMA i b er a i s e dwh e nt h ef u l l t r a n s f e ri sc o mp l e t e . C K I RQ_KEYPAD Keypad i n t e r r u p t .Al s or e q u i r e sREG_KEYCNT{ E} .Ra i s e dwh e na n yo ra l l o rt h e k e y ss p e c i f i e di nREG_KEYCNTa r ed o wn . D C I RQ_GAMEPAKCartridge i n t e r r u p t .Ra i s e dwh e nt h ec a r ti sr e mo v e df r o mt h eGBA. 16.3. Interrupt Service Routines Yo uu s et h ei n t e r r u p tr e g i s t e r sd e s c r i b e da b o v et oi n d i c a t ewh i c hi n t e r r u p t sy o uwa n tt ou s e .Th en e x ts t e pi swr i t i n ga ni n t e r r u p ts e r v i c e r o u t i n e .Th i si sj u s tat y p e l e s sf u n c t i o n( void func(void)) ;aCf u n c t i o nl i k ema n yo t h e r s .He r e ' sa ne x a mp l eo fa nHBl a n k file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 136/331 28-03-13 Tonc : GBA Programming in rot13 i n t e r r u p t . void hbl_pal_invert() { pal_bg_mem[0] ^= 0x7FFF; REG_IF = IRQ_HBLANK; } Th ef i r s tl i n ei n v e r t st h ec o l o ro ft h ef i r s te n t r yo ft h ep a l e t t eme mo r y .Th es e c o n dl i n er e s e t st h eHBl a n kb i to fREG_IFi n d i c a t i n gt h e i n t e r r u p th a sb e e nd e a l twi t h .Si n c et h i si sa nHBl a n ki n t e r r u p t , t h ee n d r e s u l ti st h a tt h a tt h ec o l o rc h a n g e se v e r ys c a n l i n e .Th i ss h o u l d n ' t b et o oh a r dt oi ma g i n e . I fy o us i mp l ya d dt h i sf u n c t i o nt oa ne x i s t i n gp r o g r a m, n o t h i n gwo u l dc h a n g e .Ho wc o me ?We l l , t h o u g hy o uh a v ea ni s rn o w, y o us t i l l n e e dt ot e l l t h eGBAwh e r et of i n di t .Fo rt h a t , wewi l l n e e dt ot a k eac l o s e rl o o ka tt h ei n t e r r u p tp r o c e s sa sawh o l e . On acknowledging interrupts correctly Toa c k n o wl e d g et h a ta ni n t e r r u p th a sb e e nd e a l twi t h , y o uh a v et oset t h eb i to ft h a ti n t e r r u p ti nREG_IF, a n donly t h a tb i t . Th a tme a n st h a t‘ REG_IF = IRQ_x’i su s u a l l yt h ec o r r e c tc o u r s eo fa c t i o n , a n dn o t‘ REG_IF |= IRQ_x’ .Th e| = v e r s i o na c k n o wl e d g e sa l l i n t e r r u p t st h a th a v eb e e nr a i s e d , e v e ni fy o uh a v e n ' td e a l twi t ht h e my e t . Us u a l l y , t h e s et wor e s u l ti nt h es a met h i n g , b u ti fmu l t i p l ei n t e r r u p t sc o mei na tt h es a met i met h i n g swi l l g ob a d .J u s tp a y a t t e n t i o nt owh a ty o u ' r ed o i n g . 16.3.1. The interrupt process Th ec o mp l e t ei n t e r r u p tp r o c e s si sk i n do ft r i c k ya n dp a r to fi ti sc o mp l e t e l yb e y o n dy o u rc o n t r o l .Wh a tf o l l o wsn o wi sal i s to ft h i n g st h a t y o u , t h ep r o g r a mme r , n e e dt ok n o w.Fo rt h ef u l l s t o r y , s e eGBATe k: i r qc o n t r o l . 1 .I n t e r r u p to c c u r s .So meb l a c kma g i cd e e pwi t h i nt h ed e e p e s td u n g e o n so fBI OSh a p p e n sa n dt h eCPUi ss wi t c h e dt oI RQ mo d ea n dARM s t a t e .An u mb e ro fr e g i s t e r s( r0-r3, r12, lr)a r ep u s h e do n t ot h es t a c k . 2 .BI OSl o a d st h ea d d r e s sl o c a t e da t0300:7FFCa n db r a n c h e st ot h a ta d d r e s s . 3 .Th ec o d ep o i n t e dt ob y0300:7FFCi sr u n .Si n c ewe ' r ei nARMs t a t en o w, t h i smust t ob eARM c o d e ! 4 .Af t e rt h ei s ri sd o n e , a c k n o wl e d g et h a tt h ei n t e r r u p th a sb e e nd e a l twi t hb ywr i t i n gt oREG_IF, t h e nr e t u r nf r o mt h ei s rb y i s s u i n gabx lri n s t r u c t i o n . 5 .Th ep r e v i o u s l ys a v e dr e g i s t e r sa r ep o p p e df r o ms t a c ka n dp r o g r a ms t a t ei sr e s t o r e dt on o r ma l . St e p s1 , 2a n d5a r ed o n eb yBI OS;3a n d4a r ey o u r s .No w, i np r i n c i p l ea l l y o un e e dt od oi sp l a c et h ea d d r e s so fy o u ri s ri n t oa d d r e s s 0300:7FFC.Toma k eo u rj o bal i t t l ee a s i e r , wewi l l f i r s tc r e a t eo u r s e l v e saf u n c t i o np o i n t e rt y p e . typedef void (*fnptr)(void); #define REG_ISR_MAIN *(fnptr*)(0x03007FFC) // Be careful when using it like this, see notes below void foo() { REG_ISR_MAIN= hbl_pal_invert; // tell the GBA where my isr is REG_DISPSTAT |= VID_HBL_IRQ; // Tell the display to fire HBlank interrupts REG_IE |= IRQ_HBLANK; // Tell the GBA to catch HBlank interrupts REG_IME= 1; // Tell the GBA to enable interrupts; } No w, t h i swi l l p r o b a b l ywo r k , b u ta su s u a l t h e r e ' smo r et ot h es t o r y . Fi r s t , t h ec o d et h a tREG_ISR_MAINj u mp st omust b eARM c o d e !I fy o uc o mp i l ewi t ht h e-mthumbf l a g , t h ewh o l et h i n g c o me st oas c r e e c h i n gh a l t . Wh a th a p p e n swh e ny o u ' r ei n t e r r u p t e di n s i d ea ni n t e r r u p t ?We l l , t h a t ' sn o tq u i t ep o s s i b l ea c t u a l l y ;n o tu n l e s sy o ud os o me f a n c ys t u f fwe ' l l g e tt ol a t e r .Yo us e e , REG_IMEi sn o tt h eo n l yt h i n gt h a ta l l o wsi n t e r r u p t s , t h e r e ' sab i tf o ri r q si nt h e program status register ( PSR)a swe l l .Wh e na ni n t e r r u p ti sr a i s e d , t h eCPUd i s a b l e si n t e r r u p t st h e r eu n t i l t h ewh o l et h i n gi s o v e ra n dd o n ewi t h . hbl_pal_invert()d o e s n ' tc h e c kwh e t h e ri th a sb e e na c t i v a t e db ya nHBl a n ki n t e r r u p t .No w, i nt h i sc a s ei td o e s n ' t r e a l l yma t t e rb e c a u s ei t ' st h eo n l yo n ee n a b l e d , b u twh e ny o uu s ed i f f e r e n tt y p e so fi n t e r r u p t s , s o r t i n gt h e mo u ti se s s e n t i a l . Th a t ' swh ywe ' l l c r e a t ea ni n t e r r u p ts wi t c h b o a r di nt h en e x ts e c t i o n . La s t l y , wh e ny o uu s eBI OSc a l l st h a tr e q u i r ei n t e r r u p t s , y o ua l s on e e dt oa c k n o wl e d g et h e mi nREG_IFBIOS( == 0300:7FF8) .Th eu s ei st h es a mea sREG_IF. file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 137/331 28-03-13 Tonc : GBA Programming in rot13 On section mirroring GBA' sme mo r ys e c t i o n sa r emi r r o r e de v e rs oma n yb y t e s .Fo re x a mp l eI WRAM ( 0300:0000)i smi r r o r e de v e r y8 0 0 0 h b y t e s , s ot h a t0300:7FFCi sa l s o03FF:FFFC, o r0400:0000−4 .Wh i l et h i si sf a s t e r , I ' mn o tq u i t es u r ei ft h i ss h o u l db e t a k e na d v a n t a g eo f .n o $ g b av 2 . 2 bma r k si ta sa ne r r o r , e v e nt h o u g ht h i swa sa p p a r e n t l yas ma l l o v e r s i g h ta n df i x e di nv 2 . 2 c . Ne v e r t h e l e s s , c o n s i d e ry o u r s e l fwa r n e d . 16.4. Creating an interrupt switchboard Th ehbl_pal_invert()f u n c t i o ni sa ne x a mp l eo fas i n g l ei n t e r r u p t , b u ty o uma yh a v et od e a l wi t hmu l t i p l ei n t e r r u p t s .Yo uma y a l s owa n tt ob ea b l et ou s ed i f f e r e n ti s r ' sd e p e n d i n go nc i r c u ms t a n c e s , i nwh i c hc a s es t u f f i n gi ta l l i n t oo n ef u n c t i o nma yn o tb et h eb e s t wa yt og o .I n s t e a d , we ' l l c r e a t ea ni n t e r r u p ts wi t c h b o a r d . Aninterrupt switchboard wo r k sal i t t l el i k eat e l e p h o n es wi t c h b o a r d : y o uh a v eac a l l ( i . e . , a ni n t e r r u p t , i nREG_IF)c o mi n gi n , t h e o p e r a t o rc h e c k si fi ti sa na c t i v en u mb e r( c o mp a r e si twi t hREG_IE)a n di fs o , c o n n e c t st h ec a l l t ot h er i g h tr e c e i v e r( y o u ri s r ) . Th i sp a r t i c u l a rs wi t c h b o a r dwi l l c o mewi t han u mb e ro fa d d i t i o n a l f e a t u r e sa swe l l .I twi l l a c k n o wl e d g et h ec a l l i nb o t hREG_IFa n d REG_IFBIOS) , e v e nwh e nt h e r e ' sn oa c t u a l I SRa t t a c h e dt ot h a ti n t e r r u p t .I twi l l a l s oa l l o wn e s t e di n t e r r u p t s , a l t h o u g ht h i sr e q u i r e sa l i t t l ee x t r awo r ki nt h eI SRi t s e l f . 16.4.1. Design and interface considerations Th ea c t u a l s wi t c h b o a r di so n l yo n ep a r to ft h ewh o l e ;Ia l s on e e dac o u p l eo fs t r u c t s , v a r i a b l e sa n df u n c t i o n s .Th eb a s i ci t e msIr e q u i r e a r et h e s e . __isr_table[].Ani n t e r r u p tt a b l e .Th i si sat a b l eo ff u n c t i o np o i n t e r st ot h ed i f f e r e n ti s r ' s .Be c a u s et h ei n t e r r u p t ss h o u l d b ep r i o r i t i z e d , t h et a b l es h o u l da l s oi n d i c a t ewh i c hi n t e r r u p tt h ep o i n t e r sb e l o n gt o .Fo rt h i s , we ' l l u s ea nIRQ_RECs t r u c t . irq_init()/irq_set_master().Se tma s t e ri s r .irq_init()i n i t i a l i z e st h ei n t e r r u p tt a b l ea n di n t e r r u p t s t h e ms e l v e sa swe l l . irq_enable()/irq_disable().Fu n c t i o n st oe n a b l ea n dd i s a b l ei n t e r r u p t s .Th e s ewi l l t a k ec a r eo fb o t hREG_IEa n d wh a t e v e rr e g i s t e rt h es e n d e rb i ti so n .I ' mk e e p i n gt h e s eb i t si na ni n t e r n a l t a b l ec a l l e d__irq_senders[]a n dt ob ea b l et o u s et h e s e , t h ei n p u tp a r a me t e ro ft h e s ef u n c t i o n sn e e dt ob et h eindex o ft h ei n t e r r u p t , n o tt h ei n t e r r u p tf l a gi t s e l f .Wh i c hi s wh yIh a v eII_fooc o u n t e r p a r t sf o rt h eIRQ_foof l a g s . irq_set()/irq_add()/irq_delete().Fu n c t i o nt oa d d / d e l e t ei n t e r r u p ts e r v i c er o u t i n e s .Th ef i r s ta l l o wsf u l l p r i o r i t i z a t i o no fi s r ' s ;irq_add()wi l l r e p l a c et h ec u r r e n ti r sf o rag i v e ni n t e r r u p t , o ra d do n ea tt h ee n do ft h el i s t ; irq_delete()wi l l d e l e t eo n ea n dc o r r e c tt h el i s tf o rt h ee mp t ys p a c e . Al l o ft h e s ef u n c t i o n sd os o me t h i n gl i k et h i s : d i s a b l ei n t e r r u p t s( REG_IME=0 ) , d ot h e i rs t u f fa n dt h e nr e e n a b l ei n t e r r u p t s .I t ' sag o o d i d e at od ot h i sb e c a u s eb e i n gi n t e r r u p t e dwh i l emu c k i n ga b o u twi t hi n t e r r u p t si sn o tp r e t t y .Th ef u n c t i o n sc o n c e r n e dwi t hs e r v i c er o u t i n e s wi l l a l s ot a k eaf u n c t i o np o i n t e r( t h efnptrt y p e ) , a n da l s or e t u r naf u n c t i o np o i n t e ri n d i c a t i n gt h ep r e v i o u si s r .Th i sma yb eu s e f u l i fy o u wa n tt ot r yt oc h a i nt h e m. Be l o wy o uc a ns e et h es t r u c t s , t a b l e s , a n dt h ei mp l e me n t a t i o no firq_enable()a n dirq_add().I nb o t hf u n c t i o n s , t h e __irq_senders[]a r r a yi su s e dt od e t e r mi n ewh i c hb i tt os e ti nwh i c hr e g i s t e rt oma k es u r et h i n g ss e n di n t e r r u p tr e q u e s t s .Th e irq_add()f u n c t i o ng o e so nt of i n d i n ge i t h e rt h er e q u e s t e di n t e r r u p ti nt h ec u r r e n tt a b l et or e p l a c e , o ra ne mp t ys l o tt of i l l .Th eo t h e r r o u t i n e sa r es i mi l a r .I fy o un e e dt os e emo r e , l o o ki ntonc_irq.h/.ci nt o n c l i b . //! Interrups Indices typedef enum eIrqIndex { II_VBLANK=0, II_HBLANK, II_VCOUNT, II_TIMER0, II_TIMER1, II_TIMER2, II_TIMER3, II_SERIAL, II_DMA0, II_DMA1, II_DMA2, II_DMA3, II_KEYPAD, II_GAMEPAK,II_MAX } eIrqIndex; //! Struct for prioritized irq table typedef struct IRQ_REC { u32 flag; //!< Flag for interrupt in REG_IF, etc fnptr isr; //!< Pointer to interrupt routine } IRQ_REC; // === PROTOTYPES ===================================================== IWRAM_CODE void isr_master_nest(); void irq_init(fnptr isr); fnptr irq_set_master(fnptr isr); fnptr irq_add(enum eIrqIndex irq_id, fnptr isr); fnptr irq_delete(enum eIrqIndex irq_id); file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 138/331 28-03-13 Tonc : GBA Programming in rot13 fnptr irq_set(enum eIrqIndex irq_id, fnptr isr, int prio); void irq_enable(enum eIrqIndex irq_id); void irq_disable(enum eIrqIndex irq_id); // IRQ Sender information typedef struct IRQ_SENDER { u16 reg_ofs; //!< sender reg - REG_BASE u16 flag; //!< irq-bit in sender reg } ALIGN4 IRQ_SENDER; // === GLOBALS ======================================================== // One extra entry for guaranteed zero IRQ_REC __isr_table[II_MAX+1]; static const IRQ_SENDER __irq_senders[] = { { 0x0004, 0x0008 }, // REG_DISPSTAT, { 0x0004, 0x0010 }, // REG_DISPSTAT, { 0x0004, 0x0020 }, // REG_DISPSTAT, { 0x0102, 0x0040 }, // REG_TM0CNT, { 0x0106, 0x0040 }, // REG_TM1CNT, { 0x010A, 0x0040 }, // REG_TM2CNT, { 0x010E, 0x0040 }, // REG_TM3CNT, { 0x0128, 0x4000 }, // REG_SCCNT_L { 0x00BA, 0x4000 }, // REG_DMA0CNT_H, { 0x00C6, 0x4000 }, // REG_DMA1CNT_H, { 0x00D2, 0x4000 }, // REG_DMA2CNT_H, { 0x00DE, 0x4000 }, // REG_DMA3CNT_H, { 0x0132, 0x4000 }, // REG_KEYCNT, { 0x0000, 0x0000 }, // cart: none }; DSTAT_VBL_IRQ DSTAT_VHB_IRQ DSTAT_VCT_IRQ TM_IRQ TM_IRQ TM_IRQ TM_IRQ BIT(14) // not sure DMA_IRQ>>16 DMA_IRQ>>16 DMA_IRQ>>16 DMA_IRQ>>16 KCNT_IRQ // === FUNCTIONS ====================================================== //! Enable irq bits in REG_IE and sender bits elsewhere void irq_enable(enum eIrqIndex irq_id) { u16 ime= REG_IME; REG_IME= 0; const IRQ_SENDER *sender= &__irq_senders[irq_id]; *(u16*)(REG_BASE+sender->reg_ofs) |= sender->flag; REG_IE |= BIT(irq_id); REG_IME= ime; } //! Add a specific isr fnptr irq_add(enum eIrqIndex irq_id, fnptr isr) { u16 ime= REG_IME; REG_IME= 0; int ii; u16 irq_flag= BIT(irq_id); fnptr old_isr; IRQ_REC *pir= __isr_table; // Enable irq const IRQ_SENDER *sender= &__irq_senders[irq_id]; *(u16*)(REG_BASE+sender->reg_ofs) |= sender->flag; REG_IE |= irq_flag; // Search for previous occurance, or empty slot for(ii=0; pir[ii].flag; ii++) if(pir[ii].flag == irq_flag) break; file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 139/331 28-03-13 Tonc : GBA Programming in rot13 old_isr= pir[ii].isr; pir[ii].isr= isr; pir[ii].flag= irq_flag; REG_IME= ime; return old_isr; } 16.4.2. The master interrupt service routine Th ema i nt a s ko ft h ema s t e rI SRi st os e e ko u tt h er a i s e di n t e r r u p ti n___isr_table, a n da c k n o wl e d g ei ti nb o t hREG_IFa n d REG_IFBIOS.I ft h e r ei sa ni r q s p e c i f i cs e r v i c er o u t i n e , i ts h o u l dc a l l i t ;o t h e r wi s e , i ts h o u l dj u s te x i tt oBI OSa g a i n .I nC, i two u l dl o o k s o me t h i n gl i k et h i s . // This is mostly what tonclib's isr_master does, but // you really need asm for the full functionality IWRAM_CODE void isr_master_c() { u32 ie= REG_IE; u32 ieif= ie & REG_IF; IRQ_REC *pir; // (1) Acknowledge IRQ for hardware and BIOS. REG_IF = ieif; REG_IFBIOS |= ieif; // (2) Find raised irq for(pir= __isr_table; pir->flag!=0; pir++) if(pir->flag & ieif) break; // (3) Just return if irq not found in list or has no isr. if(pir->flag == 0 || pir->isr == NULL) return; // --- If we're here have an interrupt routine --// (4a) Disable IME and clear the current IRQ in IE u32 ime= REG_IME; REG_IME= 0; REG_IE &= ~ieif; // (5a) CPU back to system mode //> *(--sp_irq)= lr_irq; //> *(--sp_irq)= spsr //> cpsr &= ~(CPU_MODE_MASK | CPU_IRQ_OFF); //> cpsr |= CPU_MODE_SYS; //> *(--sp_sys) = lr_sys; pir->isr(); // (6) Run the ISR REG_IME= 0; // Clear IME again (safety) // (5b) Back to irq mode //> lr_sys = *sp_sys++; //> cpsr &= ~(CPU_MODE_MASK | CPU_IRQ_OFF); //> cpsr |= CPU_MODE_IRQ | CPU_IRQ_OFF; //> spsr = *sp_irq++ //> lr_irq = *sp_irq++; // (4b) Restore original ie and ime REG_IE= ie; REG_IME= ime; } Mo s to ft h e s ep o i n t sh a v eb e e nd i s c u s s e da l r e a d y , s oIwo n ' tr e p e a tt h e ma g a i n .Don o t et h ed i f f e r e n c ei sa c k n o wl e d g i n gREG_IFa n d REG_IFBIOS: t h ef o r me ru s e sas i mp l ea s s i g n me n ta n dt h el a t t e ra n| =.St e p s4 , 5a n d6o n l ye x e c u t ei ft h ec u r r e n tI RQh a si t so wn s e r v i c er o u t i n e .St e p s4 aa n d5 awo r ka si n i t i a l i z a t i o ns t e p st oe n s u r et h a tt h eI SR( s t e p6 )c a nwo r ki nCPUmo d ea n dt h a ti tc a n ' tb e i n t e r r u p t e du n l e s si ta s k sf o ri t .St e p s4 ba n d5 bu n wi n d4 aa n d5 a . Th i sr o u t i n ewo u l dwo r kf i n ei nC, we r ei tn o tf o ri t e ms5 aa n d5 b .Th e s ea r et h ec o d et os e t / r e s t o r et h eCPUmo d et os y s t e m/ i r q mo d e , b u tt h ei n s t r u c t i o n sn e c e s a s r yf o rt h a ta r e n ' ta v a i l a b l ei nC.An o t h e rp r o b l e mi st h a tt h el i n kr e g i s t e r s( t h e s ea r eu s e dt oh o l dt h e r e t u r na d d r e s s e so ff u n c t i o n s )h a v et ob es a v e ds o me h o w, a n dt h e s edefinitely a r e n ' ta v a i l a b l ei nC. file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 140/331 28-03-13 Tonc : GBA Programming in rot13 No t e : Is a i dr e g i s t e r s, p l u r a l !Ea c hCPUmo d eh a si t so wns t a c ka n dl i n kr e g i s t e r , a n de v e nt h o u g ht h en a me sa r et h es a me( lra n d sp) , t h e yr e a l l ya r e n ' ti d e n t i c a l .Us u a l l yaCr o u t i n ewi l l s a v elro ni t so wn , b u ts i n c ey o un e e di tt wi c en o wi t ' sv e r yu n s a f et ol e a v e t h i su pt ot h ec o mp i l e r .As i d ef r o mt h a t , y o un e e dt os a v et h es a v e dp r o g r a ms t a t u sr e g i s t e rspsr, wh i c hi n d i c a t e st h ep r o g r a ms t a t u s wh e nt h ei n t e r r u p to c c u r r e d .Th i si sa n o t h e rt h i n gt h a tCc a n ' tr e a l l yd o .Ass u c h , a s s e mb l yi sr e q u i r e df o rt h ema s t e rI SR. So , a s s e mb l yi ti st h e n .Th ef u n c t i o nb e l o wi st h ea s s e mb l ye q u i v a l e n to firs_master_c().I ti sa l mo s tal i n eb yl i n et r a n s l a t i o n , a l t h o u g hIa mma k i n gu s eo faf e wf e a t u r e so ft h ei n s t r u c t i o ns e tt h ec o mp i l e rwo n t ' to rc a n ' t .Id o n ' te x p e c ty o ut or e a l l yu n d e r s t a n d e v e r y t h i n gwr i t t e nh e r e , b u twi t hs o mei ma g i n a t i o ny o us h o u l db ea b l et of o l l o wmo s to fi t .Te a c h i n ga s s e mb l yi sway b e y o n dt h es c o p e o ft h i sc h a p t e r , b u two r t ht h ee f f o r ti nmyv i e w.To n c ' sa s s e mb l yc h a p t e rs h o u l dg i v ey o ut h en e c e s s a r yi n f o r ma t i o nt ou n d e r s t a n dmo s t o fi ta n ds h o wswh e r et og ot ol e a r nmo r e . .file "tonc_isr_master.s" .extern __isr_table; /*! \fn IWRAM_CODE void isr_master() \brief Default irq dispatcher (no automatic nesting) */ .section .iwram, "ax", %progbits .arm .align .global isr_master @ --- Register list --@ r0 : ®_IE @ r1 : __isr_table / isr @ r2 : IF & IE @ r3 : tmp @ ip : (IF<<16 | IE) isr_master: @ Read IF/IE mov r0, #0x04000000 ldr ip, [r0, #0x200]! and r2, ip, ip, lsr #16 @ irq= IE & IF @ (1) Acknowledge irq in IF and for BIOS strh r2, [r0, #2] ldr r3, [r0, #-0x208] orr r3, r3, r2 str r3, [r0, #-0x208] @ (2) Search for irq. ldr r1, =__isr_table .Lirq_search: ldr r3, [r1], #8 tst r3, r2 bne .Lpost_search cmp r3, #0 bne .Lirq_search @ Found one, break off search @ Not here; try next irq @ (3) Search over : return if no isr, otherwise continue. .Lpost_search: ldrne r1, [r1, #-4] @ isr= __isr_table[ii-1].isr cmpne r1, #0 bxeq lr @ If no isr: quit @ --- If we're here, we have an isr --@ (4a) Disable IME and clear the current IRQ in IE ldr r3, [r0, #8] @ Read IME strb r0, [r0, #8] @ Clear IME bic r2, ip, r2 strh r2, [r0] @ Clear current irq in IE mrs stmfd r2, spsr sp!, {r2-r3, ip, lr} @ sprs, IME, (IE,IF), lr_irq @ (5a) Set mode to sys mrs r3, cpsr bic r3, r3, #0xDF orr r3, r3, #0x1F file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 141/331 28-03-13 Tonc : GBA Programming in rot13 msr cpsr, r3 @ (6) Call isr stmfd sp!, {r0,lr} mov lr, pc bx r1 ldmfd sp!, {r0,lr} @ --- Unwind --strb r0, [r0, #8] @ (5b) Reset mode to irq mrs r3, cpsr bic r3, r3, #0xDF orr r3, r3, #0x92 msr cpsr, r3 @ ®_IE, lr_sys @ ®_IE, lr_sys @ Clear IME again (safety) @ (4b) Restore original spsr, IME, IE, lr_irq ldmfd sp!, {r2-r3, ip, lr} @ sprs, IME, (IE,IF), lr_irq msr spsr, r2 strh ip, [r0] str r3, [r0, #8] bx lr Nested irqs are nasty Ma k i n gan e s t e di n t e r r u p tr o u t i n ewo r ki sn o tap l e a s a n te x e r c i s ewh e ny o uo n l yp a r t i a l l yk n o wwh a ty o u ' r ed o i n g .Fo r e x a mp l e , t h a td i f f e r e n tCPUmo d e su s e dd i f f e r e n ts t a c k st o o kmeawh i l et of i g u r eo u t , a n di tt o o kmeq u i t eawh i l et or e a l i z e t h a tt h er e a s o nmyn e s t e di s r sd i d n ' two r kwa sb e c a u s et h e r ea r ed i f f e r e n tl i n kr e g i s t e r st o o . Th eisr_master_nesti sl a r g e l yb a s e do nl i b g b a ' si n t e r r u p td i s p a t c h e r , b u ta l s ob o r r o wsi n f o r ma t i o nf r o mGBATe k a n dA.Bi l y ka n dDe k u Tr e e ' sa n a l y s i so ft h ewh o l et h i n ga sd e s c r i b e di nf o r u m: 4 0 6 3 .Al s oi n v a l u a b l ewa st h eh o me u s e d e b u g g e rv e r s i o no fn o $ g b a , h u r r a yf o rb r e a k p o i n t s . I fy o uwa n tt od e v e l o py o u ro wni n t e r r u p tr o u t i n e , t h e s es o u r c e swi l l h e l py o ui mme n s e l ya n dwi l l k e e pt h el o s so fs a n i t y d o wnt os o me wh a ta c c e p t a b l el e v e l s . Deprecation notice Iu s e dt oh a v ead i f f e r e n tma s t e rs e r v i c er o u t i n et h a tt o o kc a r eo fn e s t i n ga n dp r i o r i t i z i n gi n t e r r u p t sa u t o ma t i c a l l y .Be c a u s ei t wa sd e e me dt o oc o mp l i c a t e d , i th a sb e e nr e p l a c e dwi t ht h i so n e . Ne s t e di n t e r r u p t sa r es t i l l p o s s i b l e , b u ty o uh a v et oi n d i c a t ei n t e r r u p t a b i l i t yi n s i d et h ei s ry o u r s e l fn o w. 16.5. Nested interrupt demo To d a y ' sd e mos h o wsal i t t l eb i to fe v e r y t h i n gd e s c r i b e da b o v e : I t ' l l d i s p l a yac o l o rg r a d i e n to nt h es c r e e nt h r o u g ht h eu s eo fa nHBl a n ki n t e r r u p t . I twi l l a l l o wy o ut ot o g g l eb e t we e nt wod i f f e r e n tma s t e ri s r s : Th es wi t c h b o a r disr_masterwh i c hr o u t e st h ep r o g r a mf l o w t oa nHBl a n ki s r , a n da ni s ri nCt h a th a n d l e st h eHBl a n ki n t e r r u p td i r e c t l y .Fo rt h el a t t e rt owo r k , we ' l l n e e dt ou s eARMc o mp i l e dc o d e , o fc o u r s e , a n dI ' l l a l s os h o wy o uh o wi nami n u t e . Fi n a l l y , h a v i n gan e s t e di s rs wi t c h b o a r dd o e s n ' tme a nmu c hu n l e s sy o uc a na c t u a l l ys e en e s t e di n t e r r u p t si na c t i o n .I nt h i sc a s e , we ' l l u s et woi n t e r r u p t s : VCo u n ta n dHBl a n k .Th eHBl a n ki s rc r e a t e sav e r t i c a l c o l o rg r a d i e n t .Th eVCo u n ti s rwi l l r e s e tt h e c o l o ra n dt i eu pt h eCPUf o rs e v e r a l s c a n l i n e s .I fi n t e r r u p t sd o n ' tn e s t , y o u ' l l s e et h eg r a d i e n ts t o pf o rawh i l e ;i ft h e yd on e s t , i t ' l l c o n t i n u ea sn o r ma l . An dj u s tf o rt h eh e l l o fi t , y o uc a nt o g g l et h eHBl a n ka n dVCo u n ti r q so na n do f f . Th ec o n t r o l sa r ea sf o l l o ws : g g l e sb e t we e na s ms wi t c h b o a r da n dCd i r e c ti s r . A To g g l e sHBl a n ka n dVCo u n tp r i o r i t i e s . B To g g l e sVCo u n ta n dHBl a n ki r q so na n do f f . L,R To #include <stdio.h> #include <tonc.h> IWRAM_CODE void isr_master(); IWRAM_CODE void hbl_grad_direct(); void vct_wait(); void vct_wait_nest(); file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 142/331 28-03-13 Tonc : GBA Programming in rot13 CSTR strings[]= { "asm/nested", "HBlank", }; "c/direct", "VCount" // Function pointers to master isrs. const fnptr master_isrs[2]= { (fnptr)isr_master, (fnptr)hbl_grad_direct }; // VCount interrupt routines. const fnptr vct_isrs[2]= { vct_wait, vct_wait_nest }; // (1) Uses tonc_isr_master.s' isr_master() as a switchboard void hbl_grad_routed() { u32 clr= REG_VCOUNT/8; pal_bg_mem[0]= RGB15(clr, 0, 31-clr); } // (2a) VCT is triggered at line 80; this waits 40 scanlines void vct_wait() { pal_bg_mem[0]= CLR_RED; while(REG_VCOUNT<120); } // (2b) As vct_wait(), but interruptable by HBlank void vct_wait_nest() { pal_bg_mem[0]= CLR_RED; REG_IE= IRQ_HBLANK; // Allow nested hblanks REG_IME= 1; while(REG_VCOUNT<120); } int main() { u32 bDirect=0, bVctPrio= 0; tte_init_chr4_b4_default(0, BG_CBB(2)|BG_SBB(28)); tte_set_drawg((fnDrawg)chr4_drawg_b4cts_fast); tte_init_con(); tte_set_margins(8, 8, 128, 64); REG_DISPCNT= DCNT_MODE0 | DCNT_BG0; // (3) Initialize irqs; add HBL and VCT isrs // and set VCT to trigger at 80 irq_init(master_isrs[0]); irq_add(II_HBLANK, hbl_grad_routed); BFN_SET(REG_DISPSTAT, 80, DSTAT_VCT); irq_add(II_VCOUNT, vct_wait); irq_add(II_VBLANK, NULL); while(1) { //vid_vsync(); VBlankIntrWait(); key_poll(); // Toggle HBlank irq file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 143/331 28-03-13 Tonc : GBA Programming in rot13 if(key_hit(KEY_R)) REG_IE ^= IRQ_HBLANK; // Toggle Vcount irq if(key_hit(KEY_L)) REG_IE ^= IRQ_VCOUNT; // (4) Toggle between // asm switchblock + hbl_gradient (red, descending) // or purely hbl_isr_in_c (green, ascending) if(key_hit(KEY_A)) { bDirect ^= 1; irq_set_master(master_isrs[bDirect]); } // (5) Switch priorities of HBlank and VCount if(key_hit(KEY_B)) { //irq_set(II_VCOUNT, vct_wait, bVctPrio); bVctPrio ^= 1; irq_add(II_VCOUNT, vct_isrs[bVctPrio]); } tte_printf("#{es;P}IRS#{X:32}: %s\nPrio#{X:32}: %s\nIE#{X:32}: %04X", strings[bDirect], strings[2+bVctPrio], REG_IE); } return 0; } Th ec o d el i s t i n ga b o v ec o n t a i n st h ema i nd e moc o d e , t h eHBl a n k , a n dVCo u n ti s r st h a twi l l b er o u t e da n ds o mes u n d r yi t e msf o r c o n v e n i e n c e .Th eCma s t e ri s rc a l l e dhbl_grad_direct()i si na n o t h e rf i l e , wh i c hwi l l b ed i s c u s s e dl a t e r . Fi r s t , t h ec o n t e n t so ft h ei n t e r r u p ts e r v i c er o u t i n e s( p o i n t s1a n d2 ) .Bo t hr o u t i n e sa r ep r e t t ys i mp l e : t h eHBl a n kr o u t i n e ( hbl_grad_routed())u s e st h ev a l u eo ft h es c a n l i n ec o u n t e rt os e tac o l o rf o rt h eb a c k d r o p .Att h et o p , REG_VCOUNTi s0 , s ot h e c o l o rwi l l b eb l u e ;a tt h eb o t t o m, i t ' l l b e1 6 0 / 8 =2 0 , s oi t ' ss o me wh e r eb e t we e nb l u ea n dr e d : p u r p l e .No w, y o uma yn o t i c et h a tt h ef i r s t s c a n l i n ei sa c t u a l l yr e da n dn o tb l u e : t h i si sb e c a u s ea )t h eHBl a n ki n t e r r u p to c c u r safter t h es c a n l i n e( wh i c hh a sc a u s e dt r o u b l eb e f o r e i nt h eDMAd e mo )a n db )b e c a u s eHBl a n k sh a p p e nd u r i n gt h eVBl a n ka swe l l , s ot h a tt h ec o l o rf o rl i n e0i ss e ta tREG_VCOUNT=2 2 7 , wh i c hwi l l g i v eab r i g h tr e dc o l o r . Th eVCo u n tr o u t i n e sa c t i v a t ea ts c a n l i n e8 0 .Th e ys e tt h ec o l o rt or e da n dt h e nwa i t su n t i l s c a n l i n e1 2 0 .Th ed i f f e r e n c eb e t we e nt h e t woi st h a tvct_wait()j u s twa i t s , b u tvct_wait_nest()e n a b l e st h eHBl a n ki n t e r r u p t .Re me mb e rt h a tisr_masterd i s a b l e s i n t e r r u p t sb e f o r ec a l l i n ga ns e r v i c er o u t i n e , s ot h el a t t e rVc o u n tr o u t i n es h o u l db ei n t e r r u p t e db yhbl_grad_routed(), b u tt h e f o r me rwo u l dn o t .Asy o uc a ns e ef r o mf i g1 6 . 1 aa n df i g1 6 . 1 b , t h i si se x a c t l ywh a th a p p e n s . Po i n t3i swh e r et h ei n t e r r u p t sa r es e tu pi nt h ef i r s tp l a c e .Th ec a l l t oirq_init()c l e a r st h ei s rt a b l ea n ds e t su pt h ema s t e ri s r . I t sa r g u me n tc a nb eNULL, i nwh i c hc a s et h et o n c ' sd e f a u l tma s t e ri s ri su s e d .Th ec a l l st oirq_add()i n i t i a l i z et h eHBl a n ka n d VCo u n ti n t e r r u p t sa n dt h e i rs e r v i c er o u t i n e s .I fy o ud o n ' ts u p p l yas e r v i c er o u t i n e , t h es wi t c h b o a r dwi l l j u s ta c k n o wl e d g et h ei n t e r r u p t a n dr e t u r n .Th e r ea r et i me swh e nt h i si su s e f u l , a swe ' l l s e ei nt h en e x tc h a p t e r .irq_add()a l r e a d yt a k e sc a r eo fb o t hREG_IEa n d t h eI RQb i t si nREG_DISPSTAT;wh a ti td o e s n ' td oy e ti ss e tt h eVCo u n ta twh i c ht h ei n t e r r u p ts h o u l db et r i g g e r e d , s ot h i si sd o n e s e p a r a t e l y .Th eo r d e ro firq_add()d o e s n ' tr e a l l yma t t e r , b u tl o we ro r d e r sa r es e a r c h e df i r s ts oi tma k e ss e n s et op u tmo r ef r e q u e n t i n t e r r u p t sf i r s t . Yo uc a ns wi t c hb e t we e nma s t e rs e r v i c er o u t i n e swi t hirq_set_master(), a si sd o n ea tp o i n t4 .Po i n t5c h o o s e sb e t we e nt h e n e s t e da n dn o n n e s t e dVCo u n tr o u t i n e . Fig 16.1a: Gr a d i e n t ; n e s t e d vct_wait_nested. Fig 16.1b: Gr a d i e n t ; n o n n e s t e d vct_wait. Fig 16.1c: Gr a d i e n t ; HBl a n ki nma s t e r I S Ri nC. Th i se x p l a i n smo s to fwh a tt h ed e moc a ns h o w.Fo rRe a l Li f eu s e , irq_init()a n dirq_add()a r ep r e t t ymu c ha l l y o un e e d , b u t t h ed e mos h o wss o meo t h e ri n t e r e s t i n gt h i n g sa swe l l .Al s oi n t e r e s t i n gi st h a tt h er e s u l ti sa c t u a l l yal i t t l ed i f f e r e n tf o rVBA, n o $ g b aa n d h a r d wa r e , wh i c hb r i n g su pa n o t h e rp o i n t : i n t e r r u p t sa r et i me c r i t i c a l r o u t i n e s , a n de mu l a t i n gt i mi n gi sr a t h e rt r i c k y .I fs o me t h i n gwo r k so n file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 144/331 28-03-13 Tonc : GBA Programming in rot13 a ne mu l a t o rb u tn o th a r d wa r e , i n t e r r u p t sa r eag o o dp l a c et os t a r tl o o k i n g . Th i sa l mo s tc o n c l u d e sd e mos e c t i o n , e x c e p tf o ro n et h i n g : t h ed i r e c tHBl a n ki s ri nC.Bu tt od ot h a t , wen e e di ti nARM c o d ea n dt o ma k ei te f f i c i e n t , i ts h o u l db ei nI WRAM a swe l l .An dh e r e ' sh o wwed ot h a t . 16.5.1. Using ARM + IWRAM code Th ema s t e ri n t e r r u p tr o u t i n e sh a v et ob eARM c o d e .Aswe ' v ea l wa y sc o mp i l e dt oTHUMBc o d e , t h i swo u l db es o me t h i n gn e w.Th e r e a s o nt h a twe ' v ea l wa y sc o mp i l e dt oTHUMBc o d ei st h a tt h e1 6 b i tb u s e so ft h en o r ma l c o d es e c t i o n sma k eARMc o d es l o wt h e r e . Ho we v e r , wh a twec o u l dd oi sp u tt h eARM c o d ei nI WRAM, wh i c hh a sa3 2 b i tb u s( a n dn owa i t s t a t e s )s ot h a ti t ' sa c t u a l l yb e n e f i c i a l t o u s eARM c o d et h e r e . Co mp i l i n ga sARM c o d ei sa c t u a l l yq u i t es i mp l e : u s e-marmi n s t e a do f-mthumb.Th eI WRAM p a r ti swh a tc a u s e st h emo s t p r o b l e ms .Th e r ea r eGCCe x t e n s i o n st h a tl e ty o us p e c i f ywh i c hs e c t i o naf u n c t i o ns h o u l db ei n .To n c l i bh a st h ef o l l o wi n gma c r o sf o r t h e m: #define EWRAM_DATA __attribute__((section(".ewram"))) #define IWRAM_DATA __attribute__((section(".iwram"))) #define EWRAM_BSS __attribute__((section(".sbss"))) #define EWRAM_CODE __attribute__((section(".ewram"), long_call)) #define IWRAM_CODE __attribute__((section(".iwram"), long_call)) // --- Examples of use: --// Declarations extern EWRAM_DATA u8 data[]; IWRAM_CODE void foo(); // Definitions EWRAM_DATA u8 data[8]= { ... }; IWRAM_CODE void foo() { .... } Th eEWRAM/ I WRAM t h i n g ss h o u l db es e l f e x p l a n a t o r y .Th eDATA_IN_xt h i n g sa l l o wg l o b a l d a t at ob ep u ti nt h o s es e c t i o n s .No t e t h a tt h ed e f a u l ts e c t i o nf o rd a t ai sI WRAM a n y wa y , s ot h a tma yb eal i t t l er e d u n d a n t .EWRAM_BSSc o n c e r n su n i n i t i a l i z e dg l o b a l s .Th e d i f f e r e n c ewi t hi n i t i a l i z e dg l o b a l si st h a tt h e yd o n ' th a v et ot a k eu ps p a c ei nROM: a l l y o un e e dt ok n o wi sh o wmu c hs p a c ey o un e e dt o r e s e r v ei nRAM f o rt h ea r r a y . Th ef u n c t i o nv a r i a n t sa l s on e e dt h elong_calla t t r i b u t e .Co d eb r a n c h e sh a v eal i mi t e dr a n g ea n ds e c t i o nb r a n c h e sa r eu s u a l l yt o o f a rt oh a p p e nb yn o r ma l me a n sa n dt h i si swh a tma k e si two r k .Yo uc a nc o mp a r et h e mwi t h‘ f a r ’a n d‘ n e a r ’t h a tu s e dt ob ep r e s e n ti n PCp r o g r a mmi n g . I ts h o u l db en o t e dt h a tt h e s ee x t e n s i o n sc a nb es o me wh a tf i c k l e .Fo ro n et h i n g , t h ep l a c e me n to ft h ea t t r i b u t e si nt h ed e c l a r a t i o n sa n d d e f i n i t i o n ss e e mst oma t t e r .It h i n kt h ee x a mp l e sg i v e nwo r k , b u ti ft h e yd o n ' tt r yt omo v et h e ma r o u n dab i ta n ds e ei ft h a th e l p s .A b i g g e rp r o b l e mi st h a tt h el o n g _c a l l a t t r i b u t ed o e s n ' ta l wa y swa n tt owo r k .Pr e v i o u se x p e r i e n c eh a sl e dmet ob e l i e v et h a tt h e long_calli si g n o r e dunless t h ed e f i n i t i o no ft h ef u n c t i o ni si na n o t h e rf i l e .I fi t ' si nt h es a mef i l ea st h ec a l l i n gf u n c t i o n , y o u ' l l g e ta ‘ r e l o c a t i o ne r r o r ’ , wh i c hb a s i c a l l yme a n st h a tt h ej u mpi st o of a r .Th eu p s h o to ft h i si st h a ty o uh a v et os e p a r a t ey o u rc o d ed e p e n d i n go n s e c t i o na sf a ra sf u n c t i o n sa r ec o n c e r n e d .Wh i c hwo r k so u tn i c e l y , a sy o u ' l l wa n tt os e p a r a t eARM c o d ea n y wa y . So , f o rARM/ I WRAM c o d e , y o un e e dt oh a v eas e p a r a t ef i l ewi t ht h er o u t i n e s , u s et h eIWRAM_CODEma c r ot oi n d i c a t et h e s e c t i o n , a n du s e-marmi nc o mp i l a t i o n .I ti sa l s oag o o di d e at oa d d-mlong-callst o o , i nc a s ey o ue v e rwa n tt oc a l l ROM f u n c t i o n s f r o mI WRAM.Th i so p t i o nma k e se v e r yc a l l al o n gc a l l .So met o o l c h a i n s( i n c l u d i n gDKP)h a v es e tu pt h e i rl i n k s c r i p t ss ot h a tf i l e swi t h t h ee x t e n s i o n.iwram.ca u t o ma t i c a l l yg oi n t oI WRAM, s ot h a tIWRAM_CODEi so n l yn e e d e df o rt h ed e c l a r a t i o n . I nt h i sc a s e , t h a t ' db et h ef i l ec a l l e disr.iwram.c.Th i sc o n t a i n sas i mp l ema s t e ri s ri nC, a n do n l yt a k e sc a r eo ft h eHBl a n ka n d a c k n o wl e d g i n gt h ei n t e r r u p t s . #include <tonc.h> IWRAM_CODE void hbl_grad_direct(); // an interrupt routine purely in C // (make SURE you compile in ARM mode!!) void hbl_grad_direct() { u32 irqs= REG_IF & REG_IE; REG_IFBIOS |= irqs; if(irqs & IRQ_HBLANK) file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 145/331 28-03-13 Tonc : GBA Programming in rot13 { u32 clr= REG_VCOUNT/8; pal_bg_mem[0]= RGB15(0, clr, 0); } REG_IF= irqs; } Flags for ARM+IWRAM compilation Re p l a c et h e‘ mt h u mb ’i ny o u rc o mp i l a t i o nf l a g sb y‘ ma r mml o n g c a l l s ’ .Fo re x a mp l e : CBASE := $(INCDIR) -O2 -Wall # ROM flags RCFLAGS := $(CBASE) -mthumb-interwork -mthumb # IWRAM flags ICFLAGS := $(CBASE) -mthumb-interwork -marm -mlong-calls Fo rmo r ed e t a i l s , l o o ka tt h ema k e f i l ef o rt h i sp r o j e c t . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 146/331 28-03-13 Tonc : GBA Programming in rot13 17. BIOS Calls I n t r o d u c t i o n Th eBI OSf u n c t i o n s Us i n gBI OSc a l l s De mog r a p h s Vs y n c i n gp a r tI I , VBl a n k I n t r Wa i t Fi n a l t h o u g h t s 17.1. Introduction Ap a r tf r o mh a r d wa r ei n t e r r u p t s , l i k eHBl a n ka n dc a r t r i d g ei n t e r r u p t s , t h e r ea r ea l s ot h i n g sc a l l e dsoftware interrupts, a l s ok n o wna s BIOS calls.Th es o f t wa r ei n t e r r u p t swo r kv e r ymu c hl i k eo r d i n a r yf u n c t i o n s : y o us e t u pi n p u t , c a l l t h er o u t i n e , a n dg e ts o meo u t p u t b a c k .Th ed i f f e r e n c el i e si nh o wy o ur e a c ht h ec o d e ;f o rn o r ma l f u n c t i o n sy o uj u s t , we l l , j u mpt ot h er o u t i n ey o uwa n t .So f t wa r e i n t e r r u p t su s et h eswii n s t r u c t i o n , wh i c hd i v e r t st h ep r o g r a mf l o wt os o me wh e r ei nBI OS, c a r r i e so u tt h er e q u e s t e da l g o r i t h ma n dt h e n r e s t o r e st h en o r ma l f l o wo fy o u rp r o g r a m.Th i si ss i mi l a rt owh a th a r d wa r ei n t e r r u p t sd o , o n l yn o wy o ur a i s et h ei n t e r r u p t p r o g r a mma t i c a l l y .He n c e : s o f t wa r ei n t e r r u p t . Th eGBABI OSh a s4 2s o f t wa r ei n t e r r u p t s , wi t hb a s i cr o u t i n e sf o rc o p y i n g , ma t h( d i v i s i o n , s q u a r er o o t ) , a f f i n et r a n s f o r ma t i o n sf o r s p r i t e sa n db a c k g r o u n d s , d e c o mp r e s s i o na mo n go t h e r s .Th e r ea r ea l s os o mev e r ys p e c i a l f u n c t i o n sl i k et h eIntrWaitr o u t i n e s , wh i c h c a ns t o pt h eCPUu n t i l ah a r d wa r ei n t e r r u p to c c u r s .Th eVBl a n kv a r i a n ti sh i g h l yr e c o mme n d e d , wh i c hi swh a tma k e st h i sc h a p t e r i mp o r t a n t . Us i n gs o f t wa r ei n t e r r u p t si s n ' tt o oh a r di fi twe r e n ' tf o ro n et h i n g : t h eswii n s t r u c t i o ni t s e l f .Th i sa g a i nr e q u i r e ss o mea s s e mb l y . Ho we v e r , n o tmuch a s s e mb l y , a n di t ' se a s yt owr i t eCwr a p p e r sf o rt h e m, wh i c hwe ' l l a l s oc o v e rh e r e . 17.2. The BIOS functions Ca l l i n gt h eBI OSf u n c t i o n sc a nb ed o n ev i at h e‘ swi n’i n s t r u c t i o n , wh e r en i st h eBI OSc a l l y o uwa n tt ou s e .Mi n dy o u , t h ee x a c t n u mb e r sy o un e e dt ou s ed e p e n d so nwh e t h e ry o u rc o d ei si nARM o rTHUMBs t a t e .I nTHUMBt h ea r g u me n ti ss i mp l yt h en i t s e l f , b u ti nARM y o un e e dt ou s en<<1 6 .J u s tl i k en o r ma l f u n c t i o n s , t h eBI OSc a l l sc a nh a v ei n p u ta n do u t p u t .Th ef i r s tf o u rr e g i s t e r s( r 0 r 3 ) a r eu s e df o rt h i sp u r p o s e ;t h o u g ht h ee x a c tp u r p o s ea n dt h en u mb e ro fr e g i s t e r sd i f f e rf o re a c hc a l l . He r e ' sal i s tc o n t a i n i n gt h en a me so fe a c hBI OSc a l l .Ia mn o tg o i n gt os a ywh a te a c ho ft h e md o e ss i n c eo t h e rs i t e sh a v ed o n et h a t a l r e a d ya n di ts e e msp o i n t l e s st oc o p yt h e i rs t u f fv e r b a t i m.Fo rf u l l d e s c r i p t i o n sg ot oGBATe k , f o re x a mp l e .Iwi l l g i v ead e s c r i p t i o no fa f e wo ft h e ms oy o uc a ng e tat a s t eo fh o wt h e ywo r k . 17.2.1. Full list id 0 x 0 0 0 x 0 1 0 x 0 2 0 x 0 3 0 x 0 4 0 x 0 5 0 x 0 6 0 x 0 7 Name So f t Re s e t Re g i s t e r Ra mRe s e t Ha l t St o p I n t r Wa i t VBl a n k I n t r Wa i t Di v Di v Ar m id 0 x 0 8 0 x 0 9 0 x 0 A 0 x 0 B 0 x 0 C 0 x 0 D 0 x 0 E 0 x 0 F Sq r t Ar c Ta n Ar c Ta n 2 CPUSe t CPUFa s t Se t Bi o s Ch e c k s u m Bg Af f i n e Se t Ob j Af f i n e Se t 0 x 1 0 0 x 1 1 0 x 1 2 0 x 1 3 0 x 1 4 0 x 1 5 0 x 1 6 0 x 1 7 Bi t Un Pa c k LZ7 7 Un Co mp WRAM LZ7 7 Un Co mp VRAM Hu f f Un Co mp RLUn Co mp WRAM RLUn Co mp VRAM Di f f 8 b i t Un Fi l t e r WRAM Di f f 8 b i t Un Fi l t e r VRAM 0 x 1 8 0 x 1 9 0 x 1 A 0 x 1 B 0 x 1 C 0 x 1 D 0 x 1 E 0 x 1 F Di f f 1 6 b i t Un Fi l t e r So u n d Bi a s Ch a n g e So u n d Dr i v e r I n i t So u n d Dr i v e r Mo d e So u n d Dr i v e r Ma i n So u n d Dr i v e r VSy n c So u n d Ch a n n e l Cl e a r MI DI Ke y 2 Fr e q 0 x 2 8 So u n d Dr i v e r VSy n c Of f 0 x 2 0 Mu s i c Pl a y e r Op e n file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm Name 147/331 28-03-13 Tonc : GBA Programming in rot13 0 x 2 1 0 x 2 2 0 x 2 3 0 x 2 4 0 x 2 5 0 x 2 6 0 x 2 7 Mu s i c Pl a y e r St a r t Mu s i c Pl a y e r St o p Mu s i c Pl a y e r Co n t i n u e Mu s i c Pl a y e r Fa d e Ou t Mu l t i Bo o t Ha r d Re s e t Cu s t o mHa l t 0 x 2 9 0 x 2 A So u n d Dr i v e r VSy n c On Ge t J u mp Li s t 17.2.2. Div, Sqrt, Arctan2 and ObjAffineSet descriptions 0x06: Div I n p u t : r 0 : n u me r a t o r r 1 : d e n o mi n a t o r Ou t p u t : r 0 : n u me r a t o r/d e n o mi n a t o r r 1 : n u me r a t o r% d e n o mi n a t o r r 3 : a b s ( n u me r a t o r/d e n o mi n a t o r ) No t e : d oNOTd i v i d eb yz e r o ! 0x08: Sqrt I n p u t : r 0 : n u m, au n s i g n e d3 2 b i ti n t e g e r Ou t p u t : r 1 : s q r t ( n u m) 0x0a: ArcTan2 I n p u t : r 0 : x, asigned 16bit n u mb e r( s16) r 1 : y, asigned 16bit n u mb e r( s16) Ou t p u t : r 0 : x≥0: θ=a r c t a n ( y/x)∨ x<0: θ=s i g n ( y) * ( π−a r c t a n ( | y/x| ) . Th i sd o e st h ef u l l i n v e r s eo fy =x* t a n ( θ ) .Th ep r o b l e mwi t ht h et a n g e n ti st h a tt h ed o ma i ni sas e mi c i r c l e , a si st h er a n g eo fa r c t a n g e n t .Tog e tt h ef u l l c i r c l er a n g e , b o t hx a n dy v a l u e sa r er e q u i r e df o rn o to n l yt h e i rq u o t i e n t , b u tt h e i rs i g n sa swe l l .Th e ma t h e ma t i c a l r a n g eo fθ i s[ −π, π, wh i c hc o r r e s p o n d st o[ −0 x 8 0 0 0 , 0 x 8 0 0 0 ( o r[ 0 , 2 π a n d[ 0 , 0 x FFFF]i fy o ul i k e ) 0x0f: ObjAffineSet I n p u t : r 0 : s o u r c ea d d r e s s r 1 : d e s t i n a t i o na d d r e s s r 2 : n u mb e ro fc a l c u l a t i o n s r 3 : Of f s e to fP ma t r i xe l e me n t s( 2f o rb g s , 8f o ro b j e c t s ) Th es o u r c ea d d r e s sp o i n t st oa na r r a yo fAFF_SRCs t r u c t s( a l s ok n o wna sObjAffineSource, wh i c hi sab i tmi s l e a d i n gs i n c ey o u c a nu s et h e mf o rb a c k g r o u n d sa swe l l ) .Th eAFF_SRCs t r u c tc o n s i s to ft wos c a l e ssx, sya n da na n g l eα, wh i c ha g a i nu s e st h er a n g e [ 0 , 0 x FFFF]f o r2 π.Th er e s u l t i n gP: s· c os ( α) −sx· s i n ( α) (17.1) P = x sy· s i n ( α) sy· c os ( α) Byn o wy o us h o u l dk n o wwh a tt h i sd o e s : i ts c a l e sh o r i z o n t a l l yb y1 / sx, v e r t i c a l l yb y1 / syf o l l o we db yac o u n t e r c l o c k wi s er o t a t i o nb yα. ObjAffineSet()d o e sa l mo s te x a c t l ywh a tobj_aff_rotscale()a n dbg_aff_rotscale()d o , e x c e p tt h a t ObjAffineSet()c a na l s os e tmu l t i p l ema t r i c e sa to n c e . Th es o u r c ed a t ai sk e p ti nObjAffineSource( i . e . , AFF_SRC)s t r u c t s .No w, a st h er o u t i n es e t sa f f i n ema t r i c e s , y o umi g h tt h i n k t h a tt h ed e s t i n a t i o n sa r ee i t h e rOBJ_AFFINEo rObjAffineDests t r u c t s .Ho we v e r , y o u ' db ewr o n g .We l l , p a r t i a l l ya n y wa y .Th e p r o b l e mi st h a tt h ed e s t i n a t i o na l wa y sp o i n t st oap ae l e me n t , wh i c hi sn o tn e c e s s a r i l yt h ef i r s te l e me n ti ns t r u c t .Yo uwill ma k et h e mi s t a k eo fs i mp l ys u p p l y i n ga nOBJ_AFFINEp o i n t e rwh e ny o ut r yt ou s ei tt of i l l t h o s e .Do n ' ts a yId i d n ' twa r ny o u . Twoo t h e rt h i n g sn e e dt ob es a i dh e r ea swe l l .Fi r s t , o n c ea g a i nweh a v eab i to fami s n o me r : Ob j Af f i n e Se td o e s n ' tr e a l l yh a v emu c h t od owi t ho b j e c t sp e rs e , b u tc a nb eu s e di nt h a tc a p a c i t yb ys e t t i n gr3t o8i n s t e a do f2 .Th es e c o n di st h a tt h er o u t i n ec a na l s ob eu s e d t os e tu pmu l t i p l ea r r a y sv i ar2.Ho we v e r , be careful wh e ny o ud ot h i swi t hd e v k i t Pr o1 9 .ObjAffineSet()e x p e c t si t ss o u r c e s t r u c t st ob ewo r d a l i g n e d , wh i c ht h e ywo n ' tb eu n l e s sy o ua d dt h ea l i g n me n ta t t r i b u t e sy o u r s e l f . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 148/331 28-03-13 Tonc : GBA Programming in rot13 // Source struct. Note the alignment! typedef struct AFF_SRC { s16 sx, sy; u16 alpha; } ALIGN4 AFF_SRC, ObjAffineSource; // Dst struct for background matrices typedef struct Aff_DST { s16 pa, pb; s16 pc, pd; } ALIGN4 AFF_DST, ObjAffineDest; // Dst struct for objects. Note that r1 should be // the address of pa, not the start of the struct typedef struct OBJ_AFFINE { u16 fill0[3]; s16 pa; u16 fill1[3]; s16 pb; u16 fill2[3]; s16 pc; u16 fill3[3]; s16 pd; } ALIGN4 OBJ_AFFINE; 17.3. Using BIOS calls 17.3.1. Assembly for BIOS calls Yo umi g h tt h i n kt h i swh o l ed i s c u s s i o nwa sr a t h e rp o i n t l e s ss i n c ey o uc a n ' ta c c e s st h er e g i s t e r sa n dt h eswii n s t r u c t i o nu n l e s sy o uu s e a s s e mb l y , wh i c hwi l l b ep r e t t yt o u g h , r i g h t ?We l l , n o , y e sa n dn o .Th en e c e s s a r ya s s e mb l ys t e p sf o rBI OSc a l l sa r ea c t u a l l yr a t h e r s i mp l e , a n da r eg i v e nb e l o w. @ In tonc_bios.s @ at top of your file .text @ aka .section .text .code 16 @ aka .thumb @ for each swi (like division, for example) .align 2 @ aka .balign 4 .global Div .thumb_func Div: swi 0x06 bx lr Th i si sa s s e mb l yc o d ef o rt h eGNUa s s e mb l e r( GAS) ;f o rGo l d r o a do rARM STDt h es y n t a xi sl i k e l yt ob es l i g h t l yd i f f e r e n t .Th ef i r s t t h i n gy o un e e dt od oi sg i v es o medirectives, wh i c ht e l l ss o med e t a i l sa b o u tt h ef o l l o wi n gc o d e .I nt h i sc a s e , weu s et h e‘ .text’t op u t t h ec o d ei nt h etexts e c t i o n( ROM o rEWRAM f o rmu l t i b o o t ) .Wea l s os a yt h a tt h ec o d ei sTHUMBc o d eb yu s i n g‘ .code 16’o r ‘ .thumb’ .I fy o up l a c et h e s ea tt h et o po ft h ef i l e , t h e y ' l l h o l df o rt h er e s to ft h et h i n g .Fo re a c hBI OSc a l l , y o u ' l l n e e dt h ef o l l o wi n g6 i t e ms . Word-alignment.Ora tl e a s th a l f wo r da l i g n me n t , b u two r d sa r ep r o b a b l yp r e f e r a b l e .Th e r ea r et wod i r e c t i v e sf o rt h i s , n s .align na n d.balign m.Th ef o r me ra l i g n st o2 or e q u i r e s‘ .align 2’ ;t h el a t t e ra l i g n st om s oy o uc a nj u s tu s e ‘ balign m’ .No t et h a tb o t hwi l l o n l ywo r ko nt h enext p i e c eo fc o d eo rd a t aa n dn of u r t h e r , wh i c hi swh yi t ' sb e s tt oa d di t f o re a c hf u n c t i o n . Scope.Th e.global named i r e c t i v ema k e sas y mb o l o u to fname, wh i c hwi l l t h e nb ev i s i b l ef o ro t h e rf i l e si nt h ep r o j e c t a swe l l .Ab i tl i k eexterno r , r a t h e r , a na n t i static. Thumb indicator I two u l ds e e mt h a t.code 16a l o n ei s n ' te n o u g h , y o ua l s on e e d.thumb_func.I nf a c t , i fIr e a dt h e ma n u a l c o r r e c t l yt h i so n ea l s oi mp l i e s.code 16, wh i c hwo u l dma k et h a td i r e c t i v er e d u n d a n t . Label.‘ name: ’ma r k swh e r et h es y mb o l name s t a r t s .Ob v i o u s l y , t ou s eaf u n c t i o ni tmu s ta c t u a l l ye x i s t . BIOS call Toa c t u a l l ya c t i v a t et h eBI OSc a l l , u s e‘ s wi n’ , wi t hn t h eBI OSc a l l y o uwa n t . Return An dwe ' r ep r a c t i c a l l yd o n ea l r e a d y , a l l weh a v et od on o wi sr e t u r nt ot h ec a l l e rwi t h‘ b xl r ’ . Se e ?I t ' sr e a l l yn o tt h a tc o mp l i c a t e d .So me t i me sy o umi g h twa n tal i t t l emo r ef u n c t i o n a l i t yt h a nt h i s , b u tf o rt h emo s tp a r ty o uo n l yn e e d t wome a s l yi n s t r u c t i o n s . 17.3.2. The Arm Architecture Procedure Call Standard Th a t ' sa l l f i n ea n dg o o d , b u tt h a ts t i l l l e a v e st h eq u e s t i o n so fa )h o wd oIc o mb i n et h i swi t hCc o d ea n db )wh e r e ' da l l t h ei n p u ta n do u t p u t g o ?Th ea n s we rt ot h ef i r s ti ss i mp l e : j u s ta d daf u n c t i o nd e c l a r a t i o nl i k eu s u a l : file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 149/331 28-03-13 Tonc : GBA Programming in rot13 // In tonc_bios.h int Div(int num, int denom); Mk a y , b u tt h a tstill d o e s n ' te x p l a i nwh e r emyi n p u ta n do u t p u twe n t .We l l a c t u a l l y… i tdoes. “ Ia mn o ts u r eh o wc l o u d sg e tf o r me d .Bu tt h ec l o u d sk n o wh o wt od oi t , a n dt h a ti st h ei mp o r t a n tt h i n g ” Fo u n dt h a tq u o t el o n ga g oi no n eo ft h o s eKi d so nSc i e n c el i s t s , a n dI ' ma l wa y sr e mi n d e do fi twh e np r o g r a mmi n g .Th et h i n ga b o u t c o mp u t e r si st h a tt h e yd o n ' tt h i n ki nt e r mso fi n p u t , o u t p u t , t e x t , p i c t u r e se t c .Ac t u a l l y , t h e yd o n ' tt h i n ka ta l l , b u tt h a t ' sa n o t h e rs t o r y .Al l ac o mp u t e rs e e si sd a t a ;n o te v e nc o d ea n dd a t a , j u s td a t as i n c ec o d ei sd a t at o o .Ofc o u r s e , you ma yn o ts e ei tt h a twa yb e c a u s ey o u ' r e u s e dt oCo rVBo rwh a t e v e r , b u twh e na l l i ss a i da n dd o n e , i t ' sa l l j u s to n e sa n dz e r o s .I ft h eo n e sa n dz e r o sc o met ot h eCPUv i at h e p r o g r a mc o u n t e r( PC r e g i s t e r , r15)i t ' sc o d e , o t h e r wi s ei t ' sd a t a . Soh o wd o e st h a te x p l a i nt h ei n p u t / o u t p u t ?We l l , i td o e s n ' td oi td i r e c t l y , b u ti tp o i n t st oh o wy o us h o u l db el o o k i n ga tt h es i t u a t i o n . Co n s i d e ry o u ' r et h ec o mp i l e ra n dy o uh a v et oc o n v e r ts o meb l o k e ' sCc o d ei n t oma c h i n ec o d e( o ra s s e mb l y , wh i c hi sa l mo s tt h es a me t h i n g )t h a taCPUc a na c t u a l l yu s e .Yo uc o mea c r o s st h el i n e“ q= Div(x,y);” .Wh a td o e sDiv()d o ?We l l , i ft h e r e ' sn os y mb o l i n t h eCf i l ef o rt h a tn a me( a n dt h e r ei s n ' t , a si t ' si ntonc_bios.s) , y o uwo u l d n ' tk n o w.Te c h n i c a l l y , y o ud o n ' te v e nk n o wwhat i ti s .Bu t Divk n o ws , a n dt h a t ' st h ei mp o r t a n tt h i n g .Atl e a s t , t h a t ' salmost h o wi two r k s .Th ec o mp i l e rs h o u l ds t i l l n e e dt ok n o wwh a ts o r to f c r i t t e rDivi st oa v o i dc o n f u s i o n : Av a r i a b l e ?Ama c r o ?Af u n c t i o n ?Th a t ' swh a tt h ed e c l a r a t i o n sa r ef o r .An dt h ed e c l a r a t i o na b o v e s a y st h a tDivi saf u n c t i o nt h a te x p e c t st wos i g n e di n t e g e r sa n dr e t u r n so n et o o .Asf a ra st h ec o mp i l e r ' sc o n c e r n e d , i te n d st h e r e . Ofc o u r s e , t h a ts t i l l d o e s n ' te x p l a i nh o wt h ec o mp i l e rk n o wswh a td ot o .We l l , i ts i mp l yf o l l o wst h eARM Architecture Procedure Call Standard, AAPCS f o rs h o r t .Th i ss t a t e sh o wf u n c t i o n ss h o u l dp a s sa r g u me n t st oe a c ho t h e r .Th i sPDFd o c u me n tc a nb ef o u n dh e r e a n di fy o u ' r ec o n t e mp l a t i n ga s s e mb l yi sav e r ywo r t h wh i l ed o wn l o a d . Fo rn o w, h e r e ' swh a ty o un e e dt ok n o w.Th ef i r s tf o u ra r g u me n t sa r ep l a c e di nt h ef i r s tf o u rr e g i s t e r sr0-r3, e v e r yo n ea f t e rt h a ti s p l a c e do nt h es t a c k .Th eo u t p u tv a l u ei sp l a c e di nr0.Asl o n ga sy o ut a k et h ea r g u me n tl i s to ft h eBI OSc a l l a st h el i s ti nt h e d e c l a r a t i o n , i ts h o u l dwo r kf i n e .No t et h a tt h ed e c l a r a t i o na l s ot a k e sc a r eo fa n yc a s t i n gt h a tn e e d st ob ed o n e .I ti si mp o r t a n tt h a ty o u r e a l i z ej u s twh a tt h ed e c l a r a t i o nme a n sh e r e : it d e t e r mi n e sh o wt h ef u n c t i o ni sc a l l e d , n o tt h ea c t u a l definition a s s e mb l yf u n c t i o n .Or e v e nCf u n c t i o n .Th i n g sc a ng ov e r ywr o n gi fy o ume s su pt h ed e c l a r a t i o n . An o t h e rt h i n gt h eAAPCSt e l l sy o ui st h a tr e g i s t e rr 0 r 3( a n dr 1 2 )a r es o c a l l e dscratch r e g i s t e r s .Th i sme a n st h a tt h ec a l l e rexpects t h ec a l l e df u n c t i o nt ome s st h e s eu p .Af t e rt h ef u n c t i o nr e t u r n st h e i rc o n t e n t sa r et ob ec o n s i d e r e du n d e f i n e d–u n l e s sy o u ' r et h eo n e wr i t i n gb o t ha s mf u n c t i o n s , i nwh i c hc a s et h e r ema yb ec e r t a i n… a l l o wa n c e s .Ha v i n gt h e s ea ss c r a t c hr e g i s t e r sme a n st h a taf u n c t i o n c a nu s et h e mwi t h o u tn e e d i n gt op u s ha n dp o pt h eo r i g i n a l so na n do f ft h es t a c k , t h u ss a v i n gt i me .Th i sd o e sn o th o l df o rt h eo t h e r r e g i s t e r s , t h o u g h : r 4 r 1 1 , r 1 3 , r 1 4must b er e t u r n e di nt h ewa yt h ec a l l i n gf u n c t i o ng o tt h e m.Th el a s to n e , r 1 5 , i se x e mp tf r o mt h i s , a sy o u s h o u l d n ' ts c r e wa r o u n dwi t ht h ep r o g r a mc o u n t e r . 17.3.3. Inline assembly Ac t u a l l y , y o ud o n ' te v e nn e e daf u l l a s s e mb l yf i l ef o rBI OSc a l l s : y o uc o u l du s einline assembly.Wi t hi n l i n ea s s e mb l y , y o uc a nmi xC c o d ea n da s s e mb l yc o d e .Si n c et h ef u n c t i o n sa r eu s u a l l yr a t h e rs i mp l e , y o uc o u l du s es o me t h i n gl i k e // In a C file int Div(int num, int denom) { asm("swi 0x06"); } Th i sd o e se x a c t l yt h es a met h i n ga st h ea s s e mb l yv e r s i o no fDiv.Ho we v e r , y o un e e dt ob ec a r e f u l wi t hi n l i n ea s s e mb l yb e c a u s ey o u c a n ' ts e et h ec o d ea r o u n di ta n dmi g h ta c c i d e n t a l l yclobber s o mer e g i s t e r st h a ty o us h o u l d n ' tb eme s s i n gwi t h , t h u sr u i n i n gt h er e s to ft h e c o d e .Fo rt h ef u l l r u l e so ni n l i n ea s s e mb l y , s e et h eGCCma n u a l .Yo uc a na l s of i n das h o r tf a qo ni n l i n ea s s e mb l yu s ea td e v r s . c o m.Th e ‘ p r o p e r ’s y n t a xo fi n l i n ea s s e mb l yi s n ' tt h ef r i e n d l i e s ti nt h ewo r l d , mi n dy o u , a n dt h e r ea r eo t h e rp r o b l e msa swe l l .Co n s i d e rt h eC f u n c t i o ng i v e na b o v e .Si n c ei td o e s n ' tr e a l l yd oa n y t h i n gi t s e l f , t h eo p t i mi s e rma yb et e mp t e dt ot h r o wi ta wa y .Th i swi l l h a p p e nwi t h-O3 u n l e s sy o ut a k ea p p r o p r i a t ep r e c a u t i o n s .Al s o , t h ec o mp i l e rwi l l c o mp l a i nt h a tt h ef u n c t i o nd o e s n ' tr e t u r na n y t h i n g , e v e nt h o u g hi ts h o u l d . I th a sap o i n t , o fc o u r s e , c o n s i d e r i n gt h a tp a r ti st a k e nc a r eo fi n s i d et h ea s s e mb l yb l o c k .Th e r ea r ep r o b a b l yaf e wo t h e rp r o b l e mst h a t I ' mn o ta wa r eo fa tp r e s e n t ;i nt h ee n di t ' se a s i e rt ou s et h ef u l l a s s e mb l yv e r s i o n ss oy o uk n o wwh a t ' sh a p p e n i n g . 17.3.4. The swi_callmacro Ont h eo t h e rh a n d , t h e r ea r ea l s oBI OSc a l l st h a tu s en oa r g u me n t s , wh i c hc a nb er u nv i aame r ema c r o .Th eswi_call(x)ma c r o wi l l r u nt h eBI OSc a l l x, a n dc a nb ef o u n di nswi.h, a n di nWi n t e r mu t e ' sl i b g b a , wh i c hi swh e r eIg o ti tf r o m.I t ' sal i t t l emo r er e f i n e d t h a nt h eDivf u n c t i o ng i v e na b o v e .Fi r s t , i tu s e st h evolatilek e y wo r d , wh i c hs h o u l dk e e py o u ro p t i mi z e rf r o md e l e t i n gt h ef u n c t i o n ( j u s tl i k ewed i df o ra l l t h er e g i s t e r s ) .Se c o n d l y , i tu s e saclobber list ( a f t e rt h et r i p l ec o l o n s ) .Th i swi l l t e l l t h ec o mp i l e rwh i c hr e g i s t e r s a r eu s e db yt h ei n l i n ea s s e mb l y .Th i r d l y , i twi l l t a k ec a r eo ft h eTHUMB/ ARM s wi t c ha u t o ma t i c a l l y .I fy o uu s et h e-mthumbc o mp i l e r o p t i o n , t h ec o mp i l e rwi l l d e f i n e__thumb__f o ru s , wh i c hwewi l l n o wu s et og e tt h er i g h ts wi n u mb e r .Cl e v e r , e h ? #ifndef(__thumb__) #define swi_call(x) #else #define swi_call(x) asm volatile("swi\t"#x ::: "r0", "r1", "r2", "r3") asm volatile("swi\t"#x"<<16" ::: "r0", "r1", "r2", "r3") file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 150/331 28-03-13 Tonc : GBA Programming in rot13 #endif Byt h ewa y , i fy o uwa n tmo r ei n f o r ma t i o na b o u ta s s e mb l y , y o uc a nf i n dan u mb e ro ft u t o r i a l so nARM a s s e mb l ya tg b a d e v . o r g .An o t h e r n i c ewa yt ol e a r ni sb yu s i n gt h e-Sc o mp i l e rf l a g , wh i c hwi l l g i v ey o uac o mp i l e r g e n e r a t e da s s e mb l yf i l eo fy o u rc o d e .Th i swi l l s h o w y o ue x a c t l ywh a tt h ec o mp i l e rd o e st oy o u rc o d e , i n c l u d i n go p t i mi s a t i o ns t e p sa n du s eo ft h eAAPCS.Re a l l y , y o us h o u l ds e et h i sa tl e a s t o n c e . I tma ya l s oh e l pt ou s e-fverbose-asm, wh i c hwi l l wr i t eo u tt h eo r i g i n a l v a r i a b l en a me sa n do p e r a t i o n si nc o mme n t s .Us u a l l yi n t h er i g h tp l a c et o o .Al s oh a n d yi st h eASM_CMT()ma c r os h o wnb e l o w.Th i swi l l g i v ey o us o meh i n t sa st owh e r es p e c i f i cb l o c k so f c o d ea r e .Bu ta g a i n , n o ta l l t h et i me . #define ASM_CMT(str) asm volatile("@ " str) //In code. Outputs "@ Hi, I'm here!" in the generated asm ASM_CMT("Hi, I'm here!"); 17.4. Demo graphs Toi l l u s t r a t et h eu s eo fBI OSc a l l sIa mu s i n gDi v , Sq r t , Ar c Ta na n dOb j Af f i n e Se tt oc r e a t e g r a p h si fah y p e r b o l e , s q u a r er o o t , s i n ea n dc o s i n e .I ' v es c a l e dt h e mi ns u c hawa ys ot h a t t h e yf i tn i c e l yo nt h e2 4 0 x 1 6 0s c r e e n .Th ed e f i n i t i o n sa r e d i v i s i o n y=2 5 6 0 / x s q u a r er o o ty=1 6 0 * s q r t ( x/ 2 4 0 ) a r c t a n y=8 0+6 4 * ( 2 / π) * ( a r c t a n ( x1 2 0 ) / 1 6 ) ) s i n e y=1 * s y * s i n ( 2 π· x/ 2 4 0 ) ;s y =8 0 c o s i n e y=8 0 * s x * c o s ( 2 π· x/ 2 4 0 ) ;s x =1 a n dt h e s ef u n c t i o n sh a v eb e e np l o t t e di nf i g1 .I fy o u ' r ewo n d e r i n gh o wIg o tt h es i n ea n d Fig 1:di v , s q r t , a r c t a n 2 , s i na n dc o sg r a ph s , t e s yo fBI OS . c o s i n ev a l u e s , a st h e r ea r en oc a l l sf o rt h o s e , t a k eal o o ka te q1a g a i n .Th eP ma t r i xh a s cour t h e m.I ' v eu s e dp af o rt h ec o s i n ea n dp cf o rt h es i n e .No t et h a tt h eg r a p h sa p p e a ri n s t a n t l y ; t h e r ei sn os e n s eo fl o a d i n gt i mewh i l et h eg r a p h sa r ep l o t t e d .Ane a r l i e rv e r s i o no ft h emo d e7d e mo( o rPERNsmo d e7d e mo )u s e d c a l l st ot h ea c t u a l d i v i s i o n , s i n ea n dc o s i n ef u n c t i o n st ob u i l du pt h eLUTs .Ev e nwi t ht h es y mme t r yr u l e so ft r i g o n o me t r y , sin()a n d cos()a r es t i l l n o t i c e a b l ys l o we rt h a nt h eBI OSe q u i v a l e n t . #include <stdio.h> #include <tonc.h> // === swi calls ====================================================== // Their assembly equivalents can be found in tonc_bios.s void VBlankIntrWait() { swi_call(0x05); } int Div(int num, int denom) { swi_call(0x06); } u32 Sqrt(u32 num) { swi_call(0x08); } s16 ArcTan2(s16 x, s16 y) { swi_call(0x0a); } void ObjAffineSet(const AFF_SRC *src, void *dst, int num, int offset) { swi_call(0x0f); } // === swi demos ====================================================== // NOTE! // To be consistent with general mathematical graphs, the // y-axis has to be reversed and the origin moved to the // either the bottom or mid of the screen via // "iy = H - y" // or // "iy = H/2 - y" // // functions have been scaled to fit the graphs on the 240x160 screen file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 151/331 28-03-13 Tonc : GBA Programming in rot13 // y= 2560/x void div_demo() { int ix, y; for(ix=1; ix<SCREEN_WIDTH; ix++) { y= Div(0x0a000000, ix)>>16; if(y <= SCREEN_HEIGHT) m3_plot(ix, SCREEN_HEIGHT - y, CLR_RED); } tte_printf("#{P:168,132;ci:%d}div", CLR_RED); } // y= 160*sqrt(x/240) void sqrt_demo() { int ix, y; for(ix=0; ix<SCREEN_WIDTH; ix++) { y= Sqrt(Div(320*ix, 3)); m3_plot(ix, SCREEN_HEIGHT - y, CLR_LIME); } tte_printf("#{P:160,8;ci:%d}sqrt", CLR_LIME); } // y = 80 + tan((x-120)/16) * (64)*2/pi void arctan2_demo() { int ix, y; int ww= SCREEN_WIDTH/2, hh= SCREEN_HEIGHT/2; for(ix=0; ix < SCREEN_WIDTH; ix++) { y= ArcTan2(0x10, ix-ww); m3_plot(ix, hh - y/256, CLR_MAG); } tte_printf("#{P:144,40;ci:%d}atan", CLR_MAG); } // wX= 1, wY= 80 // cc= 80*sx*cos(2*pi*alpha/240) // ss= 1*sy*sin(2*pi*alpha/240) void aff_demo() { int ix, ss, cc; ObjAffineSource af_src= {0x0100, 0x5000, 0}; // sx=1, sy=80, alpha=0 ObjAffineDest af_dest= {0x0100, 0, 0, 0x0100}; // =I (redundant) for(ix=0; ix<SCREEN_WIDTH; ix++) { ObjAffineSet(&af_src, &af_dest, 1, BG_AFF_OFS); cc= 80*af_dest.pa>>8; ss= af_dest.pc>>8; m3_plot(ix, 80 - cc, CLR_YELLOW); m3_plot(ix, 80 - ss, CLR_CYAN); // 0x010000/0xf0 = 0x0111.111... af_src.alpha += 0x0111; } tte_printf("#{P:48,38;ci:%d}cos", CLR_YELLOW); tte_printf("#{P:72,20;ci:%d}sin", CLR_CYAN); } // === main =========================================================== int main() { REG_DISPCNT= DCNT_MODE3 | DCNT_BG2; tte_init_bmp_default(3); tte_init_con(); file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 152/331 28-03-13 Tonc : GBA Programming in rot13 div_demo(); sqrt_demo(); aff_demo(); arctan2_demo(); while(1); return 0; } 17.5. Vsyncing part II, VBlankIntrWait Un t i l n o w, a l l d e mo su s e dt h ef u n c t i o nvid_vsynct os y n c h r o n i z et h ea c t i o nt ot h eVBl a n k( s e et h eg r a p h i c si n t r o d u c t i o n ) .Wh a tt h i s d i dwa st oc h e c kREG_VCOUNTa n ds t a yi nawh i l el o o pu n t i l t h en e x tVBl a n kc a mea l o n g .Wh i l ei two r k s , i t ' sr e a l l yap r e t t yp o o rwa y o fd o i n gt h i n g sf o rt wor e a s o n s .Fi r s t , b e c a u s eo ft h ep o t e n t i a l p r o b l e mwh e ny o ua r ei naVBl a n ka l r e a d y , b u tt h a to n eh a db e e n c o v e r e d .Th es e c o n dr e a s o ni smo r ei mp o r t a n t : wh i l ey o u ' r ei nt h ewh i l el o o p , y o u ' r ewa s t i n ga na wf u l l o to fCPUc y c l e s , a l l o fwh i c h s l u r pb a t t e r yp o we r . Th e r ea r ean u mb e ro fBI OSc a l l st h a tc a np u tt h eCPUi n t oal o wp o we rmo d e , t h u s s p a r i n gt h eb a t t e r i e s .Th ema i nBI OSc a l l f o rt h i si sHa l t( #2 ) , b u twh a twe ' r ec u r r e n t l y i n t e r e s t e di ni sVBl a n k I n t r Wa i t( #5 ) .Th i swi l l s e tt h i n g su pt owa i tu n t i l t h en e x tVBl a n k i n t e r r u p t .Tou s ei t , y o uh a v et oh a v ei n t e r r u p t ss wi t c h e do n , o fc o u r s e , i np a r t i c u l a rt h e VBl a n ki n t e r r u p t .Asu s u a l , t h eVBl a n ki s rwi l l h a v et oa c k n o wl e d g et h ei n t e r r u p tb ywr i t i n g t oREG_IF.Bu ti talso h a st owr i t et oi t sBI OSe q u i v a l e n t , REG_IFBIOS.Th i sl i t t l eb i to f i n f o r ma t i o ni sal i t t l eh a r dt of i n de l s e wh e r e( i np a r tb e c a u s ef e wt u t o r i a l sc o v e rBI OS c a l l s ) ;f o rmo r ei n f o , s e eGBATe k , BI OSHa l tFu n c t i o n s .Fo r t u n a t e l yf o ru s , t h e s wi t c h b o a r dp r e s e n t e di nt h ei n t e r r u p t ss e c t i o nh a st h i sb u i l ti n . Tos h o wy o uh o wt os e ti tu p , s e et h eswi_vsyncd e mo .Th emo s ti mp o r t a n tc o d ei s Fig 2:swi_vsyncdemo. g i v e nb e l o w;as c r e e ns h o tc a nb ef o u n di nf i g2 .Wh a ti td o e si sg i v ear o t a t i n gme t r o i d s p r i t ewi t ha na n g u l a rv e l o c i t yo fπr a d / s( t h i sc o r r e s p o n d st oΔθ=0 x 1 0 0 0 0 / 4 / 6 0 =0 x 0 1 1 1 ) .Th eb a s i cs t e p sf o ri n t e r r u p th a n d l i n gs h o u l d b ef a mi l i a r , e x c e p tt h ef a c tt h a tt h e r e ' sn or e a l VBl a n ki s rb e c a u s et h es wi t c h b o a r da l r e a d yt a k e sc a r eo fa c k n o wl e d g i n gt h ei n t e r r u p t . Af t e rt h a ti t ' sp r e t t ys i mp l e : weu s eObjAffineSet()t oc a l c u l a t et h er e q u i r e da f f i n ema t r i xa n dVBlankIntrWaitp u t st h eCPU o nHa l tu n t i l t h en e x tVBl a n ki n t e r r u p t . // inside main, after basic initialisations AFF_SRC as= { 0x0100, 0x0100, 0 }; OBJ_AFFINE oaff; // enable isr switchboard and VBlank interrupt irq_init(NULL); irq_add(II_VBLANK, NULL); while(1) { VBlankIntrWait(); // Full circle = 10000h // 10000h/4/60= 111h -> 1/4 rev/s = 1 passing corner/s as.alpha += 0x0111; ObjAffineSet(&as, &oaff.pa, 1, 8); obj_aff_copy(obj_aff_mem, &oaff, 1); } Prefer VBlankIntrWait() over vid_vsync() Wa i t i n gf o rt h eVBl a n kv i avid_vsync()( o ri t sf u n c t i o n a l e q u i v a l e n t )i sn o tag o o di d e a : i twa s t e st o omu c hb a t t e r y p o we r .Th er e c o mme n d e dp r o c e d u r ei su s i n gVBlankIntrWait()t oh a l tt h ep r o c e s s o r , t ob ewo k e na g a i no nt h e VBl a n ki n t e r r u p t . Acknowledging IntrWait routines VBlankIntrWait()i so n l yo n eo ft h eBI OS' sIntrWait()r o u t i n e st h a tc a ns t o pt h eCPUu n t i l a ni n t e r r u p th a sb e e n r a i s e d .Ho we v e r , i td o e s n ' tl o o ka tREG_IFb u ta tREG_IFBIOS( 0 3 0 0 : 7 FF8 )f o rt h ea c k n o wl e d g e me n to ft h ei n t e r r u p t .I f y o u rg a mel o c k su pa f t e rt r y i n gVBlankIntrWait(), t h i sma yb ewh y .No t et h a ty o uma yf i n dt h ea d d r e s su n d e ro t h e r n a me s , a st h e r ei s n ' tr e a l l ya no f f i c i a l o n ef o ri t . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 153/331 28-03-13 Tonc : GBA Programming in rot13 17.6. Final thoughts No wt h a ty o uk n o wh o wt ou s et h e m, Is h o u l dwa r ny o ut h a ty o us h o u l d n ' tg oo v e r b o a r dwi t ht h e m.I ta p p e a r st h a tt h eBI OSr o u t i n e s h a v eb e e nd e s i g n e df o rs p a c e , n o ts p e e d , s ot h e ya r e n ' tt h ef a s t e s ti nt h ewo r l d .No to n l yt h a t , t h e r e ' sa no v e r h e a do fa tl e a s t6 0c y c l e s f o re a c ho n e( mi n dy o u , n o r ma l f u n c t i o n ss e e mt oh a v ea3 0c y c l eo v e r h e a d ) .I fs p e e di swh a ty o u ' r ea f t e rt h e nt h eBI OSc a l l sma yn o t b et h eb e s tt h i n g ;y o uc a np r o b a b l yf i n df a s t e rr o u t i n e so nt h ewe b. . .s o me wh e r e .Th i sd o e s n ' tme a nt h a tt h eBI OSr o u t i n e sc a n ' tb e u s e f u l , o fc o u r s e , b u ti fy o uh a v ea l t e r n a t i v eme t h o d s , u s et h o s ei n s t e a d .J u s tr e me mb e rt h a tt h a t ' sa no p t i mi s a t i o ns t e p , wh i c hy o u s h o u l d n ' td op r e ma t u r e l y . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 154/331 28-03-13 Tonc : GBA Programming in rot13 18. Beep! GBA sound introduction I n t r o d u c t i o nt oGBAs o u n d So u n da n dWa v e s GBAs o u n d De mot i me 18.1. Introduction to GBA sound Ap a r tf r o mg r a p h i c sa n di n t e r a c t i o n , t h e r ei so n eo t h e rs e n s ei mp o r t a n tt og a me s : a u d i o .Wh i l eg r a p h i c sma ys e tt h es c e n e , s o u n ds e t s t h emo o d , wh i c hc a nb ee v e nmo r ei mp o r t a n tt h a tt h eg r a p h i c s .Tr yp l a y i n gRe s i d e n tEv i l wi t h , s a y , We i r dAl Ya n k o v i cp l a y i n g : i ts i mp l y d o e s n ' two r k , t h ea t mo s p h e r ei sl o s t . Th eGBAh a ss i xs o u n dc h a n n e l s .Th ef i r s tf o u ra r er o u g h l yt h es a mea st h eo r i g i n a l Ga me Bo yh a d : t wos q u a r ewa v eg e n e r a t o r s ( c h a n n e l s1a n d2 ) , as a mp l ep l a y e r( c h a n n e l 3 )a n dan o i s eg e n e r a t o r( c h a n n e l 4 ) .Th o s ea r ea l s or e f e r r e dt oa st h eDMGc h a n n e l s . Ne wa r et woDi r e c tSo u n dc h a n n e l sAa n dB( n o tt ob ec o n f u s e dwi t hMi c r o s o f t ' sDi r e c tSo u n d , t h eDi r e c t Xc o mp o n e n t ) .Th e s ea r e 8 b i td i g i t a l c h a n n e l s . Is h o u l dp o i n to u tt h a tIr e a l l yk n o wv e r yl i t t l ea b o u ts o u n dp r o g r a mmi n g , mo s t l yb e c a u s eI ' mn o ta b l et oa c t u a l l yp u tt o g e t h e rap i e c eo f mu s i c( i t ' sk i n d ah a r dt od ot h a twh e ny o ua l r e a d yh a v emu s i cp l a y i n g ) .I fy o uwa n tt or e a l l yl e a r na b o u ts o u n dp r o g r a mmi n g , y o us h o u l d l o o ka tBe l o g i c . c o m, wh e r ea l mo s te v e r y b o d yg o tt h e i ri n f o r ma t i o nf r o m, a n dd e k u . g b a d e v . o r g , wh i c hs h o wsy o uh o wt ob u i l das o u n d mi x e r .Bo t ho ft h e s es i t e sa r ee x c e l l e n t . Ima yn o tk n o wmu c ha b o u ts o u n dc r e a t i o n / p r o g r a mmi n g , b u ta ti t sc o r es o u n di sawa v ei nma t t e r ;wa v e sa r ema t h e ma t i c a l c r i t t e r s , a n dIdo k n o wat h i n go rt woa b o u tma t h , a n dt h a t ' sk i n do fwh a tI ' l l d oh e r ef o rt h es q u a r ewa v eg e n e r a t o r s . 18.2. Sound and Waves Co n s i d e ri fy o uwi l l , ama s s i v es e ao fp a r t i c l e s , a l l c o n n e c t e dt ot h e i rn e i g h b o u r swi t hl i t t l es p r i n g s .No wg i v eo n eo ft h e mal i t t l ep u s h .I n t h ed i r e c t i o no ft h ep u s h , t h es p r i n gc o mp r e s s e sa n dr e l a x e s , p u s h i n gt h eo r i g i n a l p a r t i c l eb a c kt oi t sn o r ma l p o s i t i o na n dp a s s i n go nt h e p u s ht ot h en e i g h b o u r ;t h i sc o mp r e s s e st h en e x ts p r i n ga n dr e l a y st h ep u s ht oits n e i g h b o u r , a n ds oo na n ds oo n . Th i si sap r i mee x a mp l eo fwa v eb e h a v i o u r .Gi v i n gap r e c i s ed e f i n i t i o no fawa v et h a tc o v e r sa l l c a s e si st r i c k y , b u ti ne s s e n c e , a wave i satransferred disturbance.Th e r ea r ema n yk i n d so fwa v e s ;t woma j o rc l a s s e sa r elongitudinal wa v e s , wh i c ho s c i l l a t ei nt h e d i r e c t i o no ft r a v e l , a n dtransverse wa v e s , wh i c ha r ep e r p e n d i c u l a rt oi t .So mewa v e sa r ep e r i o d i c( r e p e a t i n gp a t t e r n so v e rt i meo r s p a c e ) , s o mea r e n ' t .So met r a v e l , s o med o n ' t . 18.2.1. Waves Th ec a n o n i c a l wa v ei st h eharmonic wave.Th i si sa n yf u n c t i o nψ( x)t h a t ' sas o l u t i o nt oe q1 8 . 1 .Th en a meo ft h ev a r i a b l ed o e s n ' tr e a l l y ma t t e r , b u tu s u a l l yi t ' se i t h e rs p a t i a l ( x, y, z)o rt e mp o r a l ( t) , o ra l l o ft h e s ea tt h es a met i me .Th eg e n e r a l s o l u t i o nc a nb ef o u n di n e q1 8 . 2 .Orp e r h a p sIs h o u l ds a ys o l u t i o n s, a st h e r ea r ema n ywa y so fwr i t i n gt h e md o wn .Th e y ' r ea l l e q u i v a l e n tt h o u g h , a n dy o uc a ng o f r o mo n et ot h eo t h e rwi t hs o met r i c k e r yt h a td o e sn o tc o n c e r nu sa tt h i smo me n t . (18.1) d ² ψ( x)+k ²ψ( x)=0 d x ² Ge n e r a l s o l u t i o n ( s ) : ψ( x)=A· c os ( k x)+B· s i n ( k x) i k x i k x =C· e +D· e (18.2) =E· s i n ( k x +φ) 0 Af u l l wa v ec a nb ed e s c r i b e db yt h r e et h i n g s .Fi r s t , t h e r e ' st h eamplitude A, wh i c hg i v e sh a l f d i s t a n c eb e t we e nt h emi n i mu ma n dma x i mu m.Se c o n d , t h ewavelength λ , wh i c hi st h el e n g t h a f t e rwh i c ht h ewa v er e p e a t si t s e l f( t h i si st i e dt owa v e n u mb e rk=2 π/ λ ) .Th e nt h e r e ' sphase constant φ, w h i c h d e f i n e s t h e s t a t i n g p o i n t . I f t h e w a v e i s i n t i m e , i n s t e a d o f a w a v e l e n g t hy o u 0 h a v eperiod T, frequency f=1 / T( a n da n g u l a rf r e q u e n c yω=2 πf=2 π/ T) .Yo uc a ns e ewh a te a c h o ft h e s ep a r a me t e r si si nf i g1 8 . 1 . Fig 18.1: ah a r mo n i cwa v e On eo ft h ei n t e r e s t i n gt h i n g sa b o u tt h ewa v ee q u a t i o ni st h a ti ti sal i n e a ro p e r a t i o no nψ. Wh a tt h a tme a n si st h a ta n yc o mb i n a t i o no fs o l u t i o n si sa l s oas o l u t i o n ;t h i si st h esuperposition principle.Fo re x a mp l e , i fy o uh a v et wo wa v e sψ1a n dψ, t h e n Ψ = aψ + bψ i s a l s o a w a v e . T h i s m a y s o u n d l i k e a t r i v i a l t h i n g b u t I a s s u r e y o u i t ' s n o t . T h e f a c t t h a t n o n l i n e a r 2 1 2 file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 155/331 28-03-13 Tonc : GBA Programming in rot13 e q u a t i o n s( a n dt h e ye x i s tt o o )t e n dt oma k es c i e n t i s t sc r i n g eal i t t l es h o u l dt e l l y o us o me t h i n ga b o u tt h ev a l u eo fl i n e a re q u a t i o n s . 18.2.2. Sound waves So u n di sa l s oawa v e .I nf a c t , i ti sal o n g i t u d i n a l p r e s s u r ewa v ei nma t t e ra n dp r e t t ymu c hwo r k sa st h es y s t e mo fp a r t i c l e so ns p r i n g s me n t i o n e de a r l i e rwi t hwh o l es e t so fmo l e c u l e smo v i n gb a c ka n df o r t h .I np r i n c i p l e , i th a sb o t hs p a t i a l a n dt e mp o r a l s t r u c t u r ea n dy o u c a nt h i n g sc a ng e th i d e o u s l yc o mp l e xi fy o uwa n tt od e a l wi t he v e r y t h i n g .Bu tI ' l l k e e pi te a s ya n do n l yc o n s i d e rt wop a r t s : a mp l i t u d eA a n dp e r i o da n df r e q u e n c yT a n df.Asy o up r o b a b l yk n o w, t h et o n eo fas o u n di sr e l a t e dt ot h ef r e q u e n c y .Hu ma nh e a r i n gh a sar a n g e b e t we e n2 0Hza n d2 0k Hz , a n dt h eh i g h e rt h ef r e q u e n c y( i . e . , t h emo r ec o mp r e s s e dt h ewa v e ) , t h eh i g h e rt h et o n e .Mo s ts o u n d sa r e a c t u a l l yac o n g l o me r a t i o no fd i f f e r e n twa v e s , wi t hd i f f e r e n ta mp l i t u d e sa n df r e q u e n c i e s–t h es u p e r p o s i t i o np r i n c i p l ea two r k .Th ef u n n y t h i n ga b o u tt h i si st h a ti fy o ua d d e da l l t h o s ec o mp o n e n t su pt oo n es i n g l ef u n c t i o na n dp l o ti t , i two u l d n ' tl o o kl i k eas i n ewa v ea ta l l a n y mo r e .Wh a t ' se v e nf u n n i e ri st h a ty o uc a na l s or e v e r s et h ep r o c e s sa n dt a k eaf u n c t i o n–any f u n c t i o n –a n db r e a ki tu pi n t oa s u p e r p o s i t i o no fs i n ea n dc o s i n ewa v e s , a n ds os e ewh a tk i n do ff r e q u e n c i e sy o u rs o u n dh a s .Th i si sc a l l e dFo u r i e rTr a n s f o r ma t i o n , a n d we ' l l g e tt ot h a ti nami n u t e . 18.2.3. Musical scale Wh i l et h ef u l l r a n g eb e t we e n2 0Hza n d2 0k Hzi sa u d i b l e , o n l yad i s c r e t es e to ff r e q u e n c i e sa r eu s e df o rmu s i c , wh i c hb r i n g su st ot h e n o t i o no ft h emusical scale.Ce n t r a l t ot h e s ea r eoctaves, r e p r e s e n t i n gaf r e q u e n c yd o u b l i n g .Ea c ho c t a v ei sd i v i d e di n t oan u mb e ro f d i f f e r e n tn o t e s ;1 2i nWe s t e r ns y s t e ms , r a n g i n gf r o mAt oG, a l t h o u g ho c t a v en u mb e r i n gs t a r t sa tCf o rs o mer e a s o n .Oc t a v e0s t a r t sa t t h ecentral C, wh i c hh a saf r e q u e n c yo fa b o u t2 6 2Hz( s e ea l s ot a b l e1 8 . 1 .An dy e s , Ik n o wt h e r ea r eo n l y7l e t t e r sb e t we e nAa n dG, t h eo t h e rn o t e sa r ef l a t sa n ds h a r p st h a tl i eb e t we e nt h e s en o t e s .Th e‘ 1 2 ’r e f e r st ot h en u mb e ro fh a l f n o t e si na no c t a v e .Th emu s i c a l 1 / 1 2 s c a l ei slogarithmic;e a c hh a l f n o t eb e i n g2 a p a r t .We l l , a l mo s ta n y wa y : f o rs o mer e a s o n , s o men o t e sd o n ' tquite f i ti ne x a c t l y . half-note 0 1 2 3 4 5 6 7 8 9 10 11 (12) name C C# D D# E F F# G G# A A# B ( C) 6 1 . 72 7 7 . 22 9 3 . 73 1 1 . 23 2 9 . 73 4 9 . 33 7 0 . 03 9 2 . 04 1 5 . 34 4 0 . 04 6 6 . 24 9 3 . 9( 5 2 3 . 3 ) freq (Hz) 2 Table 18.1: n o t e s&f r e q u e n c i e so fo c t a v e0 18.2.4. Fourier transforms and the square wave Fo u r i e rt r a n s f o r ma t i o n sa r eawa yo fg o i n gd e s c r i b i n gaf u n c t i o ni nt h et i med o ma i na sad i s t r i b u t i o no ff r e q u e n c i e sc a l l e daspectrum. Th e y ' r ea l s oo n eo ft h ema n ywa y st h a tp r o f e s s o r sc a ns c a r et h eb e j e b u so u to fy o u n g , n a t u r a l s c i e n c es t u d e n t s .Do n ' two r r y , I ' ms u r e y o u ' l l g e tt h r o u g ht h i ss e c t i o nu n s c a t h e d>:).Fo rwe l l -t or e a s o n a b l y b e h a v e df u n c t i o n s , y o uc a nr e wr i t et h e ma ss e r i e so fvery we l l b e h a v e df u n c t i o n ss u c ha sp o l y n o mi a l s , e x p o n e n t i a l sa n da l s owa v e s .Fo re x a mp l e , a saFo u r i e rs e r i e s , af u n c t i o nma yl o o kl i k ee q1 8 . 3 . (18.3) f ( t)=½A0+Σn>0Amc os ( mωt)+Σn>0Bms i n ( mωt) Ofc o u r s e , t h ewh o l et h i n gr e l i e so nb e i n ga b l et of i n dt h ec o e f f i c i e n t sAm a n dBm.Wh i l ei ti sf a i r l ys t r a i g h t f o r wa r dt od e r i v et h e e q u a t i o n sf o rt h e m, I ' l l l e a v et h a ta sa ne x e r c i s ef o rt h er e a d e ra n dj u s tp r e s e n tt h er e s u l t si nt h ef o r mo fe q1 8 . 4 .Is h o u l dme n t i o nt h a t t h e r ea r ea c t u a l l yaf e wwa y so fd e f i n i n gFo u r i e rt r a n s f o r ms .Fo re x a mp l e , t h e r ea r ev e r s i o n st h a td o n ' ti n t e g r a t eo v e r[ 0 , T] , b u to v e r [ −½T, ½T] ;o ru s et h ec o mp l e xe x p o n e n t i a l i n s t e a do fs i n e sa n dc o s i n e s , b u ti nt h ee n dt h e y ' r ea l l d o i n gt h es a met h i n g . 2 Am = ∫ f ( t)c os ( mωt)d t 0 , T (18.4) T 2 Bm = ∫ f ( t)s i n ( mωt)d t 0 , T T Asa ne x a mp l e , l e t ' st a k eal o o ka tas q u a r ewa v es e ef i g1 8 . 2 .As q u a r ewa v ei so n( 1 )f o ra c e r t a i nt i me( p a r a me t e rh) , t h e no f f( 0 )f o rt h er e s to ft h ec y c l e . I t ' ss t i l l ap e r i o d i cwa v e , s oi t d o e s n ' tr e a l l yma t t e rwh e r ewep l a c et h et h i n ga l o n gt h eta x i s .Ic e n t e r e di to nt h ep e a kf o r c o n v e n i e n c e : d o i n gs oma k e si tas y mme t r i c a l wa v ewh i c hh a st h en i c ep r o p e r l yo fr e mo v i n gall t h ea n t i s y mme t r i c a l s i n ewa v e s .A0=h/ Tb e c a u s ei t ' st h ea v e r a g eo ft h ef u n c t i o na n dt h er e s to f t h eAm' sf o l l o wf r o me q1 8 . 4 . Fig 18.2: as q u a r ewa v e 2s i n ( πmh/T) 2T s i n ( πh/T m) = · π m h πh/T m (18.5) Am = · Am i sasinc f u n c t i o n : s i n ( x) / x.Fo rh i g hm i ta p p r o a c h e sz e r o( a si ts h o u l d , s i n c eh i g h e rt e r mss h o u l db er e l a t i v e l yl e s si mp o r t a n t ) , b u ta l s o i n t e r e s t i n gi st h a to ft h eh i g h e rt e r mss o mewi l l a l s ovanish b e c a u s eo ft h es i n e .Th i swi l l h a p p e nwh e n e v e rm i samu l t i p l eo fT/h. 18.3. GBA sound 18.3.1. Sound registers Fo rg r a p h i c s , y o uo n l yh a dt od e a l wi t ho n er e g i s t e rt og e tar e s u l t ;f o rs o u n d , y o uh a v et oc o v e ral o to fr e g i s t e r sb e f o r ey o ug e t file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 156/331 28-03-13 Tonc : GBA Programming in rot13 anything.Th eDMGc h a n n e l se a c hh a v e2o r3r e g i s t e r s–s o mewi t hs i mi l a rf u n c t i o n a l i t y , s o men o t .Ap a r tf r o mt h a t , t h e r ea r ef o u r o v e r a l l c o n t r o l r e g i s t e r s . Th er e g i s t e rn o me n c l a t u r es e e msp a r t i c u l a r l yv e x e dwh e ni tc o me st os o u n d .Th e r ea r eb a s i c a l l yt wos e t so fn a me st h a ty o uc a n f i n d : o n ec o n s i s t i n go fREG_SOUNDxCNTf o l l o we db y_L, _Ha n d_Xi nar a t h e rh a p h a z a r dma n n e r ;t h eo t h e ro n eu s e saREG_SGxy a n dREG_SGCNTys t r u c t u r e( x=1 , 2 , 3o r4a n dy=0o r1 ) .It h i n kt h ef o r me ri st h en e we rv e r s i o n , wh i c hi sf u n n yb e c a u s et h eo l d e ri s mo r ec o n s i s t e n t .Ohwe l l .I na n yc a s e , If i n dn e i t h e ro ft h e mv e r yd e s c r i p t i v ea n dk e e pf o r g e t t i n gwh i c ho ft h eL/ H/ Xo r0 / 1v e r s i o n s d o e swh a t , s oIu s eathird s e to fn a me sb a s e do nt h eo n e sf o u n di nt e p p l e s ' pin8gba.h, wh i c hI MHOma k e smo r es e n s et h a nt h e o t h e rt wo . offset function old new 60h c h a n n e l 1( s q r )s we e p 62h c h a n n e l 1( s q r )l e n , d u t y , e n v 64h c h a n n e l 1( s q r )f r e q , o n 68h c h a n n e l 2( s q r )l e n , d u t y , e n v REG_SG2 0 SOUND1 CNT_L REG_SND1 SWEEP REG_SG1 0 SOUND1 CNT_H REG_SND1 CNT REG_SG1 1 h a n n e l 2( s q r )f r e q , o n 6Ch c SOUND1 CNT_X REG_SND1 FREQ SOUND2 CNT_L REG_SND2 CNT REG_SG2 1 70h c h a n n e l 3( wa v e )mo d e 72h c h a n n e l 3( wa v e )l e n , v o l 74h c h a n n e l 3( wa v e )f r e q , o n 78h c h a n n e l 4( n o i s e )l e n , v o l , e n vREG_SG4 0 SOUND2 CNT_H REG_SND1 FREQ SOUND3 CNT_L REG_SND3 SEL REG_SG3 0 SOUND3 CNT_H REG_SND3 CNT REG_SG3 1 h a n n e l 4( n o i s e )f r e q , o n 7Ch c tonc SOUND3 CNT_X REG_SND3 FREQ SOUND4 CNT_L REG_SND4 CNT REG_SG4 1 SOUND4 CNT_H REG_SND4 FREQ 80h DMGma s t e rc o n t r o l 82h DSo u n dma s t e rc o n t r o l SOUNDCNT_L REG_SNDDMGCNT REG_SGCNT0 SOUNDCNT_H REG_SNDDSCNT 84h s o u n ds t a t u s REG_SGCNT1SOUNDCNT_X REG_SNDSTAT 88h b i a sc o n t r o l REG_SGBI AS SOUNDBI AS REG_SNDBI AS Table 18.2: S o u n dr e g i s t e rn o me n c l a t u r e . “ Ohg r e a t .Th i si sg o i n gt ob eo n eo f‘ t e g e l ’t h i n g si s n ' ti t ?Wh e r eyou t h i n ky o u ' v eg o ts o me t h i n gn i c eb u td i f f e r e n tg o i n g , t h e nl a t e ry o u r e v e r tt ot h es t a n d a r dt e r mi n o l o g yt oc o n f o r mwi t ht h er e s to ft h ewo r l d .Ri g h t ? ” No , I ' l l s t i c kt ot h e s en a me s .Pr o b a b l y .Ho p e f u l l y .… Tob eh o n e s t , Ir e a l l yd o n ' tk n o w:P.Th i si sn o tr e a l l yab i gd e a l , t h o u g h : y o u c a ne a s i l ys wi t c hb e t we e nn a me swi t haf e wd e f i n e so rs e a r c h& r e p l a c e s .An y wa y , REG_SNDxFREQc o n t a i n sf r e q u e n c yi n f o r ma t i o n a n dREG_SNDxCNTt h i n g sl i k ev o l u mea n de n v e l o p es e t t i n g s ;i ns o mec a s e s , t h eb i tl a y o u t sa r ee v e ne x a c t l yt h es a me .Ap a r tf r o mt h e s we e pf u n c t i o no fc h a n n e l 1 , i ti se x a c t l yt h es a mea sc h a n n e l 2 . 18.3.2. Master sound registers REG_SNDDMGCNT, REG_SNDDSCNTa n dREG_SNDSTATa r et h ema s t e rs o u n dc o n t r o l s ;y o uh a v et os e ta tl e a s ts o meb i t so ne a c h o ft h e s et og e ta n y t h i n gt owo r k . REG_SNDDMGCNT (SOUNDCNT_L / SGCNT0_L ) @ 0400:0080h F E D C B A 9 8 7 654 3 210 R4 R3 R2 R1 L4 L3 L2 L1 - bits name define RV - LV description 0 2 LV Le f tv o l u me 4 6 RV Ri g h tv o l u me 8 B L1 L4SDMG_LSQR1 , Ch a n n e l s1 4o nl e f t SDMG_LSQR2 , SDMG_LWAVE, SDMG_LNOI SE CF R1 - SDMG_RSQR1 , Ch a n n e l s1 4o nr i g h t R4 SDMG_RSQR2 , SDMG_RWAVE, SDMG_RNOI SE REG_SNDDMGCNTc o n t r o l st h ema i nv o l u meo ft h eDMGc h a n n e l sa n dwh i c ho n e sa r ee n a b l e d .Th e s ec o n t r o l sa r es e p a r a t ef o rt h e l e f ta n dr i g h ts p e a k e r s .Be l o wa r et woma c r o st h a tma k ema n i p u l a t i n gt h er e g i s t e re a s i e r .No t et h a tt h e ydon't a c t u a l l ys e tt h er e g i s t e r , j u s tc o mb i n et h ef l a g s . #define SDMG_SQR1 #define SDMG_SQR2 0x01 0x02 file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 157/331 28-03-13 Tonc : GBA Programming in rot13 #define SDMG_WAVE #define SDMG_NOISE 0x04 0x08 #define SDMG_BUILD(_lmode, _rmode, _lvol, _rvol) \ ( ((_lvol)&7) | (((_rvol)&7)<<4) | ((_lmode)<<8) | ((_rmode)<<12) ) #define SDMG_BUILD_LR(_mode, _vol) SDMG_BUILD(_mode, _mode, _vol, _vol) REG_SNDDSCNT (SOUNDCNT_H / SGCNT0_H) @ 0400:0082h F E D C B A 9 8 7654 3 BF BT BL BR AF AT AL AR define - 2 10 BV AV DMGV bits name 0 1 DMGV SDS_DMG2 5 , SDS_DMG5 0 , SDS_DMG1 0 0 description DMGVo l u mer a t i o . 2 AV SDS_A5 0 , SDS_A1 0 0 DSo u n dAv o l u mer a t i o .5 0 %i fc l e a r ;1 0 0 %o fs e t 3 BV SDS_B5 0 , SDS_B1 0 0 DSo u n dBv o l u mer a t i o .5 0 %i fc l e a r ;1 0 0 %o fs e t 8 9 AR, AL SDS_AR, SDS_AL DSound A enable En a b l eDSAo nr i g h ta n dl e f ts p e a k e r s A AT SDS_ATMR0 , SDS_ATMR1 et i me r0( i fc l e a r )o r1( i fs e t )f o rDSA Dsound A timer.Us B AF SDS_ARESET e nu s i n gDMAf o rDi r e c ts o u n d , t h i swi l l c a u s e FIFO reset for Dsound A.Wh DMAt or e s e tt h eFI FOb u f f e ra f t e ri t ' su s e d . 00: 2 5 % 01: 5 0 % 10: 1 0 0 % 11: f o r b i d d e n CF BR, BL,SDS_BR, SDS_BL,Asb i t s8 B, b u tf o rDSo u n dB BT, BF SDS_BTMR0 , SDS_BTMR1 , SDS_BRESET Do n ' tk n o wt o omu c ha b o u tREG_SNDDSCNT, a p a r tf r o mt h a ti tg o v e r n sDi r e c t So u n d , b u ta l s oh a ss o meDMGs o u n db i t sf o rs o me r e a s o n .REG_SNDSTATs h o wst h es t a t u so ft h eDMGc h a n n e l sand e n a b l e sa l l s o u n d .I fy o uwa n tt oh a v ea n ys o u n da ta l l , y o un e e d t os e tb i t7t h e r e . REG_SNDSTAT (SOUNDCNT_X / SGCNT1) @ 0400:0084h 0 3 1 A4 A SSTAT_SQR1 , SSTAT_SQR2 , SSTAT_WAVE, SSTAT_NOI SE 7 MSE MSE define - name 7 bits FEDCBA98 654 3 - 2 1 0 4A 3A 2A 1A description n d i c a t e swh i c hDMAc h a n n e l sa r ec u r r e n t l yp l a y i n g .Th e yd o Active channels .I not e n a b l et h ec h a n n e l s ;t h a t ' swh a tREG_SNDDMGCNTi sf o r . SSTAT_DI SABLE,Master Sound Enable .Mu s tb es e ti fa n ys o u n di st ob eh e a r da ta l l .Se tt h i s SSTAT_ENABLE before y o ud oa n y t h i n ge l s e : t h eo t h e rr e g i s t e r sc a n ' tb ea c c e s s e do t h e r wi s e , s e e GBATe kf o rd e t a i l s . Sound register access Emu l a t o r sma ya l l o wa c c e s st os o u n dr e g i s t e r se v e ni fs o u n di sd i s a b l e d( REG_SNDSTAT{ 7 }i sc l e a r ) , b u th a r d wa r ed o e s n ' t . Al wa y se n a b l es o u n db e f o r eu s e . 18.3.3. GBA Square wave generators Th eGBAh a st wos q u a r es o u n dg e n e r a t o r s , c h a n n e l s1a n d2 .Th eo n l yd i f f e r e n c eb e t we e nt h e mi sc h a n n e l 1 ' sfrequency sweep, wh i c hc a nma k et h ef r e q u e n c yr i s eo rd r o pe x p o n e n t i a l l ya si t ' sp l a y e d .Th a t ' sa l l d o n ewi t hREG_SND1SWEEP.REG_SNDxCNT c o n t r o l st h ewa v e ' sl e n g t h , e n v e l o p ea n dd u t yc y c l e .Le n g t hs h o u l db eo b v i o u s .Th eenvelope i sb a s i c a l l yt h ea mp l i t u d ea sf u n c t i o no f t i me : y o uc a nma k ei tf a d ei n( attack) , r e ma i na tt h es a mel e v e l ( sustain)a n df a d eo u ta g a i n( decay) .Th ee n v e l o p eh a s1 6v o l u me file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 158/331 28-03-13 Tonc : GBA Programming in rot13 l e v e l sa n dy o uc a nc o n t r o l t h es t a r t i n gv o l u me , d i r e c t i o no ft h ee n v e l o p ea n dt h et i met i l l t h en e x tc h a n g e .Th eduty r e f e r st ot h er a t i oo f t h e‘ o n ’t i mea n dt h ep e r i o d , i no t h e rwo r d sD =h/T. Ofc o u r s e , y o uc a nc o n t r o l t h ef r e q u e n c ya swe l l , n a me l ywi t hREG_SNDxFREQ.Ho we v e r , i ti s n ' tt h ef r e q u e n c yt h a ty o ue n t e ri n t h i sf i e l d .I t ' sn o te x a c t l yt h ep e r i o de i t h e r ;i t ' ss o me t h i n gI ' l l r e f e rt oa st h erate R.Th et h r e eq u a n t i t i e sa r er e l a t e d , b u td i f f e r e n ti ns u b t l e wa y sa n dc h a o se n s u e swh e nt h e y ' r ec o n f u s e d–a n dt h e yo f t e nare i nd o c u me n t a t i o n , s ob ec a r e f u l .Th er e l a t i o nb e t we e nf r e q u e n c yf a n dr a t eR i sd e s c r i b e db ye q1 8 . 6 ;i ft h er a t eg o e su p , s od o e st h ef r e q u e n c y .Si n c eR ∈ [ 0 , 2 0 4 7 ] , t h er a n g eo ff r e q u e n c i e si s[ 6 4 Hz , 1 3 1k Hz ] .Wh i l et h i ss p a n st e no c t a v e s , t h eh i g h e s to n e sa r e n ' to fmu c hu s eb e c a u s et h ef r e q u e n c ys t e p sb e c o met o ol a r g e( t h e d e n o mi n a t o ri ne q1 8 . 6a p p r o a c h e s0 ) . (18.6a) f( R)= 217 2048−R (18.6b) R( f)= 2048−217/ f 18.3.4. Square sound registers Bo t hs q u a r e wa v eg e n e r a t o r sh a v er e g i s t e r sREG_SNDxCNTf o re v e l o p e / l e n g t h / d u t yc o n t r o l a n dREG_SNDxFREQf o rf r e q u e n c y c o n t r o l .So u n d1a l s oh a ss we e pc o n t r o l i nt h ef o r mo fREG_SND1SWEEP. Lo o ki nt a b l e1 8 . 2f o rt h et r a d i t i o n a l n a me s ;n o t et h a ti n t r a d i t i o n a l n o me n c l a t u r et h es u f f i x e sf o rc o n t r o l a n df r e q u e n c ya r edifferent f o rc h a n n e l s1a n d2 , e v e nt h o u g ht h e yh a v ee x a c t l yt h e s a mef u n c t i o n . REG_SND1CNT (SOUND1CNT_H / SG10_H) @ 0400:0062h and REG_SND2CNT (SOUND2CNT_L / SG20_L) @ 0400:0068h FEDC B A98 76 543210 EIV ED EST D L bits name define description 0 5 L SSQR_LEN# So u n dLength.Th i si sawrite-only f i e l da n do n l ywo r k si ft h ec h a n n e l i st i me d ( REG_SNDxFREQ{E}) .Th el e n g t hi t s e l fi sa c t u a l l y( 6 4 −L) / 2 5 6s e c o n d sf o ra[ 3 . 9 , 2 5 0 ]msr a n g e . 6 7 D SSQR_DUTY1 _8 , Wa v eduty cycle .Ra t i ob e t we e no na n do ft i me so ft h es q u a r ewa v e .Lo o k i n gb a c k SSQR_DUTY1 _4 , a te q1 8 . 2 , t h i sc o me sd o wnt oD=h/T.Th ea v a i l a b l ec y c l e sa r e1 2 . 5 %, 2 5 %, 5 0 %, SSQR_DUTY1 _2 , a n d7 5 %( o n ee i g h t h , q u a r t e r , h a l fa n dt h r e eq u a r t e r s ) . SSQR_DUTY3 _4 , SSQR_DUTY# 8 A EST SSQR_TI ME# En v e l o p estep-time .Ti meb e t we e ne n v e l o p ec h a n g e s : Δt=EST/ 6 4s . B ED SSQR_DEC, SSQR_I NC En v e l o p edirection.I n d i c a t e si ft h ee n v e l o p ed e c r e a s e s( d e f a u l t )o ri n c r e a s e swi t h e a c hs t e p . SSQR_I VOL# En v e l o p einitial value .Ca nb ec o n s i d e r e davolume s e t t i n go fs o r t s : 0i ss i l e n ta n d 1 5i sf u l l v o l u me .Co mb i n e dwi t ht h ed i r e c t i o n , y o uc a nh a v ef a d e i na n df a d e o u t s ; t oh a v eas u s t a i n i n gs o u n d , s e ti n i t i a l v o l u met o1 5a n da ni n c r e a s i n gd i r e c t i o n .To v a r yt h ereal v o l u me , r e me mb e rREG_SNDDMGCNT. CF EI V 2s i n ( πD m) Am = · π m So memo r eo nt h ed u t yc y c l e .Re me mb e rwe ' v ed o n eaFo u r i e ra n a l y s i so ft h e s q u a r ewa v es owec o u l dd e t e r mi n et h ef r e q u e n c i e si ni t .Ap a r tf r o mt h ebase frequency, t h e r ea r ea l s oovertones o ff r e q u e n c i e sm·f.Th es p e c t r u m( s e e f i g1 8 . 3 )g i v e st h ea mp l i t u d e so fa l l t h e s ef r e q u e n c i e s .No t et h a te v e nt h o u g h t h ef i g u r eh a sl i n e s , o n l yi n t e g r a l v a l u e so fm a r ea l l o we d .Th eb a s ef r e q u e n c y a tm=1h a st h eh i g h e s ts i g n i f i c a n c ea n dt h er e s tf a l l so f fwi t h1 / m.Th e i n t e r e s t i n gp a r ti swh e nt h es i n ec o me si n t op l a y : wh e n e v e rm·D i sa ni n t e g e r , t h a tc o mp o n e n tv a n i s h e s !Wi t haf r a c t i o n a l d u t yn u mb e r–l i k et h eo n e swe h a v e –t h i sh a p p e n se v e r yt i mem i se q u a l t ot h ed e n o mi n a t o r .Fo rt h e5 0 %d u t y , Fig 18.3: S q u a r ewa v es pe c t r u m. ( i n t e g e rm o n l y) e v e r ys e c o n do v e r t o n ed i s a p p e a r s , l e a v i n gaf a i r l ys mo o t ht o n e ;f o r1 2 . 5 %, o n l y e v e r ye i g h t hv a n i s h e sa n dt h er e s u l ti si n d e e dan o i s i e rs o u n d .No t et h a tf o r both ¼a n d¾d u t i e se v e r yf o u r t hv a n i s h e ss ot h a tt h e ys h o u l db ei n d i s t i n g u i s h a b l e .Iwa sal i t t l es u r p r i s e da b o u tt h i sr e s u l t , b u ts u r e e n o u g h , wh e nIc h e c k e dt h e yr e a l l yd i ds o u n dt h es a met ome . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 159/331 28-03-13 Tonc : GBA Programming in rot13 REG_SND1FREQ (SOUND1CNT_X / SG11) @ 0400:0062h and REG_SND2FREQ (SOUND2CNT_H / SG21) @ 0400:006Ch F E DCB A9876543210 Re T - R bits name define description 0 A R SFREQ_RATE#So u n drate .We l l , i n i t i a l r a t e .Th a t ' srate, n o tf r e q u e n c y .No rp e r i o d .Th er e l a t i o n 1 7 b e t we e nr a t ea n df r e q u e n c yi sf =2 . Wr i t e o n l y f i e l d . / ( 2 0 4 8 R) E T SFREQ_HOLD,Timed f l a g .I fs e t , t h es o u n dp l a y sf o ra sl o n ga st h el e n g t hf i e l d( 0 REG_SNDxCNT{ SFREQ_TI MED5 } )i n d i c a t e s .I fc l e a r , t h es o u n dp l a y sf o r e v e r .No t et h a te v e ni fad e c a y i n ge n v e l o p e h a sr e a c h e d0 , t h es o u n di t s e l fwo u l ds t i l l b ec o n s i d e r e do n , e v e ni fi t ' ss i l e n t . F Re SFREQ_RESETSo u n dreset.Re s e t st h es o u n dt ot h ei n i t i a l v o l u me( a n ds we e p )s e t t i n g s .Re me mb e r t h a tt h er a t ef i e l di si nt h i sr e g i s t e ra swe l l a n dd u et oi t swr i t e o n l yn a t u r eas i mp l e‘ |= SFREQ_RESET’wi l l not s u f f i c e( e v e nt h o u g hi tmi g h to ne mu l a t o r s ) . REG_SND1SWEEP (SOUND1CNT_L / SG10_L) @ 0400:0060h FEDCBA987 654 3 210 - T M N bits name define description 0 2 N SSW_SHI FT#Swe e pnumber.Not t h en u mb e ro fs we e p s ;s e et h ed i s c u s s i o nb e l o w. 3 M SSW_I NC, Swe e pmode .Th es we e pc a nt a k et h er a t ee i t h e ru p( d e f a u l t )o rd o wn( i fs e t ) . SSW_DEC 4 6 T SSW_TI ME# Swe e pstep-time .Th et i meb e t we e ns we e p si sme a s u r e di n1 2 8Hz( n o tk Hz ! ) : Δt= T/ 1 2 8ms≈7 . 8 T ms ;i fT=0 , t h es we e pi sd i s a b l e d . I ' mr e a s o n a b l yc o n f i d e n tt h a tt h eexact wo r k i n g so fs h i f t sa r ee x p l a i n e dwi t h o u td u ec a r ei nmo s td o c u me n t s , s oh e r ea r eaf e wmo r e t h i n g sa b o u ti t .Su r ee n o u g h , t h es we e pdoes ma k et h ep i t c hg ou po rd o wnwh i c hi sc o n t r o l l e db yb i t3 , a n dt h es t e p t i medoes c h a n g e t h ep i t c ha f t e rt h a tt i me , b u te x a c t l ywh a tt h es we e p s h i f td o e si sa mb i g u o u sa tb e s t .Th ei n f o r ma t i o ni si nt h e r e , b u to n l yi fy o uk n o w wh a tt ol o o kf o r .Th eu s u a l f o r mu l ag i v e ni ss o me t h i n gl i k e : T = T ±T· 2−n Th a t ' swh a tb e l o g i cg i v e sa n di fy o uk n o wwh a tt h et e r msa r ey o u ' l l b ef i n e .Co n t r a r yt owh a ty o uma yr e a d , t h es we e pd o e snot a p p l y t ot h ef r e q u e n c y( f) .I td o e snot a p p l yt ot h ep e r i o d( T, s e ea b o v e ) .I ta p p l i e st ot h erate ( R) .I fy o ul o o ki ne mu l a t o r s , y o uc a na c t u a l l y see t h er a t e v a l u ec h a n g e . Se c o n d , t h en i nt h ee x p o n e n ti snot t h ec u r r e n ts we e pi n d e xt h a tr u n su pt ot h en u mb e ro fs we e ps h i f t s .I ti si nf a c ts i mp l yt h e sweep shift number, a n dt h es we e p sc o n t i n u eu n t i l t h er a t er e a c h e s0o rt h ema x i mu mo f2 0 4 7 . Th ef o r mu l a sy o uma ys e ed os a yt h a t , b u ti t ' se a s yt omi s r e a dt h e m.Id i d .Eq1 8 . 7h o l d san u mb e ro fc o r r e c tr e l a t i o n s .R i st h er a t e , ni st h es we e ps h i f t( 1 8 . 7 ce x p l a i n swh yi t ' sc a l l e dashift ( s i n g u l a r , n o tp l u r a l ) ) , a n dj i st h ec u r r e n ts we e pi n d e x .Yo uc a nv i e wt h e mi n an u mb e ro fwa y s , b u tt h e ya l l b o i l d o wnt oe x p o n e n t i a l f u n c t i o n s , t h a t ' swh a t‘ d y( x)=a·y( x) d x’me a n s , a f t e ra l l .Fo re x a mp l e , i fn=1 , je t h e ny o ug e t1 ½ja n d½jb e h a v i o u rf o ri n c r e a s i n ga n dd e c r e a s i n gs we e p s , r e s p e c t i v e l y ;wi t hn=2i t ' s1 ¼ja n d¾, t c .Th eh i g h e rt h es h i f t , t h es l o we rt h es we e p . −n (18.7a) ΔR = 2 · R Rj= Rj ±Rj · 2-n −1 −1 = Rj ( 1±2−n) (18.7b) −1 j = R0( 1±2−n) (18.7c) R += R>>n; 18.3.5. Playing notes Ev e nt h o u g ht h er a t e sa r ee q u a l , s o mema yb ec o n s i d e r e dmo r ee q u a l t h a no t h e r s .I ' v ea l r e a d yg i v e nat a b l ewi t ht h ef r e q u e n c i e sf o rt h e s t a n d a r dn o t e s( t a b l e1 8 . 1o fo c t a v e0 .Yo uc a no fc o u r s ec o n v e r tt h o s et or a t e sv i ae q1 8 . 6 ba n du s et h e ma ss u c h .Ho we v e r , i tmi g h t p a yt of i g u r eo u th o wt op l a yt h en o t e so fall o c t a v e s . Tod ot h i s , we ' l l u s es o mef a c t sIme n t i o n e di ns e c t i o n1 8 . 2 . 3 .a b o u tt h ema k e u po ft h emu s i c a l s c a l e .Wh i l eIcould ma k eu s eo f file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 1 / 12 160/331 28-03-13 Tonc : GBA Programming in rot13 1 / 12 t h el o g a r i t h mi cr e l a t i o nb e t we e ns u c c e s s i v en o t e s( Δf=2 · f) , I ' l l r e s t r i c tmy s e l ft ot h ef a c tt h a tn o t e sb e t we e no c t a v e sd i f f e rb ya f a c t o ro ft wo .We ' l l a l s on e e dt h er a t e f r e q u e n c yr e l a t i o n( o b v i o u s l y ) .Th a t ' st h eb a s i ci n f o r ma t i o ny o un e e d , I ' l l e x p l a i nmo r eo n c ewe g e tt h r o u g ha l l t h ema t h .Ye s , i t ' smo r ema t h , b u ti t ' l l b et h el a s to ft h i sp a g e , Ip r o mi s e . Th ee q u a t i o n swe ' l l s t a r twi t ha r et h eg e n e r a l f r e q u e n c ye q u a t i o na n dt h er a t e f r e q u e n c yr e l a t i o n .I nt h e s eweh a v er a t eR, f r e q u e n c yf a n do c t a v ec.Wea l s oh a v eab a s eo c t a v eC a n df r e q u e n c yF i nt h a tb a s eo c t a v e . f( F, c) = F· 2c−C R( F, c)= 211−217/ f( F, c) An dn o wf o rt h ema g i c .An dy o uare e x p e c t e dt ou n d e r s t a n dt h i s . R( F, c)= 211−217/ f( F, c) = 211−217/ ( F· 2c−C) = 211−217+C−c/ F = 211−1/ F ·217+C+m−(c+m) (18.8) = 211−{217+C+m/ F }·2−(c+m) Ri g h t , a n dn o wf o rwhy t h i st h i n g ' su s e f u l .Re me mb e rt h a tt h eGBAh a sn oh a r d wa r ed i v i s i o no rf l o a t i n g p o i n ts u p p o r t , s owe ' r el e f twi t h i n t e g e r sa n d( i fp o s s i b l e )s h i f t s .Th a t ' swh yt h el a s tt e r mi nt h el a s ts t e po fe q1 8 . 8wa ss e p a r a t e d .Th et e r mwi t hF g i v e sar a t eo f f s e t f o rt h eb a s eo c t a v e , wh i c hwen e e dt od i v i d e( r e a d : s h i f t )b yt h eo c t a v eo f f s e tt e r mf o rt h ed i f f e r e n to c t a v e s .Re me mb e rt h a ti n t e g e r d i v i s i o nt r u n c a t e s , s owen e e dab i gn u me r a t o rf o rt h emo s ta c c u r a c y .Th i sc a nb ed o n ewi t hal a r g eC a n db ya d d i n ga ne x t r at e r mm. Ba s e i c a l l y , t h i sma k e si ta nmff i x e dp o i n td i v i s i o n .Th ewo r k a b l eo c t a v er a n g ei s−2t o5 , s owet a k eC=5 .Th ev a l u ef o rm i salmost a r b i t r a r y , b u tn e e d st ob eh i g h e rt h a nt wob e c a u s eo ft h emi n i mu mo c t a v ei s−2 , a n das h i f tc a nn e v e rb en e g a t i v e .m=4wi l l s u f f i c e . No t et h a tt h e r ei sstill ad i v i s i o ni nt h e r e .Fo r t u n a t e l y , t h e r ea r eo n l yt we l v ev a l u e sa v a i l a b l ef o rF, s omi g h tj u s ta swe l l s t o r et h e wh o l et e r mi nal o o k u pt a b l e .Th ef i n a l r e s u l ti sl i s t i n g1 8 . 1b e l o w. // Listing 18.1: a sound-rate macro and friends typedef enum { NOTE_C=0, NOTE_CIS, NOTE_D, NOTE_DIS, NOTE_E, NOTE_F, NOTE_FIS, NOTE_G, NOTE_GIS, NOTE_A, NOTE_BES, NOTE_B } eSndNoteId; // Rates for traditional notes in octave +5 const u32 __snd_rates[12]= { 8013, 7566, 7144, 6742, // C , C#, D , D# 6362, 6005, 5666, 5346, // E , F , F#, G 5048, 4766, 4499, 4246 // G#, A , A#, B }; #define SND_RATE(note, oct) ( 2048-(__snd_rates[note]>>(4+(oct))) ) // sample use: note A, octave 0 REG_SND1FREQ= SFREQ_RESET | SND_RATE(NOTE_A, 0); He r ey o uh a v eac o u p l eo fc o n s t a n t sf o rt h en o t e i n d i c e s , t h eLUTwi t hr a t e o f f s e t s__snd_ratesa n das i mp l ema c r ot h a tg i v e sy o u wh a ty o uwa n t .Wh i l e__snd_ratesi sc o n s t a n th e r e , y o uma yc o n s i d e ran o n c o n s tv e r s i o nt oa l l o wt u n i n g .No tt h a tas q u a r ewa v e i sa n y t h i n gwo r t ht u n i n g , b u tI ' mj u s ts a y i n g… y ' k n o w. On ep o s s i b l ea n n o y a n c ei st h a ty o uh a v et os p l i c et h en o t ei n t oan o t ea n do c t a v ep a r ta n dt od ot h a td y n a mi c a l l yy o u ' dn e e dd i v i s i o n a n dmo d u l ob y1 2 .Ord oy o u ?I fy o uk n e waf e wt h i n g sa b o u td i v i s i o nb yac o n s t a n ti smu l t i p l i c a t i o nb yi t sr e c i p r o c a l , y o u ' dk n o wwh a t t od o .( Hi n t : c=( N* 4 3 >>9 ) −2 , wi t hN t h et o t a l n o t ei n d e xb e t we e n0a n d9 5( o c t a v e−2t o+5 ) . ) 18.4. Demo time It h i n kI ' v ed o n ea b o u te n o u g ht h e o r yf o rt o d a y , d o n ' ty o ud e a rr e a d e r ? “@_@ ” file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 161/331 28-03-13 Tonc : GBA Programming in rot13 I ' l l t a k et h a ta say e s .Th ed e moi nq u e s t i o nd e mo n s t r a t e st h eu s eo ft h ev a r i o u sma c r o so ft h i sc h a p t e r , mo s tn o t a b l ySND_RATE.I t a l s os h o wsh o wy o uc a np l a yal i t t l es o n g–a n dIu s et h et e r ml i g h t l y–wi t ht h es q u a r ewa v eg e n e r a t o r .Ih o p ey o uc a nr e c o g n i z ewh i c h o n e . #include <stdio.h> #include <tonc.h> u8 txt_scrolly= 8; const char *names[]= { "C ", "C#", "D ", "D#", "E ", "F ", "F#", "G ", "G#", "A ", "A#", "B " }; // === FUNCTIONS ====================================================== // Show the octave the next note will be in void note_prep(int octave) { char str[32]; siprintf(str, "[ %+2d]", octave); se_puts(8, txt_scrolly, str, 0x1000); } // Play a note and show which one was played void note_play(int note, int octave) { char str[32]; // Clear next top and current rows SBB_CLEAR_ROW(31, (txt_scrolly/8-2)&31); SBB_CLEAR_ROW(31, txt_scrolly/8); // Display note and scroll siprintf(str, "%02s%+2d", names[note], octave); se_puts(16, txt_scrolly, str, 0); txt_scrolly -= 8; REG_BG0VOFS= txt_scrolly-8; // Play the actual note REG_SND1FREQ = SFREQ_RESET | SND_RATE(note, octave); } // Play a little ditty void sos() { const u8 lens[6]= { 1,1,4, 1,1,4 }; const u8 notes[6]= { 0x02, 0x05, 0x12, 0x02, 0x05, 0x12 }; int ii; for(ii=0; ii<6; ii++) { note_play(notes[ii]&15, notes[ii]>>4); VBlankIntrDelay(8*lens[ii]); } } int main() { REG_DISPCNT= DCNT_MODE0 | DCNT_BG0; irq_init(NULL); irq_add(II_VBLANK, NULL); txt_init_std(); txt_init_se(0, BG_CBB(0) | BG_SBB(31), 0, CLR_ORANGE, 0); pal_bg_mem[0x11]= CLR_GREEN; int octave= 0; // turn sound on file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 162/331 28-03-13 Tonc : GBA Programming in rot13 REG_SNDSTAT= SSTAT_ENABLE; // snd1 on left/right ; both full volume REG_SNDDMGCNT = SDMG_BUILD_LR(SDMG_SQR1, 7); // DMG ratio to 100% REG_SNDDSCNT= SDS_DMG100; // no sweep REG_SND1SWEEP= SSW_OFF; // envelope: vol=12, decay, max step time (7) ; 50% duty REG_SND1CNT= SSQR_ENV_BUILD(12, 0, 7) | SSQR_DUTY1_2; REG_SND1FREQ= 0; sos(); while(1) { VBlankIntrWait(); key_poll(); // Change octave: octave += bit_tribool(key_hit(-1), KI_R, KI_L); octave= wrap(octave, -2, 6); note_prep(octave); // Play note if(key_hit(KEY_DIR|KEY_A)) { if(key_hit(KEY_UP)) note_play(NOTE_D, octave+1); if(key_hit(KEY_LEFT)) note_play(NOTE_B, octave); if(key_hit(KEY_RIGHT)) note_play(NOTE_A, octave); if(key_hit(KEY_DOWN)) note_play(NOTE_F, octave); if(key_hit(KEY_A)) note_play(NOTE_D, octave); } // Play ditty if(key_hit(KEY_B)) sos(); } return 0; } Th eb o l d e dc o d ei nmain()i n i t i a l i z e st h es o u n dr e g i s t e r ;n o t h i n gf a n c y , b u ti th a st ob ed o n eb e f o r ey o uh e a ra n y t h i n ga ta l l .I ti s i mp o r t a n tt os t a r twi t hREG_SNDSTATb i t7( SSTAT_ENABLE) , i . e . , t h ema s t e rs o u n de n a b l e .Wi t h o u ti t , y o uc a n n o te v e na c c e s st h e o t h e rr e g i s t e r s .Se t t i n gv o l u met os o me t h i n gn o n z e r oi sag o o di d e at o o , o fc o u r s e .Th e nwet u r no f ft h es we e pf u n c t i o na n ds e ts o u n d1 t ou s eaf a d i n ge n v e l o p ewi t ha5 0 %d u t y .An dt h a t ' swh e r et h ef u ns t a r t s . I ' l l e x p l a i nwh a tsos()i nal i t t l ewh i l e , f i r s ts o me t h i n ga b o u tt h ec o n t r o l so ft h ed e mo .Yo uc a np l a yn o t e swi t ht h eDp a da n dA ( h mm, t h e r e ' ss o me t h i n gf a mi l i a ra b o u tt h a ta r r a n g e me n t ) .Th eo c t a v ec y o u ' r ewo r k i n gi nc a nb ec h a n g e dwi t hLa n dR;t h e b a c k g r o u n dc o l o rc h a n g e swi t hi t .Bp l a y ssos()a g a i n . a yan o t e A / D-pad Pl ↑: D( n e x to c t a v e ) ←: B →: A ↓: F A: D L/R B De c r e a s e/I n c r e a s ec u r r e n to c t a v e( [ 2 , 5 ] , wr a p sa r o u n d ) Pl a yal i t t l et u n e . Th eDp a da n dAs e l e c tan o t et op l a y , wh i c hi sh a n d l e db ynote_play().Th eb o l d e dl i n et h e r ep l a y st h ea c t u a l n o t e , t h er e s ti s e x t r as t u f ft h a twr i t e st h en o t ej u s tp l a y e dt ot h es c r e e na n ds c r o l l sa l o n gs oy o uc a ns e et h eh i s t o r yo fwh a t ' sb e e np l a y e d .Th ec o d ef o r t h i si sk i n d au g l y , b u ti sn o te x a c t l yc e n t r a l t ot h es t o r ys ot h a t ' sf i n e . 18.4.1. Playing a little ditty file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 163/331 28-03-13 Tonc : GBA Programming in rot13 Sowh a ti ssos()a l l a b o u tt h e n ?Le t ' st a k ea n o t h e rl o o k . void sos() { const u8 lens[6]= { 1,1,4, 1,1,4 }; const u8 notes[6]= { 0x02, 0x05, 0x12, 0x02, 0x05, 0x12 }; int ii; for(ii=0; ii<6; ii++) { note_play(notes[ii]&15, notes[ii]>>4); VBlankIntrDelay(8*lens[ii]); } } Th e r ea r et woa r r a y sh e r e , notesa n dlens, a n dal o o po v e ra l l e l e me n t s .Wet a k eab y t ef r o mnotesa n du s et h en y b b l e sf o r o c t a v ea n dn o t ei n f o r ma t i o n , p l a yt h en o t e , t h e nwa i tawh i l e–t h el e n g t hi si n d i c a t e db yt h elensa r r a y –b e f o r et h en e x tn o t ei sp l a y e d . Ba s i c a l l y , we ' r ep l a y i n gmu s i c .He y , i ft h el i k e so fSchnappi a n dCrazy Frog c a nma k ei ti n t ot h et o p1 0 , It h i n kI ' ma l l o we dt oc a l l this mu s i ct o o , a l r i g h t ?Al r i g h t . Th ep o i n tI ' mt r y i n gt oma k ei st h a ti t ' sv e r ywe l l p o s s i b l et op l a yat u n ewi t hj u s tt h et o n eg e n e r a t o r s , t e c h n i c a l l yy o ud o n ' tn e e d d i g i t i z e dmu s i ca n da l l t h a ts t u f ft op l a ys o me t h i n g .Ofc o u r s e , i t ' l l s o u n db e t t e ri fy o ud o , b u ti fy o uj u s tn e e dal i t t l ej i n g l et h et o n e g e n e r a t o r sma yb ea l l y o un e e d .J u s td e f i n es o men o t e s( t h en y b b l ef o r ma tf o ro c t a v e sa n dn o t e swi l l d o )a n ds o mel e n g t h sa n dy o uh a v e t h eb a s i c sa l r e a d y .Yo uc o u l de v e nu s emo r et h a no n ec h a n n e l f o rd i f f e r e n te f f e c t s . I fy o uu n d e r s t o o dt h a t , t h e ng e tt h i s : t h en o t e +l e n g t h +c h a n n e l i d e ai sp r e t t ymu c hwh a tt r a c k e dmu s i c( mo d , i t , x m, e t c )d o e s , o n l y t h e yu s eamo r es o p h i s t i c a t e dwa v et h a nas q u a r ewa v e .Bu tt h ep r i n c i p l ei st h es a me .Ge t t i n gi tt owo r kt a k e sal i t t l emo r ee f f o r t , b u t t h a t ' swh a tDe k u ' ss o u n dmi xt u t o r i a l i sf o r . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 164/331 28-03-13 Tonc : GBA Programming in rot13 19. Text systems I n t r o d u c t i o n Te x ts y s t e mi n t e r n a l s Bi t ma pt e x t Ti l e ma pt e x t Sp r i t et e x t So med e mo s Ot h e rc o n s i d e r a t i o n s Deprecation notice Th i sc h a p t e rh a sb e e ns u p e r c e d e db yTTE.I n f o r ma t i o nf r o mt h i sc h a p t e rc a ns t i l l b eu s e f u l , b u tf o rs e r i o u swo r k , TTEs h o u l d b ep r e f e r r e d . 19.1. Introduction #include <stdio.h> int main() { printf("Hello World"); return 0; } Aa a h , y e s , “ He l l owo r l d ” : t h ec a n o n i c a l f i r s te x a mp l ef o re v e r yCc o u r s ea n ds y s t e m.Ex c e p tf o rc o n s o l e s .Wh i l ep r i n t i n gt e x to naPCi s t h ee a s i e s tt h i n gi nt h ewo r l d , i ti sa c t u a l l yal i t t l et r i c k yo nac o n s o l e .I t ' sn o tt h a tt h e r e ' sn oprintf()f u n c t i o n , b u tr a t h e rt h a tt h e r ei s n o wh e r ef o ri tt owr i t et oo re v e naf o n tt owr i t ewi t h( a n dt h a t ' sh a r d l yt h ef u l l l i s to ft h i n g st oc o n s i d e r ) .No p e , i fy o uwa n tt ob ea b l et o d i s p l a yt e x t , y o u ' l l h a v et ob u i l dt h ewh o l et h i n gf r o ms c r a t c hy o u r s e l f .An dy o ud owa n tt ob ea b l et owr i t et e x tt ot h es c r e e n , So , wh a td owen e e df o rat e x ts y s t e m?We l l , t h a t ' sa c t u a l l yn o tas i mp l eq u e s t i o n .Ob v i o u s l y , y o un e e daf o n t .J u s tab i t ma pwi t ht h e v a r i o u sc h a r a c t e r sh e r e , n on e e dt od e p r e s so u r s e l v e swi t hv e c t o r b a s e df o n t so naGBA.Se c o n d , y o un e e dawa yo ft a k i n gs p e c i f i c c h a r a c t e r sa n ds h o wt h e mo nt h es c r e e n . Bu twa i tami n u t e , wh i c hv i d e omo d ea r eweu s i n g ?Th e r e ' st i l e ma p s , b i t ma pmo d e sa n ds p r i t e st oc h o o s ef r o m, a l l o fwh i c hn e e dt o b ed e a l twi t hi ne n t i r e l yd i f f e r e n twa y s .Ar ewes e t t l i n gf o ro n eo ft h e m, o rc r e a t es o me t h i n gu s a b l ef o ra l l ?Al s o , wh a ti st h ef o n twe ' r e u s i n g , a n dwh a ta r et h ec h a r a c t e rs i z e s ?Fi x e dwi d t ho rv a r i a b l ewi d t h ?Va r i a b l ewi d t ha n ds i z e sa r e n ' tmu c ho fap r o b l e mf o rt h eb i t ma p mo d e s , b u ti t ' sab i t c ht os p l i c et h e mf o rt i l e s .Al s o , j u s tf o rt i l e s , d owek e e pt h ef u l l f o n ti nVRAM?I fs o , t h a t ' sal o to ft i l e s , e s p e c i a l l y c o n s i d e r i n gy o u ' l l h a r d l yb eu s i n ga l l o ft h e ma tt h es a met i me .I two u l db emo r eVRAM e f f i c i e n tt oo n l yc o p yi nt h eg l y p h st h a ty o u ' r e u s i n ga tt h a tt i me .Th i swi l l t a k es o mema n a g e me n t , t h o u g h . J u s twi t ht h e s ei t e ms , y o u ' dh a v ee n o u g ho p t i o n sf o ro v e r2 0d i f f e r e n tt e x ts y s t e mi mp l e me n t a t i o n s , a l l i n c o mp a t i b l ei nv e r ys u b t l e wa y s .Att h ev e r yl e a s ty o u ' l l n e e dputc()a n dputs()f o re a c h .An dt h e np e r h a p saprintf()l i k ef u n c t i o nt o o ;f o re a c ht e x t t y p e , mi n dy o u , b e c a u s eg l y p hp l a c e me n tg o e so nt h ei n s i d e .Ma y b eas c r e e nc l e a rt o o ;o rh o wa b o u ts c r o l l i n gf u n c t i o n a l i t y .We l l , y o u g e tt h ei d e a . Is u p p o s ei t ' sp o s s i b l et oc r e a t eab i g , c o mp l i c a t e ds y s t e m, t a i l o r i n gt oe v e r yn e e da n y o n ec o u l dp o s s i b l yh a v e .Bu tI ' mn o tg o i n gt o . Fi r s t l y , b e c a u s ei t ' sab i twa s t eo ft i me : t h ec h a n c e sy o u ' l l n e e dt h ea b i l i t yt or u n , s a y , b i t ma pa n dt i l e ma pmo d e sc o n c u r r e n t l ya r e v i r t u a l l y–i fn o ta c t u a l l y –n i l .Mo s to ft h et i me , y o u ' l l u s eas i n g l ev i d e omo d ea n ds t i c kt ot h a t .Sp e n d i n gt i me( a n ds p a c e )f o ra l l o we v e r y v a r i a t i o ni ma g i n a b l e , wh e nh a r d l ya n ywi l l e v e rb eu s e di sp r o b a b l yn o two r t ht h et r o u b l e .Be s i d e s , wr i t i n gt o n so fc o d et h a ti sa l mo s t i d e n t i c a l e x c e p tf o rs o mes ma l l d e t a i l i nt h eh e a r to ft h er o u t i n ei sj u s tp l a i nb l e h . Th ep o i n to ft h i sc h a p t e ri st os h o wh o wt ob u i l da n du s eas e to fs i mp l e , l i g h t we i g h tt e x twr i t e r s .Do n ' te x p e c tt h emo t h e ro fa l l t e x t s y s t e ms , I ' mma i n l yi n t e r e s t e di ng e t t i n gt h ee s s e n t i a l t h i n gd o n e , n a me l yg e t t i n gt h ec h a r a c t e r so fas t r i n go nt h es c r e e n .Th i si sac o r e t e x ts y s t e m, wi t ht h ef o l l o wi n gf e a t u r e s : Bi t ma p( mo d e3 , 4 , 5 ) , r e g u l a rt i l e ma p( mo d e0 , 1 )a n ds p r i t es u p p o r t . Th e r ewi l l b eaxxx_puts()f o rs h o wi n gt h es t r i n g , a n daxxx_clrs()t owi p ei t .Th e i ra r g u me n t swi l l as t r i n g , t h e p o s i t i o nt op l o tt o , a n ds o mec o l o ri n f o r ma t i o n .I fy o uwa n ts c r o l l i n ga n d / o rf o r ma ts p e c i f i e r s , I ' l l l e a v et h a tu pt oy o u . Th ef o n ti saf i x e dwi d t h , mo n o c h r o mef o n twi t ho n e8 x 8t i l ep e rc h a r a c t e r .Th eg l y p h sc a nb es ma l l e rt h a n8 x 8 , a n dI ' l l e v e n l e a v ei nh o o k st h a ta l l o wv a r i a b l ewi d t h s , b u tt h i n g sj u s tg e th o r r i b l ei fI ' da l l o we df o rmu l t i t i l ef o n t s . Av a r i a b l ec h a r a c t e rma p .Th i si sag r e a tf e a t u r ei fy o up l a no nu s i n go n l yas ma l l s e to fc h a r a c t e r s , o rn o n a s c i i g l y p ho r d e r s . Th i sa r r a n g e me n ta l l o wsf o rt h emo s tb a s i cc a s e sa n da l l o wsf o rs o mev a r i a t i o n si ns e t u p , b u tv e r yl i t t l eo nt h es i d e .Ho we v e r , t h o s e e x t r a swo u l dp r o b a b l yb ev e r yg a mes p e c i f i ca n y wa y , a n dmi g h tb ei l l s u i t e df o rag e n e r a l t e x ts y s t e m.I fy o uwa n te x t r a s , i ts h o u l d n ' tb e file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 165/331 28-03-13 Tonc : GBA Programming in rot13 t o oh a r dt owr i t et h e my o u r s e l f . No printf(). O rly? Is a i dt h a tt h e r ei sn oprintf()o nt h eGBA, b u tt h i si s n ' tq u i t et r u e ;n o ta n y mo r e , a n y wa y .I ti sp o s s i b l et oh o o ky o u ro wn I Os y s t e mt ot h es t a n d a r dI Or o u t i n e s , wh i c hi sd o n ei nlibgba. Semi-obsolete Ih a v ea n o t h e rt e x ts y s t e mh e r et h a ti smu c hmo r ep o we r f u l ( a si n , r e a l l ywo r k i n go ne v e r yv i d e omo d ea n dh a sap r i n t ft o o ) t h a nwh a t ' sd e s c r i b e di nt h i sp a g e .Ho we v e r , i t ' sr a t h e rl a r g e , n o tc o mp l e t e l yf i n i s h e da n di two u l dt a k es o met i met owr i t et h e d e s c r i p t i o np a g ea n da l t e rt h et e x tt of i tt h ed e mo sa g a i n .At o n c l i bv e r s i o nt h a th a st h er e l e v a n tc h a n g e sc a nb ef o u n da t h t t p : / / www. c o r a n a c . c o m/ f i l e s / mi s c / t o n c l i b 1 . 3 b . r a r . 19.2. Text system internals 19.2.1. Variables Fo rk e e p i n gt r a c ko ft h et e x t s y s t e m' ss t a t e , we ' l l n e e dac o u p l eo fv a r i a b l e s .Th eo b v i o u sv a r i a b l e sa r eaf o n ta n dac h a r a c t e rma p . Be c a u s eIl i k et ok e e pt h i n g sf l e x i b l e , I ' l l a l s ou s et wop o i n t e r sf o rt h e s es ot h a ty o uc a nu s ey o u ro wnf o n ta n dc h a r ma pi fy o uwa n t . Yo ua l s on e e dt ok n o wwh e r ei ti sy o uwa n tt owr i t et o , wh i c hi sd o n ev i aab a s e d e s t i n a t i o np o i n t e r .Ase x t r a s , I ' l l a l s oh a v ec h a r a c t e r s i z ev a r i a b l e sf o rv a r i a b l eg l y p hs p a c i n g , a n de v e nap o i n t e rt oac h a r wi d t ha r r a y , f o rap o s s i b l ev a r i a b l e wi d t hf o n t . I ' l l u s eas t r u c tt os t o r et h e s e , p a r t i a l l yb e c a u s ei t ' se a s i e rf o rmet oma i n t a i n , b u ta l s ob e c a u s et h eCPUa n dc o mp i l e rc a nd e a l wi t h t h e mmo r ee f f i c i e n t l y .I ' l l a l s ol e a v eaf e wb y t e se mp t yf o ra n ye v e n t u a l e x p a n s i o n .Fi n a l l y , a ni n s t a n c eo ft h i ss t r u c t , a n dap o i n t e rt oi t s oy o uc a ns wi t c hb e t we e nd i f f e r e n ts y s t e msi fy o ue v e rn e e dt o( wh i c hi su n l i k e l y , b u ts t i l l ) .Ye s , Ia mwa s t i n gaf e wb y t e s , b u ti fy o u ma xo u tI WRAM o v e rt h i s , Id a r es a yy o uh a v eb i g g e rp r o b l e mst owo r r ya b o u t . // In text.h typedef struct tagTXT_BASE { u16 *dst0; // writing buffer starting point u32 *font; // pointer to font used u8 *chars; // character map (chars as in letters, not tiles) u8 *cws; // char widths (for VWF) u8 dx,dy; // letter distances u16 flags; // for later u8 extra[12]; // ditto } TXT_BASE; extern TXT_BASE __txt_base, *gptxt; // In text.c TXT_BASE __txt_base; TXT_BASE *gptxt= &__txt_base; Main TXT_BASE instance and a pointer to it 19.2.2. The font Fig 19.1: De f a u l tt o n cf o n t : mi n i a s c i i , mo n o c h r o me , 8 x 8pi x e l spe rg l yph . Fi g1 9 . 1s h o wst h ef o n tI ' l l b eu s i n g .Th i sp a r t i c u l a rf o n ti smo n o c h r o mea n de a c ho ft h eg l y p h sf i t si n t oa n8 x 8b o x .Th e9 6g l y p h s t h e ms e l v e sas u b s e to ft h ef u l l ASCI It h a tI ' l l r e f e rt oa smini-ascii.I t ' st h el o we ra s c i i h a l ft h a tc o n t a i n st h ema j o r i t yo ft h es t a n d a r d ASCI It a b l e , b u tl e a v e so u tASCI I0 3 1b e c a u s et h e y ' r ee s c a p ec o d e sa n dn o tr e a l l yp a r to ft h ep r i n t a b l ec h a r a c t e r sa n y wa y . I ti sp o s s i b l et ou s ead i f f e r e n tf o n twi t ha n o t h e rg l y p ho r d e r , b u tt h ef u n c t i o n sI ' l l p r e s e n tb e l o wr e l yo no n et i l ep e rg l y p h , and i nt i l e l a y o u t .In e e dt h i sa r r a n g e me n tb e c a u s eIi n t e n dt ou s ei tf o ra l l mo d e s , a n dn o ns i n g l e t i l ef o r ma t swo u l db eh e l l i nt i l emo d e s . An o t h e rr e s t r i c t i o ni st h a tt h ef o n tmu s tb eb i t p a c k e dt o1 b p p .Ih a v eac o u p l eo fr e a s o n sf o rt h i s .Fi r s t , t h e r ei st h es i z e c o n s i d e r a t i o n .A9 6g l y p h , 1 6 b i tf o n t( f o rmo d e s3 / 5 )wo u l dt a k eu p1 2 k B.Pa c kt h a tt o1 b p pa n di t ' sl e s st h a to n ek B!Ye s , y o u ' r e r e s t r i c t e dt omo n o c h r o me , b u tf o raf o n t , t h a t ' sr e a l l yn o tmu c ho fap r o b l e m.Of t e nf o n t sa r emo n o c h r o mea n y wa ya n du s i n g1 6b i t s wh e r ey o uo n l yn e e do n es e e msab i to fawa s t e .Se c o n d l y , h o wwo u l dy o ug e ta1 6 b p pf o n tt owo r kf o r4 b p po r8 b p pt i l e s ?Go i n gf r o m al o wb p pt oah i g h e ro n ei sj u s tal o te a s i e r .Ofc o u r s e , i fy o ud o n ' tl i k et h i sa r r a n g e me n t , f e e l f r e et owr i t ey o u ro wnf u n c t i o n s . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 166/331 28-03-13 Tonc : GBA Programming in rot13 Asf o rt h ef o n td a t ai t s e l f , h e r ei st h ewh o l et h i n g . const unsigned int toncfontTiles[192]= { 0x00000000, 0x00000000, 0x18181818, 0x00180018, 0x00003636, 0x00000000, 0x367F3636, 0x0036367F, 0x3C067C18, 0x00183E60, 0x1B356600, 0x0033566C, 0x6E16361C, 0x00DE733B, 0x000C1818, 0x00000000, 0x0C0C1830, 0x0030180C, 0x3030180C, 0x000C1830, 0xFF3C6600, 0x0000663C, 0x7E181800, 0x00001818, 0x00000000, 0x0C181800, 0x7E000000, 0x00000000, 0x00000000, 0x00181800, 0x183060C0, 0x0003060C, 0x7E76663C, 0x003C666E, 0x181E1C18, 0x00181818, 0x3060663C, 0x007E0C18, 0x3860663C, 0x003C6660, 0x33363C38, 0x0030307F, 0x603E067E, 0x003C6660, 0x3E060C38, 0x003C6666, 0x3060607E, 0x00181818, 0x3C66663C, 0x003C6666, 0x7C66663C, 0x001C3060, 0x00181800, 0x00181800, 0x00181800, 0x0C181800, 0x06186000, 0x00006018, 0x007E0000, 0x0000007E, 0x60180600, 0x00000618, 0x3060663C, 0x00180018, 0x5A5A663C, 0x003C067A, 0x7E66663C, 0x00666666, 0x3E66663E, 0x003E6666, 0x06060C78, 0x00780C06, 0x6666361E, 0x001E3666, 0x1E06067E, 0x007E0606, 0x1E06067E, 0x00060606, 0x7606663C, 0x007C6666, 0x7E666666, 0x00666666, 0x1818183C, 0x003C1818, 0x60606060, 0x003C6660, 0x0F1B3363, 0x0063331B, 0x06060606, 0x007E0606, 0x6B7F7763, 0x00636363, 0x7B6F6763, 0x00636373, 0x6666663C, 0x003C6666, 0x3E66663E, 0x00060606, 0x3333331E, 0x007E3B33, 0x3E66663E, 0x00666636, 0x3C0E663C, 0x003C6670, 0x1818187E, 0x00181818, 0x66666666, 0x003C6666, 0x66666666, 0x00183C3C, 0x6B636363, 0x0063777F, 0x183C66C3, 0x00C3663C, 0x183C66C3, 0x00181818, 0x0C18307F, 0x007F0306, 0x0C0C0C3C, 0x003C0C0C, 0x180C0603, 0x00C06030, 0x3030303C, 0x003C3030, 0x00663C18, 0x00000000, 0x00000000, 0x003F0000, 0x00301818, 0x00000000, 0x603C0000, 0x007C667C, 0x663E0606, 0x003E6666, 0x063C0000, 0x003C0606, 0x667C6060, 0x007C6666, 0x663C0000, 0x003C067E, 0x0C3E0C38, 0x000C0C0C, 0x667C0000, 0x3C607C66, 0x663E0606, 0x00666666, 0x18180018, 0x00301818, 0x30300030, 0x1E303030, 0x36660606, 0x0066361E, 0x18181818, 0x00301818, 0x7F370000, 0x0063636B, 0x663E0000, 0x00666666, 0x663C0000, 0x003C6666, 0x663E0000, 0x06063E66, 0x667C0000, 0x60607C66, 0x663E0000, 0x00060606, 0x063C0000, 0x003E603C, 0x0C3E0C0C, 0x00380C0C, 0x66660000, 0x007C6666, 0x66660000, 0x00183C66, 0x63630000, 0x00367F6B, 0x36630000, 0x0063361C, 0x66660000, 0x0C183C66, 0x307E0000, 0x007E0C18, 0x0C181830, 0x00301818, 0x18181818, 0x00181818, 0x3018180C, 0x000C1818, 0x003B6E00, 0x00000000, 0x00000000, 0x00000000, }; Ye s , t h i si st h eentire f o n t , f i t t i n gn i c e l yo no n es i n g l ep a g e .Th i si swh a tb i t p a c k i n gc a nd of o ry o ub u t , l i k ea n yc o mp r e s s i o nme t h o d , i t ma yb eal i t t l et r i c k ys e e i n gt h a ti ti si n d e e dt h ef o n tg i v e ne a r l i e r , s oh e r e ' sal i t t l ee x p l a n a t i o no fwh a ty o ug o ti nf r o n to fy o u . Bitpacking Bi t p a c k i n gi s n ' th a r dt ou n d e r s t a n d .Da t ai sl i t t l emo r eab i gf i e l do fb i t s .I nb i t p a c k i n g , y o us i mp l y big d r o pb i t sa tr e g u l a ri n t e r v a l sa n dt i et h er e s tb a c kt o g e t h e r .Ou rf o n ti smo n o c h r o me , me a n i n gwe 0 x 0 1 0 2 0 3 0 4 u32 o n l yh a v eo n eb i to fi n f o r ma t i o n .No w, e v e ni nt h es ma l l e s tCd a t a t y p e , b y t e s , t h i swo u l dl e a v e7 b i t su n u s e di fy o uwe r et ou s eo n eb y t ep e rp i x e l .Ho we v e r , y o uc o u l da l s oc r a me i g h tp i x e l si n t o big 0 x 0 1 0 2 0 x 0 3 0 4 o n eb y t e , a n dt h u ss a v eaf a c t o r8i ns p a c e .Fo rt h er e c o r d , t h a t ' sac o mp r e s s i o nl e v e l o f8 8 %, u16 p r e t t yg o o dI ' ds a y .Ofc o u r s e , i fy o ur e a da l l t h eo t h e rp a g e sa l r e a d y , y o u ' dh a v ea l r e a d y u8 0x01 0x02 0x03 0x04 r e c o g n i z e di n s t a n c e so fb i t p a c k i n g : 4 b p pt i l e sa r eb i t p a c k e dwi t h2p i x e l s / b y t e .Sot h i ss t u f f little s h o u l d n ' tb ec o mp l e t e l yn e w. 0 x 0 2 0 1 0 x 0 4 0 3 u16 Bi t p a c k i n gc a ns a v eal o to fr o o m, a n di np r i n c i p l e , i t ' se a s yt od o , a si t ' sj u s tama t t e ro f ma s k i n ga n ds h i f t i n g .Th e r ei so n ema j o rc a t c h , h o we v e r : endianness.Yo ua l r e a d ys e e no n e little 0 x 0 4 0 3 0 2 0 1 i n c a r n a t i o no ft h i si no t h e rd a t a a r r a y s : t h ewo r d0x01234567wo u l da c t u a l l yb es t o r e da st h e u32 b y t e s e q u e n c e0x67, 0x45, 0x23, 0x01o nARM ( a n di n t e l )s y s t e ms .Th i si sc a l l e dlittleTable 19.1: Bi ge n d i a nvsl i t t l ee n d i a n i n t e r pr e t a t i o no fb yt e s e qu e nc e01 h , endian, b e c a u s et h el i t t l ee n d( t h el o we rb y t e so famu l t i b y t et y p e )o ft h ewo r da r es t o r e di nt h e 0 2 h , 0 3 h , 0 4 h l o we ra d d r e s s e s .Th e r ei sa l s obig-endian, wh i c hs t o r e st h emo s ts i g n i f i c a n tb y t e sf i r s t .Yo uc a n s e et h ed i f f e r e n c e si nt a b l e1 9 . 1 .So meh e xe d i t o r so rme mo r yv i e we r s( i nVBAf o re x a mp l e ) a l l o wy o ut os wi t c hv i e wi n gd a t aa sb y t e s , h a l f wo r d so rwo r d s , s oy o uc a ns e et h ed i f f e r e n c e si n t e r a c t i v e l yt h e r e .Pl e a s er e me mb e rt h a t t h ed a t ai t s e l fd o e snot c h a n g eb e c a u s eo ft h i s , y o uj u s tlook a ti ti nad i f f e r e n twa y . Fo rb i t p a c k i n g , y o ua l s oh a v et od e a l wi t he n d i a n n e s sa tt h eb i tl e v e l .Th ef o n td a t ai sp a c k e di nac o n s i s t e n tb i t l i t t l ea n db y t e l i t t l e f o r ma tf o rt h r e er e a s o n s .Fi r s t , t h i si sh o wGBAb i t p a c k e ds t u f fwo r k sa n y wa y , s oy o uc a nu s et h eBI OSBi t Un p a c kr o u t i n ef o ri t . Se c o n d , i ti samo r en a t u r a l f o r mi nt e r mso fc o u n t i n g : l o we rb i t sc o mef i r s t .Th i r d , b e c a u s ey o uc a ns h i f td o wna l l t h et i mea n dd i s c a r d c o v e r e db i t st h a twa y , ma s k i n gi se a s i e ra n df a s t e r .No w, b i g e n d i a nwo u l db emo r en a t u r a l v i s u a l l yd u et ot h ef a c twewr i t en u mb e r s t h a twa yt o o , s ob i t ma p sa r eo f t e nb i t l i t t l ea swe l l .Wi n d o wsBMPf i l e s , f o re x a mp l e , t h e s eh a v et h e i rl e f t mo s tp i x e l si nt h emo s t s i g n i f i c a n tb i t s , ma k i n gt h e mb i t b i g .Ho we v e r , Wi n d o wsr u n so nI n t e l a r c h i t e c t u r e , wh i c hi sa c t u a l l ybyte l i t t l e e n d i a n , f o rma x i mu m c o n f u s i o n .Si g h .Ohwe l l . I nc a s ei t ' ss t i l l ab i th a z y , f i g1 9 . 2s h o wsh o wt h e‘ F’i sp a c k e df r o m8 x 8p i x e l si n t o2wo r d s .Al l 6 4p i x e l sa r en u mb e r e d0t o6 3 . Th e s ec o r r e s p o n dt ot h eb i t n u mb e r s .Ea c he i g h ts u c c e s s i v eb i t sf o r mab y t e : 0 7ma k eu pb y t e0 , 8 1 5f o r mb y t e1 , e t c .No t eh o wt h e b i t ss e e mt omi r r o rh o r i z o n t a l l y , b e c a u s eweg e n e r a l l ywr i t en u mb e r sb i g e n d i a n .Sot r yt of o r g e ta b o u tt h a ta n dt h i n ko fb i t si nme mo r y t owa l kt h r o u g hf r o m0t o6 3 .Yo uc a na l s ov i e wt h eb i t sa swo r d s , b i t s0 3 1f o rwo r d0a n d3 2 6 3f o rwo r d1 . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 167/331 28-03-13 Tonc : GBA Programming in rot13 pixels bits → 76543210 01111110 00000110 00000110 00011110 00000110 00000110 00000110 00000000 bytes words 0 x 7 E → 0 x 0 6 0 x 0 6 0 x 1 E 0 x 0 6 → 0 x 1 E0 6 0 6 7 E 0 x 0 0 0 6 0 6 0 6 0 x 0 6 0 x 0 6 0 x 0 0 Fig 19.2: ‘ F’ , f r o m8 x 8t i l et o1 b ppb i t l i t t l e , b yt e l i t t l ewo r d s . 19.2.3. Character map Ha v i n gt h emi n i a s c i i f o n ti sn i c ea n da l l b u ta ss t r i n g sa r ef u l l a s c i i , t h i sma yp r e s e n tap r o b l e m.We l l , n o tr e a l l y , b u tt h e r ea r es e v e r a l wa y so fg o i n ga b o u tt h ec o n v e r s i o n . Fi r s t , y o uc a nc r e a t eag i a n ts wi t c h b l o c kt h a tc o n v e r t s , s a y , ‘ A’( a s c i i 6 5 )i n t og l y p h i n d e x3 3 .An dd ot h a tf o ra l l 9 6g l y p h s .I t s h o u l db eo b v i o u st h a tt h i si sad r e a d f u l wa yo fg o i n ga b o u tt h i n g s .We l l i tshould, b u ta p p a r e n t l yi t ' sn o tb e c a u s ec o d el i k et h a ti so u t t h e r e ;Io n l yme n t i o ni th e r es oy o uc a nr e c o g n i z ei tf o rwh a ti ti sa n ds t a yt of a r , f a ra wa yf r o mi t .Si mp l yp u t , i fy o uh a v eas wi t c h b l o c k wh e r et h eo n l yd i f f e r e n c eb e t we e nt h ec a s e si sr e t u r n i n gad i f f e r e n to f f s e t–a n dafixed o f f s e ta tt h a t –y o u ' r ed o i n gs o me t h i n gv e r y , v e r ywr o n g . As e c o n dme t h o dwh i c hi sa ne n o r mo u si mp r o v e me n ti ne v e r ywa yi st os i mp l ys u b t r a c t3 2 .Th a t ' sh o wmi n i a s c i i wa sd e f i n e da f t e r a l l .Qu i c k , s h o r t , a n dt ot h ep o i n t . Ho we v e r , Ik i n d al i k et h et h i r do p t i o n : l o o k u pt a b l e s .We ' v ea l r e a d ys e e nh o wu s e f u l LUTsc a nb ef o rma t h e ma t i c s , b u ty o uc a nu s e t h e mf o ral o tmo r et h a nt h a t .I nt h i sc a s e , t h el u ti sacharcter map, c o n t a i n i n gt h eg l y p h i n d e xf o re a c ha s c i i c h a r a c t e r .Th i sh a sa l mo s t a l l t h eb e n e f i t so ft h es i mp l es u b t r a c t( al o o k u pma yb eaf e wc y c l e ss l o we r ) , b u ti smu c hmo r ef l e x i b l e .Fo re x a mp l e , y o uc a nh a v e n o n a s c i i c h a r ma p so ra l i a st h ec a s e s , t h i n g sl i k et h a t .An o t h e r‘ i n t e r e s t i n g ’t h i n gi st h a ty o ud o n ' tr e a l l yn e e dt h ef o n tt ob et e x ta ss u c h , i tc a nb ea n yk i n do fma p p e di ma g ed a t a ;wi t hal u ty o uc o u l de a s i l yu s et h et e x ts y s t e mf o rd r a wi n gb o r d e r s , a sl o n ga sy o uh a v ea b o r d e r‘ f o n t ’f o ri t .Th el u tI ' mu s i n gi s2 5 6b y t e sl o n g .Th i sma yn o tb ee n o u g hf o rUn i c o d e( s o r r yEa s t e r nd u d e s ) , b u ti t ' se n o u g ht os u i t myp u r p o s e s . General design Th ef i r s tt h i n gt od oc o d e wi s ei st oi n i t i a l i z et h eme mb e r so ft h et e x t b a s e .Th a tme a n sa t t a c ht h ef o n t , s e tt h eg l y p hs i z e s , a n di n i t i a l i z e t h el u t .Th i sc a nb ed o n ewi t htxt_init_std(). u8 txt_lut[256]; // Basc initializer for text state void txt_init_std() { gptxt->dx= gptxt->dy= 8; gptxt->dst0= vid_mem; gptxt->font= (u32*)toncfontTiles; gptxt->chars= txt_lut; gptxt->cws= NULL; int ii; for(ii=0; ii<96; ii++) gptxt->chars[ii+32]= ii; } De p e n d i n go nt h et y p eo ft e x t , y o uma yn e e dmo r es p e c i a l i z e di n i t i a l i z e r s , wh i c hwe ' l l g e tt owh e nt h et i mec o me s .Asf o rwr i t i n ga s t r i n g , t h eb a s i cs t r u c t u r ec a nb e es e e nb e l o w.I t ' sa c t u a l l yq u i t es i mp l ea n dv e r yg e n e r a l , b u tu n f o r t u n a t e l yt h ef a c tt h a txxx_putc() i si nt h ei n n e rl o o pme a n st h a ty o uh a v et oh a v ev i r t u a l l yi d e n t i c a l wr a p p e r sa r o u n de a c hc h a r p l o t t e rf o re a c ht e x tme t h o d .Ia l s oh a v e f u n c t i o n sc a l l e dxxx_clrs()t h a tc l e a rt h es t r i n gf r o mt h es c r e e n( t h e yd o n ' twi p et h ewh o l es c r e e n ) .Th e ya r ea l mo s ti d e n t i c a l t o t h e i rputs()s i b l i n g si nf o r ma n da l s or a t h e rs i mp l e , s oIwo n ' te l a b o r a t eo nt h e mh e r e . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 168/331 28-03-13 Tonc : GBA Programming in rot13 // Pseudo code for xxx_puts void xxx_puts(int x, int y, const char *str, [[more]]) { [[find real writing start]] while(c=*str++) // iterate through string { switch(c) { case [[special chars ('\n' etc)]]: [[handle special]] case [[normal chars]]: [[xxx_putc(destination pointer, lut[c])]] [[advance destination]] } } } 19.3. Bitmap text Bi t ma pt e x tc o n c e r n smo d e s3 , 4a n d5 .I fy o uc a nd omo d e3 , y o up r e t t ymu c hh a v emo d e5a swe l l , a st h et wod i f f e ro n l yb yt h ep i t c h a n d , p e r h a p s , t h es t a r t i n gp o i n t .Mo d e4i sd i f f e r e n t , n o to n l yb e c a u s ei t ' s8 b p p , b u ta l s ob e c a u s et h i sme a n sweh a v et od o2p i x e l sa t o n c e . 19.3.1. Internal routines It e n dt od ob i t ma pr e l a t e df u n c t i o n si nt wop a r t s : t h e r ea r ei n t e r n a l 1 6 b i ta n d8 b i tf u n c t i o n st h a tt a k ea na d d r e s sa n dp i t c ha st h e i r a r g u me n t s , a n dt h e ni n l i n ei n t e r f a c ef u n c t i o n swi t hc o o r d i n a t e st h a tc a l l t h e s e .Th ei n t e r n a l 1 6 b i twr i t e ri sg i v e ni nb e l o w, wi t ha n e x p l a n a t i o no ft h ema i np a r t sb e l o wt h a t . void bm16_puts(u16 *dst, const char *str, COLOR clr, int pitch) { int c, x=0; while((c=*str++) != 0) // (1) for each char in string { // (2) real char/control char switch if(c == '\n') // line break { dst += pitch*gptxt->dy; x=0; } else // normal character { int ix, iy; u32 row; // (3) point to glyph; each row is one byte u8 *pch= (u8*)&gptxt->font[2*gptxt->chars[c]]; for(iy=0; iy<8; iy++) { row= pch[iy]; // (4) plot pixels until row-byte is empty for(ix=x; row>0; row >>= 1, ix++) if(row&1) dst[iy*pitch+ix]= clr; } x += gptxt->dx; } } } 1 .Tr a d i t i o n a l wa yt ol o o pt h r o u g ha l l c h a r a c t e r si nas t r i n g .cwi l l b et h ec h a r a c t e rweh a v et od e a l wi t h , u n l e s si t ' st h ed e l i mi t e r ( '\0') , t h e nwe ' l l s t o p . 2 .No r ma l c h a r / c o n t r o l c h a rs wi t c h .Co n t r o l c h a r a c t e r sl i k e'\n'a n d'\t's h o u l db et a k e nc a r eo fs e p a r a t e l y .I ' mo n l y c h e c k i n gf o rt h en e wl i n er i g h tn o w, b u to t h e r sc o u l de a s i l yb ea d d e d . 3 .Th i si swh e r ei tg e t si n t e r e s t i n g .Wh a tt h i sl i n ed o e si sf i r s tu s et h el u tt ol o o ku pt h eg l y p hi n d e xi nt h ef o n t , l o o ku pt h ea c t u a l g l y p hi nt h ef o n t( mu l t i p l yb y2b e c a u s et h e r ea r e2wo r d s / g l y p h ) , a n dt h e ns e t u pab y t e p o i n t e rpcht op o i n tt ot h eg l y p h . Ac o u p l eo ft h i n g sc o met o g e t h e rh e r e .Fi r s t , b e c a u s ea l l g l y p h sa r ee x a c t l y8b y t e sa p a r t , f i n d i n gt h eg l y p hd a t ai sv e r ye a s y . I fy o uc r e a t ey o u ro wnt e x ts y s t e mwi t hy o u ro wnf o n t s , I ' da d v i s eu s i n gc o n s t a n to f f s e t s , e v e ni fi twa s t e sp i x e l sl i k ey o u wo u l df o rs ma l l c h a r a c t e r sl i k e‘ I ’ .Se c o n d , b e c a u s eo ft h e1 b p pt i l e df o r ma t , e a c hr o wi se x a c t l yo n eb y t el o n g , a n da l l t h e g l y p h sb i t sa r ei nc o n s e c u t i v eb y t e s , s oy o ud o n ' th a v et oj u mpa r o u n df o re a c hn e wr o w.Th i si sag o o dt h i n g . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 169/331 28-03-13 Tonc : GBA Programming in rot13 4 .Th eixl o o pi se v e nmo r ei n t e r e s t i n g .Fi r s t , wer e a dt h ea c t u a l r o wo fp i x e l si n t ot h e( wo r d )v a r i a b l erow.Tot e s twh e t h e rwe n e e dt owr i t eap i x e l , wes i mp l yc h e c kf o rag i v e nb i t .Ho we v e r , b e c a u s et h ep a c k i n gi slittle e n d i a n , t h i sa l l o wsf o rt wo s h o r t c u t s . Th ef i r s to n ei st h a tl o o p i n gt h r o u g ht h eb i t sg o e sf r o ml o wt oh i g hb i t s , me a n i n gt h a twec a ns i mp l ys h i f t r i g h to ne a c hi t e r a t i o n a n dt e s tb i t0 .Th ec o r o l l a r yt ot h i si st h a tt h eb i t swe ' v ea l r e a d yd o n ea r et h r o wna wa y , a n dthis me a n st h a twh e nrowi s0 , t h e r ewi l l b en omo r ep i x e l s , a n dwe ' r ed o n ef o rt h a tr o w.Ast h i ss h o r t c i r c u i th a p p e n si n s i d et h ei n n e ro fatriple l o o p , t h e s p e e d u pc a nb es u b s t a n t i a l . Th i sf u n c t i o no n l yd o e st h eb a r ee s s e n t i a l st og e tas t r i n go ns c r e e n .I tp l o t st h en o n z e r op i x e l so n l y( t r a n s p a r e n tc h a r a c t e r s ) , t h e r ei sn o wr a p p i n ga tt h es i d ea n dn os c r o l l i n g .Th eo n l yn o n t r i v i a l f e a t u r ei st h a ti tc a nd ol i n e b r e a k s .Wh e nt h o s eh a p p e n , t h ec u r s o rr e t u r n st o t h eo r i g i n a l x p o s i t i o no ns c r e e n . Th e8 b i tf u n c t i o ni sa l mo s ti d e n t i c a l t ot h i so n e , ‘ a l mo s t ’b e c a u s eo ft h en o b y t e wr i t er u l ef o rVRAM.Th eo b v i o u so n e sa r et h a tt h e p i t c ha n dc h a r a c t e rs p a c i n gn e e dt ob eh a l v e d .I ' ma l s oma k i n gi trequirement t h a tt h es t a r to fe a c hc h a r a c t e rn e e d st ob eo na ne v e n p i x e l b o u n d a r y .Byd o i n gs o , y o uc a nh a v ea na l mo s ti d e n t i c a l i n n e rl o o pa sb e f o r e ;i tj u s td o e st wop i x e l si ni ti n s t e a do fo n e .Ye s , i t ' sa h a c k ;n o , Id o n ' tc a r e . void bm8_puts(u16 *dst, const char *str, u8 clrid) { int c, x=0, dx= gptxt->dx >> 1; while((c=*str++) != 0) { // <snip char-switch and iy loop> for(ix=x; row>0; row >>= 2, ix++) { pxs= dst[iy*120+ix]; if(row&1) pxs= (pxs&0xFF00) | clrid; if(row&2) pxs= (pxs&0x00FF) | (clrid<<8); dst[iy*120+ix]= pxs; } // <snip> } } 19.3.2. Interface functions Th ei n t e r f a c ef u n c t i o n sa r es t r a i g h t f o r wa r d .Al l t h e yh a v et od oi ss e t u pt h ed e s t i n a t i o ns t a r tf o rt h ei n t e r n a l r o u t i n e s , a n df o rt h e1 6 b i t v e r s i o n s , p r o v i d eap i t c h .Mo d e3u s e svid_mema si t sb a s e , a n dmo d e4a n d5u s evid_paget oma k es u r ei two r k swi t hp a g e f l i p p i n g .m4_puts()a l s oe n s u r e st h a tt h ec h a r a c t e r ss t a r ta te v e np i x e l s , a n dp l e a s er e me mb e rt h a tt h i sr o u t i n eu s e sac o l o r i n d e x , r a t h e rt h a nat r u ec o l o r . // Bitmap text interface. Goes in text.h INLINE void m3_puts(int x, int y, const char *str, COLOR clr) { bm16_puts(&vid_mem[y*240+x], str, clr, 240); } INLINE void m4_puts(int x, int y, const char *str, u8 clrid) { bm8_puts(&vid_page[(y*240+x)>>1], str, clrid); } INLINE void m5_puts(int x, int y, const char *str, COLOR clr) { bm16_puts(&vid_page[y*160+x], str, clr, 160); } 19.3.3. Clearing text Do i n gat e x tc l e a ri sa l mo s tt h es a mea swr i t i n go u tas t r i n g .Th eo n l yf u n c t i o n a l d i f f e r e n c ei st h a ty o u ' r ea l wa y sp u t t i n gas p a c e( o r r a t h e r , as o l i df i l l e dr e c t a n g l e )i n s t e a do ft h eo r i g i n a l c h a r a c t e r s .Yo us t i l l n e e dt h ef u l l s t r i n gy o ut e l l y o uh o wl o n gt h el i n eg o e so n , a n d h o wma n yl i n e st h e r ea r e . Wi t ht h a ti nmi n d , t h ebm16_clrs()f u n c t i o nb e l o ws h o u l d n ' tb et h a th a r dt ou n d e r s t a n d .Th ewh o l ep o i n to fi ti st or e a dt h es t r i n g t of i n do u tt h el e n g t hi np i x e l so fe a c hl i n ei nt h es t r i n g( nx*gptxt->dx) , t h e nf i l l t h er e c t a n g l es p a n n e db yt h a tl e n g t ha n dt h eh e i g h t o ft h ec h a r a c t e r s( gptxt->dy) .Th e r e ' ss o meb o o k k e e p i n gt oma k es u r ei ta l l g o e sa c c o r d i n gt op l a n , b u ti nt h ee n dt h a t ' sa l l i td o e s . Th es a meg o e sf o rt h ec l e a rr o u t i n e so ft h eo t h e rt e x t t y p e s , s oI ' mn o tg o i n gt os h o wt h o s e . void bm16_clrs(u16 *dst, const char *str, COLOR clr, int pitch) { int c, nx=0, ny; file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 170/331 28-03-13 Tonc : GBA Programming in rot13 while(1) { c= *str++; if(c=='\n' || c=='\0') { if(nx>0) { nx *= gptxt->dx; ny= gptxt->dy; while(ny--) { memset16(dst, clr, nx); dst += pitch; } nx=0; } else dst += gptxt->dy*pitch; if(c=='\0') return; } else nx++; } } 19.4. Tilemap text I ns o mewa y s , t e x tf o rt i l e mo d e si sa c t u a l l ye a s i e rt h a nf o rb i t ma p s , a sy o uc a nj u s ts t u f ft h ef o n ti n t oac h a r b l o c ka n dt h e ny o ud o n ' t n e e da n yr e f e r e n c et ot h ef o n ti t s e l fa n y mo r e .Th a ti s , u n l e s sy o uwa n tt oh a v eav a r i a b l ewi d t hf o n t , i nt h a tc a s ey o u ' l l b ei nb i t s h i f t i n g h e l l .Bu tI ' ms t i c k i n gt oaf i x e dwi d t h , s i n g l et i l ef o n t , wh i c hk e e p st h i n g sv e r ys i mp l ei n d e e d . 19.4.1. Tile initialisation Th ef i r s to r d e ro fb u s i n e s si st ob ea b l et ou n p a c kt h ef o n tt oe i t h e r4o r8b i t .Th ee a s i e s twa yo fd o i n gt h i si st oj u s ts e t u pac a l l t o BitUnpack()a n db ed o n ewi t hi t .Ho we v e r , VBA' si mp l e me n t a t i o no fi ti s n ' t( o rwa s n ' t , t h e yma yh a v ef i x e di tb yn o w)q u i t e c o r r e c tf o rwh a tIh a dp l a n n e df o ri t , s oI ' mg o i n gt or o l l myo wn .Ar g u me n t sdstva n dsrcva r et h es o u r c ea n dd e s t i n a t i o na d d r e s s e s , r e s p e c t i v e l y ;leni st h en u mb e ro fs o u r c eb y t e sa n dbppi st h ed e s t i n a t i o nb i t d e p t h .bases e r v e st wop u r p o s e s .Pr i ma r i l y , i ti sa n u mb e rt ob ea d d e dt oa l l t h ep i x e l si fb i t3 1i ss e t , o rt oa l l e x c e p tz e r ov a l u e si fi ti sc l e a r .Th i sa l l o wsag r e a t e rr a n g eo fo u t c o me st h a n j u s tt h e0a n d1t h a tas o u r c eb i t d e p t ho fo n ewo u l ds u p p l y ;a n da no t h e rc u t et r i c kt h a tI ' l l g e tt ol a t e r . // Note, the BIOS BitUnpack does exactly the same thing! void txt_bup_1toX(void *dstv, const void *srcv, u32 len, int bpp, u32 base) { u32 *src= (u32*)srcv; u32 *dst= (u32*)dstv; len= (len*bpp+3)>>2; // # dst words u32 bBase0= base&(1<<31); // add to 0 too? base &= ~(1<<31); u32 swd, ssh=32; // src data and shift u32 dwd, dsh; // dst data and shift while(len--) { if(ssh >= 32) { swd= *src++; ssh= 0; } dwd=0; for(dsh=0; dsh<32; dsh += bpp) { u32 wd= swd&1; if(wd || bBase0) wd += base; dwd |= wd<<dsh; swd >>= 1; ssh++; } *dst++= dwd; file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 171/331 28-03-13 Tonc : GBA Programming in rot13 } } Th ea c t u a l ma p t e x ti n i t i a l i z a t i o ni sd o n eb ytxt_init_se().I t sf i r s tt woa r g u me n t sa r ee x a c t l ywh a ty o u ' de x p e c t : t h eb a c k g r o u n d t h a tt h es y s t e ms h o u l du s ef o rt e x ta n dt h ec o n t r o l f l a g st h a ts h o u l dg ot h e r e( c h a r b l o c k , s c r e e n b l o c k , b i t d e p t h , a l l t h a tj a z z ) .Th et h i r d a r g u me n t , se0, i n d i c a t e st h e‘ b a s e ’f o rp a l e t t ea n dt i l ei n d e x i n g , s i mi l a rt ot h eb a s ef o ru n p a c k i n g .Th ef o r ma ti sj u s tl i k en o r ma l s c r e e n e n t r i e s : se0{ 0 9 }i n d i c a t et h et i l eo f f s e t , a n dse0{ CF}a r ef o rt h e1 6c o l o rp a l e t t eb a n k .clrsc o n t a i n st h ec o l o rf o rt h et e x t , wh i c h wi l l g oi n t ot h ep a l e t t ei n d i c a t e db yt h es u b p a l e t t ea n dt h ef i f t ha r g u me n t , base, t h eb a s ef o rb i t u n p a c k i n g . Fo rn o w, i g n o r et h esecond c o l o ri nclrs, a n dt h ee x t r ap a l e t t ewr i t ef o r4b p p .I na l l l i k e l i h o o d , y o ud o n ' twa n tt ok n o w.I ' mg o i n g t ot e l l y o ua b o u tt h e ml a t e ra n y wa y , t h o u g h . void txt_init_se(int bgnr, u16 bgcnt, SB_ENTRY se0, u32 clrs, u32 base) { bg_cnt_mem[bgnr]= bgcnt; gptxt->dst0= se_mem[BF_GET(bgcnt, BG_SBB)]; // prep palette int bpp= (bgcnt&BG_8BPP) ? 8 : 4; if(bpp == 4) { COLOR *palbank= &pal_bg_mem[BF_GET(se0, SE_PALBANK)<<4]; palbank[(base+1)&15]= clrs&0xFFFF; palbank[(base>>4)&15]= clrs>>16; } else pal_bg_mem[(base+1)&255]= clrs&0xFFFF; // account for tile-size difference se0 &= SE_ID_MASK; if(bpp == 8) se0 *= 2; // Bitunpack the tiles txt_bup_1toX(&tile_mem[BF_GET(bgcnt, BG_CBB)][se0], toncfontTiles, toncfontTilesLen, bpp, base); } I fy o ud o n ' twa n tt od e a l wi t ha l l k i n d so fo f f s e t s , y o uc a nj u s tl e a v et h et h i r da n df i f t ha r g u me n t sz e r o .I t ' sp r o b a b l yn o tag o o di d e at o l e a v et h eo t h e r sz e r o , b u tf o rt h o s et woi t ' sn o tap r o b l e m. 19.4.2. Screen entry writer Th i si sa r g u a b l yt h emo s ts i mp l eo ft h et e x twr i t e r s .Ast h e r ei so n eg l y p hp e rs c r e e ne n t r y , a l l y o uh a v et od oi swr i t eas i n g l eh a l f wo r d t ot h es c r e e n b l o c ki nt h er i g h tp o s i t i o na n dy o uh a v eal e t t e r .Re p e a tt h i sf o rawh o l es t r i n g . Th e r ea r eaf e wt h i n g st on o t ea b o u tt h i si mp l e me n t a t i o n , t h o u g h .Fi r s t , l i k eb e f o r e , n ok i n do fwr a p p i n go rs c r o l l i n g .I fy o uwa n tt h a t , y o u ' l l h a v et od oa l l t h a ty o u r s e l f .Al s o , t h ex a n dy c o o r d i n a t e sa r es t i l l i npixels, n o tt i l e s .I ' v ed o n et h i sma i n l yf o rc o n s i s t e n c ywi t ht h e o t h e rwr i t e r s , n o t h i n gmo r e .Oh , i nc a s ey o uh a d n ' tn o t i c e db e f o r e , gptxt->dst0i si n i t i a l i z e dt op o i n tt ot h es t a r to ft h eb a c k g r o u n d ' s s c r e e n b l o c ki ntxt_init_se().La s t l y , se0i sa d d e dt oma k eu pt h ea c t u a l s c r e e ne n t r y ;i fy o uh a dan o n z e r ose0i ni n i t i a l i z a t i o n , c h a n c e sa r ey o u ' dwa n tt ou s ei th e r et o o . void se_puts(int x, int y, const char *str, SB_ENTRY se0) { int c; SB_ENTRY *dst= &gptxt->dst0[(y>>3)*32+(x>>3)]; x=0; while((c=*str++) != 0) { if(c == '\n') // line break { dst += (x&~31) + 32; x=0; } else dst[x++] = (gptxt->chars[c]) + se0; } } 19.5. Sprite text Sp r i t et e x ti ss i mi l a rt ot i l e ma pt e x t , o n l yy o uu s eOBJ _ATTRsn o wi n s t e a do fs c r e e ne n t r i e s .Yo uh a v et os e tt h ep o s i t i o nma n u a l l y ( a t t r i b u t e s0a n d1 ) , a n da t t r i b u t e2i sa l mo s tt h es a mea st h es c r e e ne n t r yf o rr e g u l a rt i l e ma p s .Th ei n i t i a l i z e rtxt_init_obj()i s s i mi l a rt otxt_init_se(), e x c e p tt h a tt h et i l e ma pd e t a i l sh a v eb e e nr e p l a c e db yt h e i rOAM c o u n t e r p a r t s .I n s t e a do fas c r e e n b l o c k , file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 172/331 28-03-13 Tonc : GBA Programming in rot13 wep o i n tt oab a s eOBJ _ATTRoe0, a n dattr2wo r k si nmu c ht h es a mewa ya sse0d i d .Th ec o d ei sa c t u a l l ys i mp l e rb e c a u s ewe c a na l wa y su s e4 b p pt i l e sf o rt h eo b j e c t st h a tweu s e , wi t h o u tu p s e t t i n gt h eo t h e r s . // OAM text initializer void txt_init_obj(OBJ_ATTR *oe0, u16 attr2, u32 clrs, u32 base) { gptxt->dst0= (u16*)oe0; COLOR *pbank= &pal_obj_mem[BF_GET(attr2, ATTR2_PALBANK)<<4]; pbank[(base+1)&15]= clrs&0xFFFF; pbank[(base>>4)&15]= clrs>>16; txt_bup_1toX(&tile_mem[4][attr2&ATTR2_ID_MASK], toncfontTiles, toncfontTilesLen, 4, base); } // OAM text writer void obj_puts(int x, int y, const char *str, u16 attr2) { int c, x0= x; OBJ_ATTR *oe= (OBJ_ATTR*)gptxt->dst0; while((c=*str++) != 0) { if(c == '\n') // line break { y += gptxt->dy; x= x0; } else { if(c != ' ') // Only act on a non-space { oe->attr0= y & ATTR0_Y_MASK; oe->attr1= x & ATTR1_X_MASK; oe->attr2= gptxt->chars[c] + attr2; oe++; } x += gptxt->dx; } } } Th es t r u c t u r eo ft h ewr i t e ri t s e l fs h o u l df e e l f a mi l i a rn o w.Th eattr2a g a i na c t sa sab a s eo f f s e tt oa l l o wp a l e t t es wa p p i n ga n da n o f f s e tt i l es t a r t .No t et h a tI ' mo n l ye n t e r i n gt h ep o s i t i o ni na t t r i b u t e s0a n d1 , a n dn o t h i n ge l s e .Ic a nd ot h i sb e c a u s et h er e s to ft h et h i n g s a r ea l r e a d ys e tt owh a tIwa n t , n a me l y , 8 x 8 ps p r i t e swi t h4 b p pt i l e sa n dn of r i l l s .Ye s , t h i sma ys c r e wt h i n g su pf o rs o me , b u ti fIdid ma s ko u te v e r y t h i n gp r o p e r l y , i t ' ds c r e wu po t h e rs t u f f .Th i si saj u d g e me n tc a l l , f e e l f r e et od i s a g r e ea n dc h a n g ei t . Th a twr i t e ra l wa y ss t a r t sa taf i x e dOBJ _ATTR, o v e r wr i t i n ga n yp r e v i o u so n e s .Be c a u s et h a tmi g h tb eu n d e s i r a b l e , Ia l s oh a v ea s e c o n d a r ys p r i t ewr i t e r , obj_puts2, wh i c ht a k e sa nOBJ _ATTRa sa na r g u me n tt os e r v ea st h en e wb a s e . INLINE void obj_puts2(int x, int y, const char *str, u16 attr2, OBJ_ATTR *oe0) { gptxt->dst0= (u16*)oe0; obj_puts(x, y, str, attr2); } Th e r ea r es o mes i d en o t e so nme mo r yu s et h a tIs h o u l dme n t i o n .Re me mb e r , t h e r ea r eo n l y1 2 8OBJ _ATTRs , a n da to n ee n t r y / g l y p hi t ma yb e c o mep r o h i b i t i v e l ye x p e n s i v ei fu s e de x t e n s i v e l y .I nt h es a mev e i n , 1 0 2 4t i l e sma ys e e ml i k eal o t , b u ty o uc a nr u no u tq u i c k l yi f y o uh a v eac o u p l eo fc o mp l e t ea n i ma t i o n si nt h e r ea swe l l .Al s o , r e me mb e rt h a ty o uo n l yh a v e5 1 2t i l e si nt h eb i t ma pmo d e s : af u l l ASCI Ic h a r a c t e rs e ti nb i t ma pmo d e swo u l dt a k eu phalf t h es p r i t et i l e s ! I fy o u ' r ej u s tu s i n gi tt of o rac o u p l eo fc h a r a c t e r sy o u ' r en o tl i k e l yt or u ni n t ot r o u b l e , b u ti fy o uwa n ts c r e e n sf u l l o ft e x t , y o umi g h t b eb e t t e ro fwi t hs o me t h i n ge l s e .Th e r ea r ewa y st og e ta r o u n dt h e s et h i n g s , o fc o u r s e ;q u i t es i mp l ewa y s , e v e n .Bu tb e c a u s et h e y ' r e r e a l l yg a me s p e c i f i c , i t ' sd i f f i c u l tt og i v eag e n e r a l s o l u t i o nf o ri t . 19.6. Some demos 19.6.1. Bitmap text demo Is u p p o s eIc o u l ds t a r twi t h“ He l l owo r l d ” , b u ta st h a t ' sp r e t t yb o r i n gIt h o u g h tI ' ds t a r twi t hs o me t h i n gmo r ei n t e r e s t i n g .Th etxt_bm d e mod o e ss o me t h i n gs i mi l a rt obm_modes: n a me l ys h o ws o me t h i n go ns c r e e na n da l l o ws wi t c h i n gb e t we e nmo d e s3 , 4a n d5t os e e wh a tt h ed i f f e r e n c e sa r e .On l yn o w, we ' r eg o i n gt ou s et h eb i t ma pputs()v e r s i o n st owr i t et h ea c t u a l s t r i n g si n d i c a t i n gt h ec u r r e n t mo d e .Be c a u s et h a t ' ss t i l l p r e t t yb o r i n g , I ' ma l s og o i n gt op u tamo v a b l ec u r s o ro ns c r e e na n dwr i t eo u ti t sc o o r d i n a t e s .He r e ' st h ef u l l file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 173/331 28-03-13 Tonc : GBA Programming in rot13 c o d e : #include <stdio.h> #include <tonc.h> #define CLR_BD 0x080F const TILE cursorTile= {{ 0x0, 0x21, 0x211, 0x2111, 0x21111, 0x2100, 0x1100, 0x21000 }}; void base_init() { vid_page= vid_mem; // init interrupts irq_init(NULL); irq_add(II_VBLANK, NULL); // init backdrop pal_bg_mem[0]= CLR_MAG; pal_bg_mem[CLR_BD>>8]= CLR_BD; pal_bg_mem[CLR_BD&255]= CLR_BD; m3_fill(CLR_BD); // init mode 4 pal pal_bg_mem[1]= CLR_LIME; pal_bg_mem[255]= CLR_WHITE; // init cursor tile_mem[5][0]= cursorTile; pal_obj_mem[1]= CLR_WHITE; pal_obj_mem[2]= CLR_GRAY; } int main() { base_init(); txt_init_std(); // (1) print some string so we know what mode we're at m3_puts( 8, 8, "mode 3", CLR_CYAN); m4_puts(12, 32, "mode 4", 1); m5_puts(16, 40, "mode 5", CLR_YELLOW); // init variables u32 mode=3, bClear=0; OBJ_ATTR cursor= { 80, 120, 512, 0 }; // init video mode REG_DISPCNT= DCNT_BG2 | DCNT_OBJ | 3; // init cursor string char str[32]; siprintf(str, "o %3d,%3d", cursor.attr1, cursor.attr0); while(1) { VBlankIntrWait(); oam_mem[0]= cursor; key_poll(); if(key_hit(KEY_START)) bClear ^= 1; // move cursor cursor.attr1 += key_tri_horz(); cursor.attr0 += key_tri_vert(); // adjust cursor(-string) only if necessary if(key_is_down(KEY_ANY)) { file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 174/331 28-03-13 Tonc : GBA Programming in rot13 // (2) clear previous coords if(bClear) bm_clrs(80, 112, str, CLR_BD); cursor.attr0 &= ATTR0_Y_MASK; cursor.attr1 &= ATTR1_X_MASK; // (3) update cursor string siprintf(str, "%c %3d,%3d", (bClear ? 'c' : 'o'), cursor.attr1, cursor.attr0); } // switch modes if(key_hit(KEY_L) && mode>3) mode--; else if(key_hit(KEY_R) && mode<5) mode++; REG_DISPCNT &= ~DCNT_MODE_MASK; REG_DISPCNT |= mode; // (4) write coords bm_puts(80, 112, str, CLR_WHITE); } return 0; } Co n t r o l s : Mo v e sc u r s o r . D-pad g g l e ss t r i n gc l e a r i n g . Start To c r e a s eo ri n c r e a s emo d e . L, R De Ma n yt h i n g sh e r es h o u l db ee i t h e rs e l fe x p l a n a t o r yo rf a i r l yi r r e l e v a n t .Th ei n t e r e s t i n gt h i n g s a r ei n d i c a t e db yn u mb e r s , s ol e t ' sg ot h r o u g ht h e m, s h a l l we ? 1. Mode indicators.Th i si swh e r ewewr i t et h r e es t r i n g st oVRAM, i n d i c a t i n gt h emo d e s . No t et h a tt h ei n t e r f a c e sa r en e a r l yi d e n t i c a l ;t h eo n l yr e a l d i f f e r e n c ei st h a tt h ef o u r t h Fig 19.3: txt_bmd e mo . a r g u me n tf o rm4_puts()i sap a l e t t ei n d e x , r a t h e rt h a nar e a l c o l o r . 2. Clear previous cursor-string.Th ec u r s o rs t r i n gk e e p st r a c ko ft h ec u r s o ra sy o umo v ea c r o s st h es c r e e n .Th ef i r s tt h i n gy o u ' l l n o t i c ei st h a tt h es t r i n gt u r n si n t oah o r r i b l eme s sb e c a u s et h eb i t ma pwr i t e r so n l ywr i t et h enon-zero p i x e l so ft h ef o n t .I no t h e rwo r d s , i t d o e snot c l e a ro u tt h er e s to ft h es p a c ea l l o t t e df o rt h a tg l y p h .Es s e n t i a l l ymx_puts()a r et r a n s p a r e n ts t r i n gwr i t e r s . Su r e , Ic o u l dh a v ea d d e das wi t c ht h a two u l de r a s et h ewh o l eg l y p hf i e l dt ot h ewr i t e r s .Qu i t ee a s i l y , a c t u a l l y , i to n l yt a k e sa ne x t r a elsec l a u s e .Ho we v e r , t h ec u r r e n twa yi sa c t u a l l ymo r ep r a c t i c a l .Fo ro n et h i n g , wh a ti fy o ua c t u a l l ywant t r a n s p a r e n c y ?Yo u ' dh a v e t owr i t ea n o t h e rr o u t i n ej u s tf o rt h a t .Th eme t h o dI ' v ec h o s e ni st oh a v ea ne x t r ac l e a r i n gr o u t i n e( wh i c hy o u ' dp r o b a b l yn e e da n y wa y ) . Too v e r wr i t et h ewh o l eg l y p h s , s i mp l yc a l l mx_clrs()f i r s t ;wh i c hi swh a tI ' md o i n gh e r e .We l l , a sl o n ga st h ebClearv a r i a b l ei ss e t ( t o g g l ewi t hSt a r t ) . As e c o n dr e a s o ni st h a tt h i sme t h o di sj u s ts omu c hf a s t e r .No to n l yb e c a u s eIwo u l d n ' tb ea b l et ou s emyp r e ma t u r eb r e a k i n gf r o m t h eixl o o pi fIh a dt oe r a s et h ewh o l ef i e l da n dt h eme r ep r e s e n c eo fa ne x t r ab r a n c ha d d smo r ec y c l e s( i n s i d eat r i p l el o o p ) , b u t p l o t t i n gi n d i v i d u a l c h a r a c t e r swi l l a l wa y sb es l o we rt h a nt od oi tb ywh o l eb l o c k sa tat i me .mx_clrs()u s e smemset16(), wh i c hi s b a s i c a l l yCpuFastSet()p l u ss a f e t i e s , a n dwi l l b ef a s t e ra f t e rj u s tame r eh a l fad o z e np i x e l s . Oh , i nc a s ey o u ' r ewo n d e r i n gwh yI ' mt a l k i n ga b o u tmx_clrs()wh e nt h ec o d eme n t i o n sbm_clrs(), t h el a t t e rf u n c t i o ni s me r e l yaf u n c t i o nt h a tu s e sas wi t c h b l o c kwi t ht h ec u r r e n tb i t ma pmo d et oc a l l t h ec o r r e c tmo d e s p e c i f i cs t r i n gc l e a r e r . 3. Updating the cursor string.Ast h ewr i t e r sd o n ' th a v ef o r ma ts p e c i f i c a t i o nf i e l d s , h o wc a nwewr i t en u mb e r s ?Si mp l e , u s e sprintf()t op r e p a r eas t r i n gf i r s t , a n dt h e nu s et h a to n ei n s t e a d .Orr a t h e r , u s esiprintf().Th i si sa ni n t e g e r o n l yv e r s i o no f sprintf(), wh i c hi sb e t t e rs u i t e dt oGBAp r o g r a mmi n gs i n c ey o u ' r en o ts u p p o s e dt ou s ef l o a t i n gp o i n tn u mb e r sa n y wa y .I ts h o u l db e r e l a t i v e l ys i mp l et oc r e a t ef u n c t i o n st owr a pa r o u n dsiprintf()a n dmx_puts(), b u tI ' mn o ts u r ei t ' swo r t ht h ee f f o r t . Is h o u l dp e r h a p sp o i n to u tt h a tu s i n gsiprintfa n do t h e rr o u t i n e st h a tc a nt u r nn u mb e r si n t os t r i n g su s ed i v i s i o nb y1 0t od os o , a n dy o uk n o wwh a tt h a tme a n s .An de v e ni fy o ud on o ta s ki tt oc o n v e r tn u mb e r s , i tc a l l sad o z e no rs or o u t i n e sf r o mt h es t a n d a r d l i b r a r y , wh i c ha d d sa r o u n d2 5 k bt oy o u rb i n a r y .Th i si s n ' tmu c hf o rROM, b u tf o rmu l t i b o o tt h i n g s( 2 5 6 k bma x )i tma yb e c o me p r o b l e ma t i c .Wi t ht h a ti nmi n d , I ' dl i k ey o ut ot a k eal o o ka tposprintf b yDa nPo s l u n s .Th i si sh a n d c o d e da s s e mb l yu s i n gas p e c i a l a l g o r i t h mf o rt h ed e c i ma l c o n v e r s i o n .I tma yn o tb ea sr i c hi no p t i o n sa ssiprintf(), b u ti t ' sb o t hf a s t e ra n ds ma l l e rb yav e r yl a r g e ma r g i n , s od e f i n i t e l ywo r t hc h e c k i n go u t . 4. Write cursor string.Th i swr i t e st h ec u r r e n tc u r s o rs t r i n gt op o s i t i o n( 8 0 , 1 2 0 ) .Li k ei nt h ec a s e so fwi p i n gt h es t r i n g , I ' mu s i n ga bm_puts()f u n c t i o nt h a ts wi t c h e sb e t we e nt h ec u r r e n tmo d ewr i t e r s . 19.6.2. Sprite text; Hello world! Ye s !He l l owo r l d !No w, i np r i n c i p l e , a l l y o uh a v et od oi sc a l l txt_init(), txt_init_obj()a n dt h e nobj_puts()wi t ht h e file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 175/331 28-03-13 Tonc : GBA Programming in rot13 r i g h tp a r a me t e r s , b u ta g a i nt h a t ' sj u s tb o r i n g , s oI ' l l a d ds o mei n t e r e s t i n gt h i n g sa swe l l .Th etxt_objd e mos h o wso n eo ft h et h i n g s b e s tp e r f o r me dwi t hs p r i t e s : i n d i v i d u a l l e t t e ra n i ma t i o n .Th el e t t e r so ft h ep h r a s e“ h e l l owo r l d ! ”wi l l f a l l f r o mt h et o po ft h es c r e e n , b o u n c i n gt oah a l to nt h ef l o o r( ag r e e nl i n eh a l f wa ya c r o s st h es c r e e n ) . #include <tonc.h> // === CONSTANTS & STRUCTS ============================================ #define POS0 (80<<8) #define GRAV 0x40 #define DAMP 0xD0 #define HWLEN 12 const char hwstr[]= "Hello world!"; typedef struct { u32 state; int tt; FIXED fy; FIXED fvy; } PATTERN; // === FUNCTIONS ====================================================== void pat_bounce(PATTERN *pat) { if(pat->tt <= 0) // timer's run out: play pattern { pat->fvy += GRAV; pat->fy += pat->fvy; // touched floor: bounce if(pat->fy > POS0) { // damp if we still have enough speed // otherwise kill movement if(pat->fvy > DAMP) { pat->fy= 2*POS0-pat->fy; pat->fvy= DAMP-pat->fvy; } else { pat->fy= POS0; pat->fvy= 0; } } } else // still in waiting period pat->tt--; } int main() { REG_DISPCNT= DCNT_MODE3 | DCNT_BG2 | DCNT_OBJ; irq_init(NULL); irq_add(II_VBLANK, NULL); memset16(&vid_mem[88*240], CLR_GREEN, 240); // (1) init sprite text txt_init_std(); txt_init_obj(&oam_mem[0], 0xF200, CLR_YELLOW, 0xEE); // (2) 12 px between letters gptxt->dx= 12; // (3) init sprite letters OBJ_ATTR *oe= oam_mem; obj_puts2(120-12*HWLEN/2, 8, hwstr, 0xF200, oe); int ii; file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 176/331 28-03-13 Tonc : GBA Programming in rot13 PATTERN pats[HWLEN]; for(ii=0; ii<HWLEN; ii++) { // init patterns pats[ii].state=0; pats[ii].tt= 3*ii+1; pats[ii].fy= -12<<8; pats[ii].fvy= 0; // init sprite position oe[ii].attr0 &= ~ATTR0_Y_MASK; oe[ii].attr0 |= 160; } while(1) { VBlankIntrWait(); for(ii=0; ii<HWLEN; ii++) { pat_bounce(&pats[ii]); oe[ii].attr0 &= ~ATTR0_Y_MASK; oe[ii].attr0 |= (pats[ii].fy>>8)& ATTR0_Y_MASK; } } return 0; } Ve r yl i t t l eo ft h i sc o d ei sa c t u a l l yc o n c e r n e dwi t ht h es t r i n gi t s e l f , n a me l yt h ei t e ms1 , 2a n d 3 .Th e r e ' sac a l l t otxt_init_std()f o rt h eb a s i ci n i t i a l i z a t i o na n dac a l l t ot h es p r i t e t e x ti n i t i a l i z e r , txt_init_obj().Th es e c o n da r g u me n ti st h eb a s ef o ra t t r i b u t e2( i fy o u d o n ' tr e me mb e rwh a ta t t r i b u t e2i s , s e et h ec h a p t e ro ns p r i t e sa g a i n ) ;0xF200me a n sI ' m u s i n gt h es u b p a l e t t e1 5a n ds t a r tt h ec h a r a c t e rt i l e sa tt i l e i n d e x5 1 2( b e c a u s eo ft h eb i t ma p mo d e ) .Th ef o n tc o l o rwi l l b ey e l l o w, a n do u ta ti n d e x2 5 5 .Th a t ' s2 4 0f r o mt h ep a l b a n k , 0x0E=1 4f r o mt h eu n p a c k i n ga n d1f o rt h ea c t u a l 1 b p pp i x e l s2 4 0 +1 4 +1 =2 5 5 .Af t e rt h i s c a l l , I ' ma l s os e t t i n gt h eh o r i z o n t a l p i x e l o f f s e tt o1 2t os p r e a do u tt h el e t t e r sal i t t l eb i t .Af t e r t h a t , Ij u s tc a l l obj_puts2()t os e tu pt h ef i r s tf e ws p r i t e so fOAM s ot h a tt h e ys h o w “ h e l l owo r l d ! ”c e n t e r e da tt h et o po ft h es c r e e n . Fig 19.4: txt_objd e mo . Ic o u l dh a v es t o p p e dt h e r e , b u tt h ed e moi sa c t u a l l yj u s tb e g i n n i n g .Th et h i n ga b o u tu s i n g s p r i t e sa sg l y p h si st h a tt h e yc a ns t i l l act a sn o r ma l s p r i t e s ;obj_puts()j u s ts e t st h e mu pt ou s el e t t e r si n s t e a do fg r a p h i c st h a ta r e mo r es p r i t e l i k e . Bouncy, bouncy, bouncy Th eg o a l h e r ei st ol e tt h el e t t e r sd r o pf r o mt h et o po ft h es c r e e n , t h eb o u n c eu pa g a i nwh e ni th i t saf l o o r , b u twi t hal i t t l el e s ss p e e dt h a n b e f o r ed u et of r i c t i o na n dwh a tn o t .Ph y s i c a l l y , t h ef a l l i n gp a r ti sd o n eu s i n gac o n s t a n ta c c e l e r a t i o n , g.Ac c e l e r a t i o ni st h ec h a n g ei n v e l o c i t y , s ot h ev e l o c i t yi sl i n e a r ;v e l o c i t yi st h ec h a n g ei np o s i t i o n , s ot h eh e i g h ti sp a r a b o l i c .Att h eb o u n c e , wed oa ninelastic collision; i no t h e rwo r d s , o n ewh e r ee n e r g yi sl o s t .I np r i n c i p l e , t h i swo u l dme a nt h a tt h ed i f f e r e n c eb e t we e nt h es q u a r e so ft h ev e l o c i t i e sb e f o r e 2 2 a n da f t e rt h ec o l l i s i o nd i f f e rb yac o n s t a n t(| v out| -| v in| =Q) .Ho we v e r , t h i swo u l dr e q u i r eas q u a r er o o tt of i n dt h en e wv e l o c i t y , a n dI d o n ' tc a r ef o rt h a tr i g h tn o ws oI ' mj u s tg o i n gt os c r a pt h es q u a r e sh e r e .I ' ms u r et h e r ea r es i t u a t i o n swh e r et h i si sa c t u a l l yq u i t ev a l i d :P.Asaf u r t h e rs i mp l i f i c a t i o n , I ' md o i n gaf i r s t o r d e ri n t e g r a t i o nf o rt h ep o s i t i o n .Wi t ht h i s , t h eb a s i cc o d ef o rmo v e me n tb e c o me sv e r y s i mp l e // 1D inelastic reflections // y, vy, ay: position, velocity, acceleration. // Q: inelastic collision coefficient. vy += ay; y += vy; if(y>ymay) // collision { if((ABS(vy)>Q) { vy= -(vy-SGN(vy)*Q); // lower speed, switch direction y= 2*ymay-y; // Mirror y at r: y= r-(y-r)= 2r-y } else // too slow: stop at ymay { vy= 0; y= ymay; } file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 177/331 28-03-13 Tonc : GBA Programming in rot13 } Th i sc o u l db er e p l a c e db yt h ef o l l o wi n g , mo r ea c c u r a t ec o d e , u s i n gs e c o n d o r d e ri n t e g r a t i o na n d‘ p r o p e r ’r e c o i l , b u ty o uh a r d l yn o t i c e a n y t h i n gf r o mt h ei mp r o v e di n t e g r a t i o n .Ia c t u a l l yp r e f e rt h el o o ko ft h es i mp l e , l i n e a rr e c o i l o v e rt h es q u a r er o o tt h o u g h . // accelerate k= vx+GRAV; // Trapezium integration rule: // x[i+1]= x[i] + (v[i]+v[i+1])/2; x += (vx+k)/2; vx= k; if(x>xmax) // collision { if(vx*vx > Q2) { vx= -Sqrt(vx*vx-Q2); x= 2*xmax-x; } else { vx= 0; x= xmax; } } 19.6.3. Map text : colors and borders Ne x tu pi st h ef i r s to ft woma pt e x td e mo s .Th eo f f i c i a l n a mef o rwh a tIc a l l ar e g u l a rb a c k g r o u n di s“ t e x tb a c k g r o u n d ” , a n dt h e y ' r e c a l l e dt h a tf o rar e a s o n : i nmo s tc a s e swh e nt h e r ei st e x t , i t ' sd o n eu s i n gr e g u l a rb a c k g r o u n d s .Ofc o u r s e , i nmo s tc a s e se v e r y t h i n ge l s ei s also d o n ewi t ht h o s e , s os t r i c t l ys p e a k i n ga s s o c i a t i n gt h e mwi t h“ t e x t ”i sami s n o me r , b u twe ' l l l e tt h a to n es l i d ef o rt o d a y .Th ef i r s td e mo i sa b o u th o wy o uc a nu s et h et e x tf u n c t i o n sf o rav a r i e t yo fe f f e c t s .Ap a r tf r o ms i mp l ys h o wi n gt e x t( b o r i n g ) , y o u ' l l s e ep a l e t t es wa p p i n g a n df r a mi n gt e x t , a n dh o wy o uc a ne a s i l yu s ed i f f e r e n tf o n t sa n db o r d e r sc o n c u r r e n t l y .Be c a u s eo ft h ewa yI ' v ed e s i g n e dmyf u n c t i o n s , a l l t h i st a k e si sac h a n g ei nap a r a me t e r .Co o l h u h . Th ed e mowi l l a l s of e a t u r ea d d i n gs h a d i n gt oamo n o c h r o mef o n t , a n da d d i n ga no p a q u eb a c k g r o u n df o ri t .No w, t h ewa yI ' mg o i n g a b o u tt h i swi l l p r o b a b l yr e s e r v emeap l a c ei nt h eCo mp u t e rSc i e n c eHe l l , b u t , we l l , t h ec o o l n e s so ft h et r i c k swi l l p r o b a b l yk e e pmef r o m b u r n i n gu pt h e r e . #include <tonc.h> #include "border.h" // === CONSTANTS & STRUCTS ============================================ #define TID_FRAME0 #define TID_FRAME1 #define TID_FONT #define TID_FONT2 #define TID_FONT3 #define TXT_PID_SHADE #define TXT_PID_BG 96 105 0 128 256 0xEE 0x88 // === FUNCTIONS ====================================================== void init() { int ii; REG_DISPCNT= DCNT_MODE0 | DCNT_BG0; irq_init(NULL); irq_add(II_VBLANK, NULL); txt_init_std(); // (1a) Basic se text initialization txt_init_se(0, BG_CBB(0) | BG_SBB(31), 0x1000, CLR_RED, 0x0E); // (1b) again, with a twist txt_init_se(0, BG_CBB(0) | BG_SBB(31), 0xF000|TID_FONT2, CLR_YELLOW | (CLR_MAG<<16), TXT_PID_SHADE); // (1c) and once more, with feeling! txt_init_se(0, BG_CBB(0) | BG_SBB(31), 0xE000|TID_FONT3, 0, TXT_PID_SHADE); u32 *pwd= (u32*)&tile_mem[0][TID_FONT3]; for(ii=0; ii<96*8; ii++) *pwd++ |= quad8(TXT_PID_BG); // extra border initialisation file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 178/331 28-03-13 Tonc : GBA Programming in rot13 memcpy32(pal_bg_mem, borderPal, borderPalLen/4); memcpy32(&tile_mem[0][TID_FRAME0], borderTiles, borderTilesLen/4); // (2) overwrite /\ [] `% ^_ to use border tiles // / ^ \ // [ # ] // ` _ ' const u8 bdr_lut[9]= "/^\\[#]`_\'"; for(ii=0; ii<9; ii++) gptxt->chars[bdr_lut[ii]]= TID_FRAME0+ii; // (3) set some extra colors pal_bg_mem[0x1F]= CLR_RED; pal_bg_mem[0x2F]= CLR_GREEN; pal_bg_mem[0x3F]= CLR_BLUE; pal_bg_mem[0xE8]= pal_bg_mem[0x08]; // bg pal_bg_mem[0xEE]= CLR_ORANGE; // shadow pal_bg_mem[0xEF]= pal_bg_mem[0x0F]; // text } void txt_se_frame(int l, int t, int r, int b, u16 se0) { int ix, iy; u8 *lut= gptxt->chars; u16 *pse= (u16*)gptxt->dst0; pse += t*32 + l; r -= (l+1); b -= (t+1); // corners pse[32*0 + 0] = se0+lut['/']; pse[32*0 + r] = se0+lut['\\']; pse[32*b + 0] = se0+lut['`']; pse[32*b + r] = se0+lut['\'']; // horizontal for(ix=1; ix<r; ix++) { pse[32*0+ix]= se0+lut['^']; pse[32*b+ix]= se0+lut['_']; } // vertical + inside pse += 32; for(iy=1; iy<b; iy++) { pse[0]= se0+lut['[']; pse[r]= se0+lut[']']; for(ix=1; ix<r; ix++) pse[ix]= se0+lut['#']; pse += 32; } } int main() { init(); // (4a) red, green, blue text se_puts(8, 16, "bank 1:\n red", 0x1000); se_puts(8, 40, "bank 2:\n green", 0x2000); se_puts(8, 72, "bank 3:\n blue", 0x3000); // (4b) yellow text with magenta shadow se_puts(8, 96, "bank 15:\n yellow, \nwith mag \nshadow", 0xF000|TID_FONT2); // (5a) framed text, v1 txt_se_frame(10, 2, 29, 9, 0); se_puts( 88, 24, "frame 0:", 0); se_puts(104, 32, "/^\\[#]`_'", 0); se_puts( 88, 40, "bank 0:\n basic text,\n transparent bg", 0); // (5b) framed text, v2 txt_se_frame(10, 11, 29, 18, TID_FRAME1-TID_FRAME0); file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 179/331 28-03-13 Tonc : GBA Programming in rot13 se_puts( 88, 96, "frame 1:", 0xE000|TID_FONT3); se_puts(104, 104, "/^\\[#]`_'", 9); se_puts( 88, 112, "bank 14:\n shaded text\n opaque bg", 0xE000|TID_FONT3); while(1) VBlankIntrWait(); return 0; } Fig 19.5b: a c c o mpa n yi n gt i l e s e t . Fig 19.5a: Fi r s tma pt e x td e mo . Code rundown Fi g1 9 . 5s h o wswh a tt h i sc o d ep r o d u c e s .Al l t h ea c t u a l t e x td r a wi n gi sd o n ei nt h ema i nf u n c t i o n , a n dI ' l l g ob yt h e mo n eb yo n e .Th e f i r s tt h r e et h i n g sa r er e d , g r e e na n db l u et e x t( p o i n t4 a ) , d o n eu s i n gp a l e t t es wa p p i n g .I ' v el o a d e du pr e d , g r e e na n db l u et op a l e t t ei n d i c e s 0x1F, 0x2Fa n d0x3F( p o i n t3 ) , a n dc a ns wi t c hb e t we e nt h e mwi t ht h el a s tp a r a me t e ro fse_puts(), wh i c hy o uwi l l r e c a l l i sa d d e d t oe a c ho ft h es c r e e ne n t r i e s .Th ev a l u e s0x1000, 0x2000a n d0x3000i n d i c a t et h a twe ' l l u s ep a l e t t eb a n k s1 , 2a n d3 , r e s p e c t i v e l y . I fy o ul o o kc l o s e l y , y o u ' l l s e et h a tf o u r t ht e x t( p o i n t4 b )i sy e l l o wwi t hama g e n t a( n oi t ' sn o tp i n k , i t ' smagenta)s h a d i n go nt h er i g h t e d g eo fe a c hl e t t e r .Atl e a s tp a r to ft h i si sd o n ewi t ht h ese0p a r a me t e r , wh i c hi sn o w0xF080.Th er e a s o ni t ' ss h a d e di sb e c a u s eo f t h el a s tp a r t : I ' ma c t u a l l yu s i n gas l i g h t l yd i f f e r e n tf o n t , o n et h a ts t a r t sa tt i l e1 2 8 .I ' l l r e p e a t , t h er e a s o nIc a nd oa l l t h i swi t ht h es a me f u n c t i o ni sb e c a u s eo ft h a to f f s e tp a r a me t e ro fse_puts(). Po i n t s( 5 a )a n d( 5 b )a r ef o rf r a mi n g , a n dt h et e x ti n s i d ei t .Th ef u n c t i o ntxt_se_frame()d r a wsmyb o r d e r .I tt a k e sar e c t a n g l e a si t si n p u t , a n dd r a wsaf r a meo ni t .No t et h a tt h ef r a mei n c l u d e st h et o p l e f t , b u te x c l u d e st h eb o t t o mr i g h t .Ag a i n , Ih a v eo n ee x t r a se0p a r a me t e ra sa no f f s e t .Th i si sh o wt h es e c o n db o r d e ri sa c t u a l l yd o n e ;Ij u s to f f s e tt h et h i n gb yt h ed i f f e r e n c eb e t we e nb o r d e r t i l e s . Th eb o r d e r st h e ms e l v e sa r ea c t u a l l yd r a wnp r e t t ymu c ha si ft h e ywe r et e x t .I ninit()I ' v er e a s s i g n e dn i n ec h a r a c t e r si nt h e c h a r a c t e rl u tt ou s et h et i l ei n d i c e sf o rt h ep r i ma r yb o r d e rt i l e s e t( p o i n t2 ) .Th e r ei sn op a r t i c u l a rr e a s o nI ' md o i n gt h i s , o t h e rt h a nt h e me r ef a c tt h a tIc a n .J u s ti l l u s t r a t i n gt h et h i n g sy o uc a nd owi t hat e x twr i t e ra n ds o mec l e v e rl u tma n i p u l a t i o n . Th et e x t si n s i d et h ef r a me sa r ea ni n t e r e s t i n gs t o r ya swe l l .Asy o uc a ns e ef r o mt h et e x ti nt h ef i r s tf r a me , t h es t a n d a r dt e x td o e s n ' t q u i t ewo r k .Th ep r o b l e mi st h a tt h ema i nt i l e s e tI ' mu s i n gi st r a n s p a r e n t , b u tt h ef r a me ' sb a c k g r o u n di s n ' t .Mi xt h et woa n dt h e y ' l l c l a s h . Soh o wt os o l v et h a t ?We l l , y o uc r e a t eanother f o n t , o n et h a td o e sn o th a v e0a si t sb a c k g r o u n dc o l o r .Th e r ea r ean u mb e ro fwa y st o d ot h a t , o n eo ft h e mb e i n ga d d i n g1 <<3 1t ot h eb i t u n p a c k i n gf l a g .Bu tI ' mo p t i n gf o ra n o t h e rme t h o d , wh i c hI ' l l g e ti n t ol a t e r .No t et h a t wh a t e v e rI ' md o i n g , i td o e swo r k : t h et e x ti nt h es e c o n df r a mei so p a q u ea f t e ra l l .No t et h a tI ' mwr i t i n gt h a tt e x tu s i n gp a l b a n k1 4 , a n d a mn o wu s i n gathird t i l e s e tf o rt h ef o n t s . No w, u pt ot h i sp o i n ti t ' sa l l b e e np r e t t ye a s y .Th eu s a g eo fse_puts()a n dtxt_se_frame()Ime a n .Ih o p ey o uu n d e r s t o o da l l o f t h ea b o v e , b e c a u s et h er e s ti sg o i n gt ob ep r e t t yi n t e r e s t i n g .No tq u i t e“ o hg o d , o hg o d , we ' r ea l l g o n n ad i e ” i n t e r e s t i n g , b u ts t i l l ami t e h a i r yf o rs o me . Bit fiddling fun I ' v ei n d i c a t e dt h a tI ' mu s i n gt h r e ed i f f e r e n tf o n t s .Bu ti fy o us t u d yt h ec o d e , y o uwi l l f i n dn ot r a c eo ff o n td e f i n i t i o n so rc o p i e s .Th a t ' s b e c a u s et h e r ea r en o n e : i t ' sa l l b a s e do nt h es a meb i t p a c k e df o n tIs h o we de a r l i e r .Al s o , t h ema t h e ma t i c a l l yi n c l i n e dwi l l h a v en o t i c e d t h a tb i t p a c k i n ga1 b p pf o n twi l l r e s u l ti nt woc o l o r s .Th a t ' swh a t1 b p pmeans, a f t e ra l l .Bu tIh a v eab a c k g r o u n dc o l o r , af o r e g r o u n d c o l o r , a n ds h a d i n g ;t h a t ' st h r e e .Fu r t h e r mo r e , t h e r ed o e s n ' ts e e mt ob ea n yc o d et h a td o e st h es h a d i n g .Th i sa l l l e a d st oo n es i mp l e q u e s t i o n , n a me l y : wh a tt h eh e l l a mId o i n g ? We l l …t h i s : #define TID_FONT #define TID_FONT2 #define TID_FONT3 #define TXT_PID_SHADE #define TXT_PID_BG 0 128 256 0xEE 0x88 file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 180/331 28-03-13 Tonc : GBA Programming in rot13 // (1a) Basic se text initialization txt_init_se(0, BG_CBB(0) | BG_SBB(31), 0x1000, CLR_RED, 0x0E); // (1b) again, with a twist txt_init_se(0, BG_CBB(0) | BG_SBB(31), 0xF000|TID_FONT2, CLR_YELLOW | (CLR_MAG<<16), TXT_PID_SHADE); // (1c) and once more, with feeling! txt_init_se(0, BG_CBB(0) | BG_SBB(31), 0xE000|TID_FONT3, 0, TXT_PID_SHADE); u32 *pwd= (u32*)&tile_mem[0][TID_FONT3]; for(ii=0; ii<96*8; ii++) *pwd++ |= quad8(TXT_PID_BG); Th e s es i xs t a t e me n t ss e tu pt h et h r e ef o n t s , c o mp l e t ewi t hs h a d i n ga n do p a c i t y .Th ef i r s to n es e t su pt h es t a n d a r df o n t , i nc h a r b l o c k0 , s c r e e n b l o c k3 1 , p a l b a n k1a n du s i n g0x0Ef o rt h eb i t u n p a c k i n go f f s e t , s ot h a tt h et e x tc o l o ri sa t0x1F.We ' v es e e nt h es a met h i n g wi t ht h eo b j e c tt e x t . Th es e c o n dc a l l t otxt_se_init()s e t su pt h es e c o n df o n ts e t , t h eo n ewi t hs h a d i n g .se0 bit val 7 654 3210 i n d i c a t e st h eu s eo fp a l b a n k1 5a n dt os t a r ta t1 2 8 , b u tt h ei mp o r t a n ts t u f fh a p p e n si nt h eclrs .......0 a n dbasep a r a me t e r s .Th e r ea r en o wt woc o l o r si nclrs, y e l l o wa n dma g e n t a .Th el o we r 0 0 h a l f wo r dwi l l b et h et e x tc o l o r , a n dt h eu p p e rh a l f wo r dt h es h a d i n gc o l o r . .....EF. 1 1 Th ea c t u a l s h a d i n gh a p p e n sb e c a u s eo ft h ev a l u eo fbase, wh i c hi s0xEE, a n dt h ewa yt h e 1 ....EF.. 2 wh o l eb i t u n p a c k i n gr o u t i n ewo r k s .Th eo f f s e ti sa d d e dt oe a c h‘ o n ’ b i ti nt h ep a c k e df o n t , g i v i n g ...EF... 3 1 0xEF, wh i c hi st h e nORr e dt ot h ec u r r e n two r dwi t ht h ea p p r o p r i a t es h i f t .Be c a u s ewe ' r ed e a l i n g ...0.... 4 0 wi t ha4 b p pf o n t , t h er e s u l twi l l a c t u a l l yo v e r f l o wi n t ot h en e x tn y b b l e .No w, i ft h en e x tb i ti sa l s o o n , i t ' l l OR0xEFwi t ht h eo v e r f l o wv a l u eo f0x0E.As0xF| 0xEi sj u s t0xF, i t ' sa si ft h e 0 ..0..... 5 o v e r f l o wn e v e rh a p p e n e d .Bu t , i ft h en e x tb i twa soff, t h ev a l u ef o rt h a tp i x e l wo u l db e0xE. 6 1 EF...... La s t l y , i ft h e r ewa sn oo v e r f l o wf o raz e r os o u r c eb i t , t h er e s u l ti sa0 .An dn o wweh a v et h et h r e e 7 0 0....... p o s s i b l ev a l u e s : 0( b a c k g r o u n d ) , 1 4( s h a d e )a n d1 5( t e x t ) .Ta b l e1 9 . 2s h o wst h ep r o c e d u r emo r e OR: E F 0 E F F F 0 g r a p h i c a l l y .Th eb i t sf o rt h es o u r c eb y t ea r eo nt h el e f t , a n dt h eb i t u n p a c k e dr e s u l tf o re a c hb i to n Table 19.2: b i t u n pa c ki ngwi t h t h eg r i do nt h er i g h t , i nt h ec o r r e c tp o s i t i o n .Th e s ea r et h e nORe dt o g e t h e rf o rt h ee n dr e s u l t .Fo r w i t h b a s e 0 x E E . 0x46t h a t ' db et h ewo r d0xEF0EFFF0.On ewo r di so n er o wo f8p i x e l si na4 b p pt i l e , a n d b e c a u s el o we rn y b b l e sa r et h el e f t mo s tp i x e l s , t h es h a d ewi l l b eo nt h er i g h to ft h ec h a r a c t e re v e n t h o u g hi tu s e st h emo r es i g n i f i c a n tb i t s . Th eb a s e0xEEi so n eo fma n yv a l u e sf o rwh i c ht h i st r i c kwo r k s .Th ek e yt h i n gi st h a tt h eh i g hn y b b l emu s tb ec o mp l e t e l y o v e r wr i t t e nb yt h el o we rn y b b l e +1 .An yn u mb e rwi t he q u a l a n de v e nn y b b l e swi l l wo r k . No w, I ' l l b et h ef i r s tt oa d mi tt h a tt h i si ss o me t h i n go fah a c k .Al o to ft h i n g sh a v et oc o met o g e t h e rf o ri tt owo r k .Th ewo r d s i z e mu s tb ea b l et of i tawh o l et i l er o w, b o t hp a c k e da n du n p a c k e dd a t amu s tb el i t t l e e n d i a ni nb o t hb i ta n db y t eo r d e r , a n dt h eu n p a c k i n g r o u t i n emu s ta c t u a l l ya l l o wo v e r f l o w, a n dp r o b a b l yaf e wo t h e rt h i n g st h a te s c a p emer i g h tn o w.Al l o ft h e s ec o n d i t i o n sa r es a t i s f i e do n t h eGBA, b u tId o u b tv e r ymu c hi fy o uc a nu s et h et r i c ko no t h e rs y s t e ms .Th e r ea r eo t h e rwa y so fa p p l y i n gs h a d i n g , o fc o u r s e , b e t t e r o n e sa tt h a t .I t ' sj u s ts od e l i c i o u s l yn a s t yt h a tIc a n ' tr e s i s tu s i n gi t . Th ef i n a l txt_se_init()wo r kp r e t t ymu c hi nt h es a mewa ya st h es e c o n do n e : s h a d i n gt h r o u g ho v e r f l o w.Wh a ti td o e s n ' td oi s ma k et h et i l e so p a q u e .Wh i l ei t ' sp o s s i b l et od ot h a twi t hBi t Un p a c k , y o uc a n ' th a v et h a tand s h a d i n gwi t ho n ec a l l , t h a ts i mp l yd o e s n ' t wo r k .Bu tt h e r ea r eo t h e rwa y s .Al l wer e a l l yn e e df o rt h et i l e st ob eo p a q u ei ss o mev a l u eo t h e rt h a nz e r of o ri tf o rt h eb a c k g r o u n d p i x e l s .We l l , t h a t ' se a s i l yd o n e : j u s to f f s e t( a d do rOR)e v e r y t h i n gb yan u mb e r .I nt h i sc a s eIc a n ' ta d dav a l u eb e c a u s et h et e x tv a l u ei s a l r e a d ya tma x i mu m, s oI ' l l u s eORh e r e .Th ev a l u eI ' l l ORwi t hi s0x88888888, wh i c hd o e s n ' tc h a n g et h et e x to rs h a d i n g , b u ts e t st h e b a c k g r o u n dp i x e l st ou s e8, s owe ' v eg o twh a twewa n t e d . An dt h a t , a st h e ys a y , i sh o wwed ot h a t .Ora tl e a s th o wI d ot h a t .I ft h ea b o v es e e msl i k emu mb o j u mb ot oy o u , n oo n e ' sf o r c i n gy o ut o d oi ti nt h es a mewa y .Yo uc a na l wa y st a k et h ee a s ywa yo u ta n di n c l u d emu l t i p l ef o n t si n t ot h ep r o g r a mr a t h e rt h a nc o n s t r u c tt h e m f r o mwh a ty o uh a v e .I ' mj u s ts h o wi n gwh a tc a nb ed o n ewi t hal i t t l ec r e a t i n gc o d i n g . 19.6.4. Map text : profiling Th el a s tt h i n gI ' l l s h o wy o ui sa ne a s yo n e , b u ts o me t h i n gt h a tmi g h tc o mei nh a n d ywh e ni t ' st i met oo p t i mi z eaf e wt h i n g s .I nc a s ey o u h a v e n ' tn o t i c e d , d e b u g g i n gGBAp r o g r a msi s n ' tq u i t ea se a s ya sd e b u g g i n gPCp r o g r a ms .Th e r ei st h ep o s s i b i l i t yo fd e b u g g i n gwi t h I n s i g h ta n dt h eGDB( t h eGCCd e b u g g e r ) , b u te v e nt h e nt h i n g sa r ei f f y , o rs oIh e a r .We l l , n o wt h a ty o uc a np r i n ty o u ro wnt e x t , y o u c a na tl e a s td os o me t h i n go ft h a ts o r t .Wr i t eo u td i a g n o s t i cme s s a g e sa n dt h el i k e . Bu tt h a t ' sn o twh a tI ' mg o i n gt os h o wy o un o w.Th el a s td e mowi l l s h o wy o uh o wt od os o me t h i n gt h a tu s u a l l yc o me safter d e b u g g i n g : p r o f i l i n g .Pr o f i l i n gt e l l sy o uh o wmu c ht i mei ss p e n td o i n gwh a t , s oy o uc a nt e l l wh a two u l db et h eb e s tp l a c e st ot r yt o o p t i mi z e .Wh a tI ' l l s h o wy o ui sas i mp l ewa yo fg e t t i n gt h et i mes p e n ti n s i d eaf u n c t i o n .St u f fl i k et h a ti sg o o dt ok n o w, e s p e c i a l l yo na p l a t f o r ml i k et h i swh e r ey o us t i l l h a v et owo r r ya b o u tt h i n g sl i k es p e e da n de f f i c i e n c ya n do t h e rs i l l ys t u f fl i k et h a t . Th en e x td e mowi l l c l o c kf i v ed i f f e r e n twa y so fc o p y i n gd a t a , i nt h i sc a s eamo d e4b i t ma pf r o mEWRAM ( myc o d ei ss e tu pf o r mu l t i b o o tb yd e f a u l t , wh i c hme a n se v e r y t h i n gg o e si nEWRAM r a t h e rt h a nROM)t oVRAM.Th eme t h o d sa r e : file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 181/331 28-03-13 Tonc : GBA Programming in rot13 u16 array.Co p yi n1 6 b i t( h a l f wo r d )c h u n k s .Pr o b a b l yt h eo n ey o u ' l l s e emo s ti no t h e rt u t o r i a l s , b u tn o th e r e .Wi t hr e a s o n , a s we ' l l s e ei nami n u t e . u32 array.Co p yi n3 2 b i t( wo r d )c h u n k s . memcpy().Th es t a n d a r dCc o p yr o u t i n e , t h eo n eI ' mu s i n gi nt h ee a r l i e rd e mo s .We l l , n o wa d a y sIa m. memcpy32().Ho meg r o wna s s e mb l y , e x p l a i n e di nd e t a i l h e r e .Ba s i c a l l yd o e swh a tCpuFastSet()d o e s , o n l ywi t h o u t t h er e s t r i c t i o nt h a tt h en u mb e ro fwo r d smu s tb eamu l t i p l eo f8 . dma_memcpy().Co p yv i a3 2 b i tDMA. #include <string.h> #include <stdio.h> #include <stdlib.h> #include <tonc.h> #include "gba_pic.h" // === CONSTANTS & STRUCTS ============================================ int gtimes[5]; const char *strs[5]= { "u16 array", "u32 array", "memcpy", "memcpy32", "DMA32" }; // === FUNCTIONS ====================================================== // copy via u16 array void test_0(u16 *dst, const u16 *src, u32 len) { u32 ii; profile_start(); for(ii=0; ii<len/2; ii++) dst[ii]= src[ii]; gtimes[0]= profile_stop(); } // copy via u32 array void test_1(u32 *dst, const u32 *src, u32 len) { u32 ii; profile_start(); for(ii=0; ii<len/4; ii++) dst[ii]= src[ii]; gtimes[1]= profile_stop(); } // copy via memcpy void test_2(void *dst, const void *src, u32 len) { profile_start(); memcpy(dst, src, len); gtimes[2]= profile_stop(); } // copy via my own memcpy32 void test_3(void *dst, const void *src, u32 len) { profile_start(); memcpy32(dst, src, len/4); gtimes[3]= profile_stop(); } // copy using DMA void test_4(void *dst, const void *src, u32 len) { profile_start(); dma3_cpy(dst, src, len); gtimes[4]= profile_stop(); } int main() { REG_DISPCNT= DCNT_MODE0 | DCNT_BG0; file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 182/331 28-03-13 Tonc : GBA Programming in rot13 irq_init(NULL); irq_add(II_VBLANK, NULL); test_0((u16*)vid_mem, (const u16*)gba_picBitmap, gba_picBitmapLen); test_1((u32*)vid_mem, (const u32*)gba_picBitmap, gba_picBitmapLen); test_2(vid_mem, gba_picBitmap, gba_picBitmapLen); test_3(vid_mem, gba_picBitmap, gba_picBitmapLen); test_4(vid_mem, gba_picBitmap, gba_picBitmapLen); // clear the screenblock I'm about to use memset32(&se_mem[7], 0, SBB_SIZE/4); // init map text txt_init_std(); txt_init_se(0, BG_SBB(7), 0, CLR_YELLOW, 0); // print results int ii; char str[32]; for(ii=0; ii<5; ii++) { siprintf(str, "%12s %6d", strs[ii], gtimes[ii]); se_puts(8, 8+8*ii, str, 0); } while(1) VBlankIntrWait(); return 0; } Th ec o d es h o u l db es e l f e x p l a n a t o r y .Ih a v ef i v ef u n c t i o n sf o rt h et h i n g sIwa n tt op r o f i l e .Ic h o s es e p a r a t ef u n c t i o n sb e c a u s et h e nI k n o wo p t i mi s a t i o nwi l l n o ti n t e r f e r e( i ts o me t i me smo v e sc o d ea r o u n d ) .Af t e rr u n n i n gt h e s ef u n c t i o n s , Is e t u pmyt e x tf u n c t i o n sa n d p r i n to u tt h er e s u l t s . Th ep r o f i l i n gi t s e l fu s e st woma c r o s , profile_start()a n dprofile_stop().Th e s ec a nb ef o u n di ncore.ho ft o n c l i b . Wh a tt h ema c r o sd oi ss t a r ta n ds t o pt i me r s2a n d3 , a n dt h e nr e t u r nt h et i mei nb e t we e nt h ec a l l s .Th i sd o e sme a nt h a tt h ec o d ey o u ' r e p r o f i l i n gc a n n o tu s et h o s et i me r s . INLINE void profile_start() { REG_TM2D= 0; REG_TM3D= 0; REG_TM2CNT= 0; REG_TM3CNT= 0; REG_TM3CNT= TM_ENABLE | TM_CASCADE; REG_TM2CNT= TM_ENABLE; } INLINE u32 profile_stop() { REG_TM2CNT= 0; return (REG_TM3D<<16)|REG_TM2D; } Fig 19.6a: txt_se2o nVBA. hardware Fig 19.6b: txt_se2o nn o $ g b a . vba no$gba vba err no$ err u16 array 6 1 4 5 7 14 9 9 4 4 06 1 4 5 7 1 1 8 . 7 3 0 . 0 0 u32 array 2 8 9 8 2 52 3 0 3 8 32 8 8 0 9 8 2 0 . 5 1 0 . 6 0 memcpy 1 9 5 1 5 61 6 1 1 1 91 9 4 5 1 9 1 7 . 4 4 0 . 3 3 file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 183/331 28-03-13 Tonc : GBA Programming in rot13 memcpy32 DMA32 8 6 8 1 67 9 3 3 6 8 5 3 2 9 7 6 8 8 9 8 . 6 2 1 . 7 1 2 5 0 7 6 8 8 8 9 9 . 6 7 0 . 0 0 Table 19.3: t i mi n gr e s u l t sf o rh a r d wa r e , v b aa n dn o $ g b a . Fi g1 9 . 6s h o wst h et i mi n gr e s u l t s , a sr u ni nVi s u a l Bo yAd v a n c ea n dn o $ g b a .No t et h a tt h e ya r en o tq u i t et h es a me .Soy o ud owh a ty o u s h o u l da l wa y sd owh e nt woo p i n i o n sd i f f e r : g e tat h i r do n e .I nt h i sc a s e , I ' l l u s et h eo n l yo n et h a tr e a l l yma t t e r s , n a me l yh a r d wa r e .Yo u c a ns e eac o mp a r i s o no ft h et h r e ei nt a b l e1 9 . 3 , wh i c hwi l l t e l l y o ut h a tn o $ g b ai sv e r ya c c u r a t ei ni t st i mi n g , b u tVBAn o ts omu c h .I g u e s sy o uc a ns t i l l u s ei tt og e ta ne s t i ma t eo rr e l a t i v et i mi n g s , b u tt r u ea c c u r a c ywi l l n o tb ef o u n dt h e r e .Fo rt h a ty o un e e dh a r d wa r eo r n o $ g b a . Ab o u tt h en u mb e r st h e ms e l v e s .Th es p r e a di sa b o u taf a c t o r9 , wh i c hi sq u i t eal o t .No n eo ft h et e c h n i q u e ss h o wnh e r ea r e p a r t i c u l a r l yh a r dt ou n d e r s t a n d , a n dd a t ac o p y i n gi ss o me t h i n gt h a ty o uc o u l ds p e n dal o to ft i med o i n g , s omi g h ta swe l l t a k ea d v a n t a g e o ft h ef a s t e ro n e sf r o mt h eg e tg o . Mo s to ft h et u t o r i a l c o d ea n dp r o b a b l yal o to fd e moc o d ey o uc a nf i n do u tt h e r eu s e st h eu 1 6 a r r a yme t h o do fc o p y i n g ;p r e s u ma b l y b e c a u s eb y t e c o p i e sa r eu n a v a i l a b l ef o rc e r t a i ns e c t i o n s .Bu ta sy o uc a ns e e , u16 copies are more than twice as slow as u32 copies!Gr a n t e d , i ti sn o tt h es l o we s tme t h o do fc o p y i n gd a t a , b u tn o tb ymu c h( u s i n gu 1 6l o o pv a r i a b l e s–a l s oac o mmo no c c u r e n c e – wo u l db es l o we rb ya b o u t2 0 %;t r yi ta n dy o u ' l l s e e ) .Th eGBAi sa3 2 b i tma c h i n e .I tlikes 3 2 b i td a t a , a n di t si n s t r u c t i o ns e t sa r eb e t t e r a td e a l i n gwi t h3 2 b i tc h u n k s .Le tg oo ft h eu 1 6f e t i s hy o uma yh a v ep i c k e du pe l s e wh e r e .Us ewo r d s i z e dd a t ai fy o uc a n , t h eo t h e r s o n l yi fy o uh a v et o .Th a ts a i d , d owa t c hy o u rd a t aa l i g n me n t !u 8o ru 1 6a r r a y sa r e n ' ta l wa y swo r d a l i g n e d , wh i c hwi l l c a u s et r o u b l ewi t h c a s t i n g . GCC and waitstates vs timing results Gi v i n ge x a c tt i mi n gr e s u l t si st r i c k yd u et oan u mb e ro ff a c t o r s .Fi r s t , o nt h eh a r d wa r es i d et h e r ea r ed i f f e r e n tme mo r y s e c t i o n swi t hd i f f e r e n twa i ts t a t e st h a tc o mp l i c a t et h i n g su n l e s sy o us i td o wn , r e a dt h ea s s e mb l ya n da d du pt h ec y c l e c o u n t s o ft h ei n s t r u c t i o n s .Th i si sah o r r i b l ej o b , t r u s tme .Th es e c o n dp r o b l e mi st h a tGCCh a s n ' tr e a c h e dt h et h e o r e t i c a l o p t i mu mf o r t h i sc o d ey e t , s ot h er e s u l t st e n dt ov a r ywi t hn e wr e l e a s e s .Wh a ty o us e ea b o v ei sag o o di n d i c a t i o n , b u ty o u rmi l e a g ema y v a r y . Th e r ea r ean u mb e ro ff a s twa y so fc o p y i n gl a r g ec h u n k so fd a t a .Fa s t e rt h a nwr i t i n gy o u ro wns i mp l el o o pt h a ti s .Co mmo no n e sa r e t h es t a n d a r dmemcpy(), wh i c hi sa v a i l a b l ef o ra n yp l a t f o r m, a n dt wome t h o d st h a ta r eGBAs p e c i f i c : t h eCpuFastSet()BI OSc a l l ( o rmyo wnv e r s i o nmemcpy32()a n dDMA.Th ef i r s tt worequire wo r d a l i g n me n t ;DMAme r e l ywo r k sb e t t e rwi t hi t .Th e p e r f o r ma n c eo fmemcpy()i sa c t u a l l yn o tt o os h a b b y , a n dt h ef a c tt h a ti t ' sa v a i l a b l ee v e r y wh e r eme a n st h a ti t ' sag o o dp l a c et os t a r t . Th eo t h e r sa r ef a s t e r , b u tc o mea tac o s t : memcpy32()i sh a n dwr i t t e na s s e mb l y ;CpuFastSet()r e q u i r e sawo r d c o u n td i v i s i b l e b y8 , a n dDMAl o c k su pt h eCPU, wh i c hc a ni n t e r f e r ewi t hi n t e r r u p t s .Yo uwo u l dd owe l l t or e me mb e rt h e s et h i n g swh e ny o uf i n dy o u n e e dal i t t l emo r es p e e d . 19.7. Other considerations Th e s ec o u p l eo ff u n c t i o n sb a r e l ys c r a t c ht h es u r f a c ea sf a ra st e x ts y s t e msa r ec o n c e r n e d .Yo uc a nh a v el a r g e rf o n t s , c o l o r e df o n t s , p r o p e rs h a d i n g , v a r i a b l ec h a r a c t e rwi d t h s , a n dmo r e .Ea c ho ft h e s ec a na p p l yt oe a c ho ft h emo d e s , wi t he x t r af o r ma t t i n gf o rt e x t j u s t i f i c a t i o na n da l i g n me n t , u p d a t i n gt i l e me mo r yi nc o n j u n c t i o nwi t hma p / OAM c h a n g e st oc u td o wno nVRAM u s e , e t c , e t c .Tot a k ea n i n d e p t hl o o ka ta l l t h ev a r i a t i o n swo u l dt a k ea ne n t i r es i t eb yi t s e l f , s oI ' l l l e a v ei ta tt h i s .Ij u s th o p ey o u ' v ep i c k e du po ns o meo ft h e b a s i c st h a tg oi n t ot e x ts y s t e ms .Wh a ty o ud owi t ht h a tk n o wl e d g eIl e a v eu pt oy o u . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 184/331 28-03-13 Tonc : GBA Programming in rot13 20. Mode 7 Part 1 I n t r o d u c t i o n . Ge t t i n gas e n s eo fp e r s p e c t i v e . En t e rMo d e7 . Th r e e f o l dd e mo . Or d e r , o r d e r ! . Fi n a l t h o u g h t s . Ri g h t , a n dn o wf o rs o me t h i n gc o o l : mo d e7 .No tj u s th o wt oi mp l e me n ti to nt h eGBA, b u ta l s ot h ema t hb e h i n di t .Yo un e e dt ok n o w y o u rwa ya r o u n dt i l e db a c k g r o u n d s( e s p e c i a l l yt h et r a n s f o r ma b l eo n e s )i n t e r r u p t s .Re a du po nt h o s es u b j e c t si fy o ud o n ' t .Th es t u f fy o u ' l l f i n dh e r ee x p l a i n st h eb a s i c so fMo d e7 .Ia l s oh a v ea na d v a n c e dp a g e , b u tIu r g ey o ut or e a dt h i so n ef i r s t , s i n c et h ema t hi ss t i l l r a t h e r e a s yc o mp a r e dt owh a tI ' l l u s et h e r e . 20.1. Introduction Wa y , wa yb a c ki n1 9 9 0 , t h e r ewa st h eSu p e rNES, t h e1 6 b i ts u c c e s s o rt ot h eNi n t e n d oEn t e r t a i n me n tSy s t e m.Ap a r tf r o mt h eu s u a l i mp r o v e me n t st h a ta r ei n h e r e n tt on e wt e c h n o l o g y , t h eSNESwa st h ef i r s tc o n s o l et oh a v es p e c i a l h a r d wa r ef o rg r a p h i ct r i c k st h a t a l l o we dl i n e a rt r a n s f o r ma t i o n s( l i k er o t a t i o na n ds c a l i n g )o nb a c k g r o u n d sa n ds p r i t e s .Mode7 t o o kt h i so n es t e pf u r t h e r : i tn o to n l yr o t a t e d a n ds c a l e dab a c k g r o u n d , b u ta d d e das t e pf o rp e r s p e c t i v et oc r e a t ea3 Dl o o k . On ec o u l dh a r d l yc a l l Mo d e7y e ta n o t h e rp r e t t yg i mmi c k .Fo re x a mp l e , i tma n a g e dt or a d i c a l l yc h a n g et h er a c i n gg a meg e n r e .Ol d e r r a c i n gg a me s( l i k ePo l ePo s i t i o na n dOu t r u n )we r el i mi t e dt os i mp l el e f ta n dr i g h tb e n d s .Mo d e7a l l o we dmo r ei n t e r e s t i n gt r a c k s , a sy o u r v i s i o nwa s n ' tl i mi t e dt ot h ep a r to ft h et r a c kr i g h ti nf r o n to fy o u .FZe r owa st h ef i r s tg a met ou s ei ta n db l e we v e r y t h i n gb e f o r ei to u to f t h ewa t e r( t h eo r i g i n a l Fi r eFi e l di ss t i l l o n eo ft h emo s tv i c i o u st r a c k sa r o u n dwi t hi t sh a i r p i n s , ma g b e a msa n dmi n e s ) .Ot h e ri l l u s t r i o u s g a me swe r es o o nt of o l l o w, l i k eSu p e rMa r i oKa r t( mmmm, Ra i n b o wRo a d .1 5 0 c c , f u l l t h r o t t l ea l l t h ewa yt h r o u g h* g a r g l e * )a n d Pi l o t wi n g s . Si n c et h eGBAi se s s e n t i a l l yami n i a t u r eSNES, i ts t a n d st or e a s o nt h a ty o uc o u l dd oMo d e 7g r a p h i c so ni ta swe l l .An d , y o u ' db er i g h t , a l t h o u g hIh e a r dt h eGBAMo d e 7i sal i t t l ed i f f e r e n ta st h eSNES' .Ont h eSNESt h ev i d e omo d e sr e a l l yd i dr u nu pt o#7( s e et h e “ q s n e s d o c . h t m”i nt h eSNESs t a r t e rk i t )Th eGBAo n l yh a smo d e s0 5 .Sot e c h n i c a l l y“ GBAMo d e7 ”i say e ta n o t h e rmi s n o me r . Ho we v e r , f o re v e r y o n ewh o ' sn o taSNESp r o g r a mme r( wh i c his n e a r l ye v e r y o n e , mei n c l u d e d )t h et e r mi ss y n o n y mo u swi t ht h e g r a p h i c a l e f f e c ti twa sf a mo u sf o r : ap e r s p e c t i v ev i e w.An dy o uc a nc r e a t eap e r s p e c t i v ev i e wo nt h eGBA, s oi nt h a ts e n s et h et e r m' s s t i l l t r u e . I ' mn o ts u r ea b o u tt h eSNES, b u tGBAMo d e7i sav e r ymu c hu n l i k et r u e3 DAPI sl i k eOp e n GLa n dDi r e c t 3 D.Ont h o s es y s t e ms , y o uc a nj u s tg i v et h ep r o p e rp e r s p e c t i v ema t r i xa n dp l a c ei ti n t ot h ep i p e l i n e .Ont h eGBA, h o we v e r , y o uo n l yh a v et h eg e n e r a l 2 D t r a n s f o r ma t i o nma t r i xP a n dd i s p l a c e me n tdx a ty o u rd i s p o s a l a n dy o uh a v et od oa l l t h ep e r s p e c t i v ec a l c u l a t i o n sy o u r s e l f .Th i sb a s i c a l l y me a n st h a ty o uh a v et oa l t e rt h es c a l i n ga n dt r a n s l a t i o no ne v e r ys c a n l i n eu s i n ge i t h e rt h eHBl a n kDMAo rt h eHBl a n ki n t e r r u p t . I nt h i st u t o r i a l , Iwi l l u s et h e6 4 x 6 4 ta f f i n eb a c k g r o u n df r o mt h es b b _a f fd e mo( wh i c hl o o k sab i tl i k ef i g2 0 . 1 ) , d ot h eMo d e 7mo j oa n d t u r ni ti n t os o me t h i n gl i k ed e p i c t e di nf i g2 0 . 2 .Th ef o c u swi l l b eo ns h o wi n g , i nd e t a i l , h o wt h ema g i cwo r k s .Wh i l et h ee n dr e s u l ti sg i v e n a saHBl a n ki n t e r r u p tf u n c t i o n ;c o n v e r t i n gt oaHBl a n kDMAc a s es h o u l d n ' tb et oh a r d . Fig 20.2: t h i si syo u rma pi nmo d e 7 . Fig 20.1: t h i si syo u rma p( we l l , k i n d a ) file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 185/331 28-03-13 Tonc : GBA Programming in rot13 20.2. Getting a sense of perspective ( I fy o ua r ef a mi l i a rwi t ht h eb a s i c so fp e r s p e c t i v e , y o uc a nj u s ts k i mt h i ss e c t i o n . ) I fy o u ' v ee v e rl o o k e da tawo r l dma po ra3 Dg a me , y o uk n o wt h a twh e nma p p i n gf r o m3 Dt o2 D, s o me t h i n g ' h a st og i v e .Th et e c h n i c a l t e r mf o rt h i si sprojection.Th e r ea r ema n yt y p e so fp r o j e c t i o n , b u tt h eo n ewe ' r ec o n c e r n e dwi t hi sperspective, wh i c hma k e so b j e c t s l o o ks ma l l e rt h ef u r t h e ro f ft h e ya r e . Wes t a r twi t ha3 Ds p a c el i k et h eo n ei nf i g2 0 . 3 .I nc o mp u t e rg r a p h i c s , i ti sc u s t o ma r yt oh a v et h exa x i sp o i n t i n gt ot h er i g h ta n d t h eya x i sp o i n t i n gu p .Th eza x i si st h ed e t e r mi n e db yt h eh a n d e d n e s so ft h es p a c e : aright-handed c o o r d i n a t es y s t e mh a si tp o i n t i n gt o t h eb a c k( o u to ft h es c r e e n ) , wh i c hi nal e f t h a n d e ds y s t e mi t ' sp o i n t i n gt ot h ef r o n t .I ' mu s i n gar i g h t h a n d e ds y s t e mb e c a u s emymi n d g e t sh o p e l e s s l yc o n f u s e di nal e f t h a n d e ds y s t e mwh e ni tc o me st or o t a t i o na n dc a l c u l a t i n gn o r ma l s .An o t h e rr e a s o ni st h a tt h i swa yt h e s c r e e nc o o r d i n a t e sc o r r e s p o n dt o( x, z)v a l u e s .I ti sa l s oc u s t o ma r yt oh a v et h ev i e we ra tt h eo r i g i n( f o rad i f f e r e n tv i e we rp o s i t i o n , s i mp l yt r a n s l a t et h ewo r l di nt h eo t h e rd i r e c t i o n ) .Fo rar i g h t h a n d e ds y s t e m, t h i sme a n st h a ty o u ' r el o o k i n gd o wnt h en e g a t i v eza x i s . Ofc o u r s e , y o uc a n ' ts e ee v e r y t h i n g : o n l yt h eo b j e c t si n s i d et h eviewing volume a r ev i s i b l e .Fo rap e r s p e c t i v ep r o j e c t i o nt h i si s d e f i n e db yt h ev i e we rp o s i t i o n( t h eo r i g i ni no u rc a s e )a n dt h eprojection plane, l o c a t e di nf r o n to ft h ev i e we ra tad i s t a n c eD.Th i n ko f i ta st h es c r e e n .Th ep r o j e c t i o np l a n eh a sawi d t hW a n dh e i g h tH.Sot h ev i e wi n gv o l u mei sa c t u a l l yaviewing pyramid, t h o u g hi n p r a c t i c ei ti su s u a l l yav i e wi n gfrustum ( ab e h e a d e dp y r a mi d ) , s i n c et h e r ei sami n i mu ma n dma x i mu mt ot h ed i s t a n c ey o uc a np e r c e i v e a swe l l . Fi g2 0 . 4s h o wswh a tt h ep e r s p e c t i v ep r o j e c t i o na c t u a l l yd o e s .Gi v e ni sap o i n t( y, z)wh i c hi sp r o j e c t e dt op o i n t( yp, −D)o nt h e p r o j e c t i o np l a n e .Th ep r o j e c t e dzc o o r d i n a t e , b yd e f i n i t i o n , i s−D.Th ep r o j e c t e dyc o o r d i n a t ei st h ei n t e r s e c t i o no ft h ep r o j e c t i o np l a n e a n dt h el i n ep a s s i n gt h r o u g ht h ev i e we ra n dt h eo r i g i n a l p o i n t : (20.1) y p=y· D/ z Ba s i c a l l y , y o ud i v i d eb yz/D.Si n c ei ti ss oi mp o r t a n taf a c t o ri th a si so wnv a r i a b l e : t h ezoom factor λ : (20.2) λ=z/ D=y/ yp Asar u l e , e v e r y t h i n gi nf r o n tt h ep r o j e c t i o np l a n e( λ <1 )wi l l b ee n l a r g e d , a n de v e r y t h i n gb e h i n di t( λ >1 )i ss h r u n k . Fig 20.4: S i d ev i e w; po i n t( y, z)i spr o j e c t e do n t ot h e( z =−D Fig 20.3: 3 Dc o o r d i n a t es ys t e ms h o wi n gt h ev i e wi n g pl a n e . Th epr o j e c t e dpo i n ti syp=y·D/z pyr a mi dd e f i n e db yt h eo r i g i n , a n dt h es c r e e n r e c t a n g l e( W×H)a tz=−D 20.3. Enter Mode 7 Fi g s2 0 . 3a n d2 0 . 4d e s c r i b et h eg e n e r a l c a s ef o rp e r s p e c t i v ep r o j e c t i o ni na3 Dwo r l dwi t ht o n so fo b j e c t sa n dv i e we ro r i e n t a t i o n s .Th e c a s ef o rMo d e7i sc o n s i d e r a b l yl e s sc o mp l i c a t e dt h a nt h a t : Objects.Weo n l ywo r kwi t ht woo b j e c t s : t h ev i e we r( a tp o i n ta =( a x, a y, a z))a n dt h ef l o o r( a ty=0 , b yd e f i n i t i o n ) . Viewer orientation.I naf u l l 3 Dwo r l d , t h ev i e we ro r i e n t a t i o ni sg i v e nb y3a n g l e s : y a w( y a x i s ) , p i t c h( x a x i s )a n dr o l l ( z a x i s ) .Wewi l l l i mi to u r s e l v e st oy a wt ok e e pt h i n g ss i mp l e . The horizon issue.Be c a u s et h ev i e wd i r e c t i o ni sk e p tp a r a l l e l t ot h ef l o o r , t h eh o r i z o ns h o u l dg oi nt h ec e n t e ro ft h es c r e e n . Th i swo u l dl e a v et h et o ph a l fo ft h es c r e e ne mp t y , wh i c hi sab i to fawa s t e .Tor e me d yt h i sweo n l yu s et h eb o t t o mh a l fo ft h e v i e wi n gv o l u me , s ot h a tt h eh o r i z o ni sa tt h et o po ft h es c r e e n .No t et h a te v e nt h o u g ht h et o pa n db o t t o mv i e wl i n e sa r en o w t h es a mea swh e ny o uwo u l dl o o kd o wnab i t , t h ec a s e sa r eNOT e q u a l a st h ep r o j e c t i o np l a n ei ss t i l l v e r t i c a l .I ti si mp o r t a n t file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 186/331 28-03-13 Tonc : GBA Programming in rot13 t h a ty o ur e a l i z et h ed i f f e r e n c e . Fig 20.5: s i d ev i e wo fMo d e7pe r s pe c t i v e Fi g2 0 . 5s h o wst h ewh o l es i t u a t i o n .Av i e we ra ty =a yi sl o o k i n gi nt h en e g a t i v ez d i r e c t i o n .Atad i s t a n c eD i nf r o n to ft h ev i e we ri st h e p r o j e c t i o np l a n e , t h eb o t t o mh a l fo fwh i c hi sd i s p l a y e do nt h eGBAs c r e e no fh e i g h tH ( =1 6 0 ) .An dn o wf o rt h ef u np a r t .Th eGBA d o e s n ' th a v ea n yr e a l 3 Dh a r d wa r ec a p a b i l i t i e s , b u ty o uc a nf a k ei tb yc l e v e r l yma n i p u l a t i n gt h es c a l i n ga n dt r a n s l a t i o nREG_BGxXREG_BGxPDf o re v e r ys c a n l i n e .Yo uj u s th a v et of i g u r eo u twh i c hl i n eo fp a r to ft h ef l o o rg o e si n t owh i c hs c a n l i n e , a n da twh i c hz o o m l e v e l .Ef f e c t i v e l y , y o u ' r eb u i l d i n gav e r ys i mp l er a y c a s t e r . 20.3.1. The math Co n c e p t u a l l y , t h e r ea r ef o u rs t e p st oMo d e7 , d e p i c t e di nf i g s2 0 . 6 a d .Gr e e nf i g u r e si n d i c a t et h eo r i g i n a l ma p ;r e di st h ema pa f t e rt h e o p e r a t i o n .Gi v e nas c a n l i n eh, h e r e ' swh a twed o : a .Pre-translation b ya=( a x, a z) .Th i sp l a c e st h ev i e we ra tt h eo r i g i n , wh i c hi swh e r ewen e e di tf o rs t e p sba n dc . b .Rotation b yα.Th i st a k e sc a r eo ft h ey a wa n g l e .Th e s es t e p sh a v eb e e nt h es a mea sf o rn o r ma l t r a n s f o r ma b l eb a c k g r o u n d s s oy o us h o u l d n ' th a v ea n yd i f f i c u l t yu n d e r s t a n d i n gt h e m. c .Perspective division.Ne x t , wes c a l et h ewh o l et h i n gb y1 / λ .Fr o me q2 0 . 2weh a v eλ=a y/ h.Th el i n ez =zhi st h el i n et h a t b e l o n g so ns c a n l i n eh.Th en e wp o s i t i o no ft h i sl i n ea f t e rs c a l i n gi sz =−D, s i n c et h a twa st h ewh o l ep o i n to fp e r s p e c t i v e d i v i s i o n . d .Post-translation b y( −x s) .No t et h emi n u ss i g n .Af t e rt h ep e r s p e c t i v ed i v i s i o n , a l l t h a tr e ma i n si smo v i n gt h ef u l l yt r a n s f o r me d ma pb a c kt oi t sp r o p e rs c r e e np o s i t i o n( t h eb e i g ea r e a ) .Fo ro b v i o u sr e a s o n st h eh o r i z o n t a l c o mp o n e n ts h o u l db eh a l ft h e s c r e e nwi d t h .Th ev e r t i c a l mo v es h o u l dmo v et h ef l o o r l i n et ot h es c a n l i n e , s ot h ev e c t o ri s : (20.3) xs=W/ 2=120 ys=( D+h) 2 0 . 6 a : p r e t r a n s l a t eb y( a x, a z)2 0 . 6 b : r o t a t eb yα 2 0 . 6 c : s c a l eb y1 / λ 2 0 . 6 d : p o s t t r a n s l a t eb y( xs, ys) Fi g2 0 . 6 a d : Th e4s t e pso fmo d e7 20.3.2. Putting it all together Wh i l et h es t e p sd e s c r i b e da b o v ea r ei n d e e dt h ef u l l p r o c e d u r e , t h e r ea r es t i l l an u mb e ro fl o o s ee n d st ot i eu p .Fi r s to fa l l , r e me mb e rt h a t t h eGBA' st r a n s f o r ma t i o nma t r i xP ma p sf r o ms c r e e ns p a c et ob a c k g r o u n ds p a c e , wh i c hi sa c t u a l l yt h ei n v e r s eo fwh a ty o u ' r et r y i n gt o d o .Sowh a ty o us h o u l du s ei s : (20.4) P = S( λ )·R( α)= λ · c os ( α) −λ · s i n ( α) λ · s i n ( α) λ · c os ( α) An dy e s , t h emi n u ss i g ni sc o r r e c tf o rac o u n t e r c l o c k wi s er o t a t i o n( Ri sd e f i n e da sac l o c k wi s er o t a t i o n ) .Al s or e me mb e rt h a tt h eGBA u s e st h ef o l l o wi n gr e l a t i o nb e t we e ns c r e e np o i n tq a n db a c k g r o u n dp o i n tp: (20.5) dx +P ·q =p , t h a ti s , o n et r a n s l a t i o na n do n et r a n s f o r ma t i o n .Weh a v et oc o mb i n et h ep r e -a n dp o s t t r a n s l a t i o n st oma k ei two r k .We ' v es e e nt h i s b e f o r ei ne q4i nt h ea f f i n eb a c k g r o u n dp a g e , o n l ywi t hd i f f e r e n tn a me s .An y wa y , wh a ty o un e e di s : file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 187/331 28-03-13 Tonc : GBA Programming in rot13 (20.6) dx + P · q =p P ·( q − xs)=p − a − dx + P · xs =a dx =a −P · xs Sof o re a c hs c a n l i n ey o ud ot h ec a l c u l a t i o n sf o rt h ez o o m, p u tt h eP ma t r i xo fe q2 0 . 4i n t oREG_BGxPA-REG_BGxPD, a n da−P·x si n t o REG_BGxXa n dREG_BGxYa n dp r e s t o !I n s t a n tMo d e7 . We l l , a l mo s t .Re me mb e rwh a th a p p e n swh e nwr i t i n gt oREG_BGxYi n s i d ea nHBl a n ki n t e r r u p t : t h ecurrent s c a n l i n ei sp e r c e i v e da s t h es c r e e n ' so r i g i nn u l l l i n e .I no t h e rwo r d s , i td o e st h e+h p a r to fys a u t o ma t i c a l l y .Re n a mi n gt h et r u eyst oys0, wh a ty o ushould u s ei s (20.7) y s=y s0−h =D. No w, i nt h e o r yy o uh a v ee v e r y t h i n gy o un e e d .I np r a c t i c e , t h o u g h , t h e r ea r ean u mb e ro ft h i n g st h a tc a ng owr o n g .Be f o r eIg oi n t ot h a t , h e r e ' san i c e , ( n o ts o )l i t t l ed e mo . 20.4. Threefold demo Asu s u a l , t h e r ei sad e mo .Ac t u a l l y , Ih a v es e v e r a l Mo d e7d e mo s , b u tt h a t ' sn o ti mp o r t a n tr i g h tn o w.Th ed e moi sc a l l e dm7_demoa n d t h ec o n t r o l sa r e : r a f e . D-pad St u r nl e f ta n dr i g h t( i . e . , r o t a t ema pr i g h ta n dl e f t , r e s p e c t i v e l y ) L, R t v eu pa n dd o wn , t h o u g hIf o r g e twh i c hi swh i c h . A, B Mo t c hb e t we e n3d i f f e r e n tMo d e 7t y p e s( A, B, C) Select Swi Start Re s e t sa l l v a l u e s( a=( 2 5 6 , 3 2 , 2 5 6 ) , α=0 ) “ Swi t c hb e t we e n3d i f f e r e n tMo d e 7t y p e s ” ?Th a t ' swh a tIs a i d , y e s .Ma k es u r ey o umo v ea r o u n di na l l t h r e et y p e s .Pl e a s e .Th e r e ' sa l a b e l i nt h et o p l e f tc o r n e ri n d i c a t i n gt h ec u r r e n tt y p e . Fig 20.7a: TypeA: b l o c k e d . Fig 20.7b: TypeB: s a wt o o t h . Fig 20.7c: TypeC: s mo o t h . 20.5. Order, order! Fi d d l e dwi t hmyd e moab i t ?Go o d .No t i c e dt h ed i f f e r e n c e sb e t we e nt h et h r e et y p e s ?Ev e nb e t t e r !Fo rr e f e r e n c e , t a k eal o o ka t Fi g s2 0 . 7 a c , wh i c hc o r r e s p o n dt ot h et y p e s .Th e ya d e q u a t e l ys h o wwh a t ' sd i f f e r e n t . Ty p eAi sh o r r i b l yb l o c k y .Th o s en u mb e r si nt h er e dt i l e sa r es u p p o s e dt ob e‘ 8 ’ s .He h , n u mb e r s ?Wh a tn u mb e r s ! Ty p eBi sb e t t e r .Th el e f t h a n ds i d ei ss mo o t h , b u tt h e r e ' ss t i l l s o met r o u b l eo nt h er i g h t h a n ds i d e .Bu ta tl e a s ty o uc a ns e e e i g h t swi t hs o mei ma g i n a t i o n . Ty p eC.No wwe ' r et a l k i n g !Th ec e n t e r l i n ei sc l e a r , wh i c hi si mp o r t a n ts i n c et h a t ' swh a ty o u ' r el o o k i n ga tmo s to ft h et i me . Bu te v e no nt h es i d e s , t h i n g sa r el o o k i n gp r e t t yd e c e n t . Soweh a v et h r e ev e r yd i f f e r e n tMo d e 7r e s u l t s , b u tIg u a r a n t e ey o ui t ' sa l l b a s e do nt h es a mema t h .Soh o wc o meo n eme t h o dl o o k ss o c r u mmy , a n dt h eo t h e rl o o k sg r e a t ? 20.5.1. The code file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 188/331 28-03-13 Tonc : GBA Programming in rot13 He r ea r et h et woHBl a n kI SRst h a tc r e a t et h et y p e s .Ty p e sAa n dBa r en e a r l yi d e n t i c a l , e x c e p tf o ro n et h i n g .Ty p eCi sv e r yd i f f e r e n t f r o mt h eo t h e r s .I fy o uh a v eat h i n gf o rs e l f t o r t u r e , t r ye x p l a i n i n gt h ed i f f e r e n c e sf r o mt h ec o d ea l o n e .Is p e n tmo s to fy e s t e r d a yn i g h t f i g u r i n go u twh a tma d eTy p eCwo r k , s oIh a v eh a l fami n do fl e a v i n gy o uh a n g i n g .Fo r t u n a t e l yf o ry o u , t h a th a l f ' sa s l e e pr i g h tn o w. #define M7_D 128 extern VECTOR cam_pos; extern FIXED g_cosf, g_sinf; // Camera position // cos(phi) and sin(phi), .8f // --- Type A --// (offset * zoom) * rotation // All .8 fixed void m7_hbl_a() { FIXED lam, xs, ys; lam= cam_pos.y*lu_div(REG_VCOUNT)>>16; // .8*.16/.16 = .8 // Calculate offsets (.8) xs= 120*lam; ys= M7_D*lam; REG_BG2PA= (g_cosf*lam)>>8; REG_BG2PC= (g_sinf*lam)>>8; REG_BG2X = cam_pos.x - ( (xs*g_cosf-ys*g_sinf)>>8 ); REG_BG2Y = cam_pos.z - ( (xs*g_sinf+ys*g_cosf)>>8 ); } // --- Type B --// (offset * zoom) * rotation // Mixed fixed point: lam, xs, ys use .12 void m7_hbl_b() { FIXED lam, xs, ys; lam= cam_pos.y*lu_div(REG_VCOUNT)>>12; // .8*.16/.12 = .12 // Calculate offsets (.12f) xs= 120*lam; ys= M7_D*lam; REG_BG2PA= (g_cosf*lam)>>12; REG_BG2PC= (g_sinf*lam)>>12; REG_BG2X = cam_pos.x - ( (xs*g_cosf-ys*g_sinf)>>12 ); REG_BG2Y = cam_pos.z - ( (xs*g_sinf+ys*g_cosf)>>12 ); } // --- Type C --// offset * (zoom * rotation) // Mixed fixed point: lam, lxr, lyr use .12 // lxr and lyr have different calculation methods void m7_hbl_c() { FIXED lam, lcf, lsf, lxr, lyr; lam= cam_pos.y*lu_div(REG_VCOUNT)>>12; // .8*.16 /.12 = 20.12 lcf= lam*g_cosf>>8; // .12*.8 /.8 = .12 lsf= lam*g_sinf>>8; // .12*.8 /.8 = .12 REG_BG2PA= lcf>>4; REG_BG2PC= lsf>>4; // Offsets // Note that the lxr shifts down first! // horizontal offset lxr= 120*(lcf>>4); lyr= (M7_D*lsf)>>4; REG_BG2X= cam_pos.x - lxr + lyr; file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 189/331 28-03-13 Tonc : GBA Programming in rot13 // vertical offset lxr= 120*(lsf>>4); lyr= (M7_D*lcf)>>4; REG_BG2Y= cam_pos.z - lxr - lyr; } 20.5.2. The discussion (technical) Al l t h r e ev e r s i o n sd ot h ef o l l o wi n gt h i n g s : c a l c u l a t et h ez o o mf a c t o rλ , u s i n ge q2a n dad i v i s i o nLUT, c a l c u l a t et h ea f f i n ema t r i xu s i n gλ a n ds t o r e dv e r s i o n so fc o s ( φ)a n ds i n ( φ) , a n dc a l c u l a t et h ea f f i n eo f f s e t s .No t et h a to n l yp aa n dp ca r ea c t u a l l yc a l c u l a t e d ;b e c a u s et h e s c a n l i n eo f f s e ti se f f e c t i v e l yz e r oa l l t h et i me , p ba n dp dh a v en oe f f e c ta n dc a nb ei g n o r e d .Th o s ea r et h es i mi l a r i t i e s , b u twh a t ' smo r e i n t e r e s t i n ga r et h ed i f f e r e n c e s : 1 .Fixed point.Ty p eAu s e s. 8f i x e dp o i n tma t ht h r o u g h o u t , b u tBa n dCu s eac o mb i n a t i o no f. 1 2a n d. 8f i x e d s . 2 .Calculation order of the affine offset Th ea f f i n ed i s p l a c e me n tdx i sac o mb i n a t i o no f3p a r t s : s c a l e , r o t a t i o na n do f f s e t s . Ty p eAa n dBu s edx =( o f f s e t * s c a l e ) * r o t a t i o n , wh i l eCu s e sdx =o f f s e t * ( s c a l e * r o t a t i o n ) .Be c a u s et y p eCd o e st h eo f f s e t s l a s t , i tc a na l s ou s ed i f f e r e n tf i x e d p o i n t sf o rt h eo f f s e t s . Th e s et wo( we l l , 2a n dah a l f , r e a l l y )d i f f e r e n c e sa r ee n o u g ht oe x p l a i nt h ed i f f e r e n c e si nt h e h 1/h λ (true) λ(.8) r e s u l t s .Pl e a s er e me mb e rt h a tt h ed i f f e r e n c e si nt h ec o d ea r eq u i t es u b t l e : f i x e dp o i n tn u mb e r s a r er a r e l yu s e do u t s i d ec o n s o l e s , a n dr e s u l t sc h a n g i n gd u et ot h eo r d e ro fc a l c u l a t i o ni sp r o b a b l y 1 5 70 . 0 1 a 1 6 d . . h0 . 3 4 2 d a 7 h0 . 3 4 h e v e nr a r e r .Ye ti st h e s et woi t e mst h a tma k ea l l t h ed i f f e r e n c eh e r e . 1 5 80 . 0 1 9 e c 8 . . h0 . 3 3 d 9 1 d h0 . 3 3 h Le t ' ss t a r twi t ht y p e sAa n dB, wh i c hd i f f e ro n l yb yt h ef i x e d p o i n to flam.λi st h er a t i o no f 5 90 . 0 1 9 c 2 d . . h0 . 3 3 8 5 a 2 h0 . 3 3 h t h ec a me r ah e i g h ta n dt h es c a n l i n e , wh i c hwi l l o f t e nb eq u i t es ma l l –s ma l l e rt h a n1a ta n yr a t e . 1 Ta b l e2 0 . 1s h o wsaf e wo ft h en u mb e r s .No t et h a tu s i n gaλwi t ho n l y8f r a c t i o n a l b i t sme a n st h a t1 6 00 . 0 1 9 9 9 9 . . h0 . 3 3 3 3 3 3 h0 . 3 3 h y o u ' l l o f t e nh a v et h es a men u mb e rf o rmu l t i p l es c a n l i n e s , wh i c hc a r r i e st h r o u g hi nt h el a t e r Table 20.1: d i v i s i o nt a b l e sa ndz oo m c a l c u l a t i o n s .Th i si swh yt y p eA, wh i c hp l a y sb yt h er u l e sa n du s e sac o n s t a n tf i x e d p o i n tl i k ea f a c t o r s . ay=3 2 g o o dl i t t l eb o y , i ss ob l o c k ya tl o wa l t i t u d e s .Th ef o u re x t r ab i t so ft y p eBg i v e smu c hb e t t e r r e s u l t s .Ru l e sa r en i c ea n da l l , b u ts o me t i me st h e yn e e d st ob eb r o k e nt og e tr e s u l t s . No w, y o uwi l l n o t i c et h a tt y p eBs t i l l h a sab i to fd i s t o r t i o n , s owh yo n l yg ot o. 1 2f i x e d si nt y p eB, wh yn o t1 6 ?We l l , wi t h1 6y o uc a n g e ti n t ot r o u b l ewi t hi n t e g e ro v e r f l o w.I t ' db ea l r i g h tf o rc a l c u l a t i n gxsa n dys, b u twes t i l l h a v et or o t a t et h e s ev a l u e sl a t e ro na swe l l . OK, s owe ' l l u s e6 4 b i tma t h , t h e nt h e3 2 b i to v e r f l o wwo u l d n ' tma t t e ra n dwec o u l du s eeven more f i x e dp o i n tb i t s !Af t e ra l l , mo r e== b e t t e r , r i g h t ? We l l , n o .Bi g g e r / s t r o n g e r / mo r ed o e sn o ta l wa y sme a nb e t t e r( s e et h eDSv sPSP) .Th er e ma i n i n gd i s t o r t i o ni sn o tama t t e ro ft h e n u mb e ro ff i x e d p o i n tb i t s ;n o te x a c t l y .Yo uc o u l du s ea1 2 8 b i tma t ha n d. 3 2 fd i v i s i o na n dt r i gt a b l e sf o ra l l Ic a r e ;i two u l d n ' tma t t e r h e r e , b e c a u s et h a t ' sn o twe r et h ep r o b l e mi s . Th ep r o b l e m, o ra tl e a s tp a r to fi t , i st h eb a s i ca l g o r i t h mu s e di nt y p e sAa n dB.I fy o ul o o kb a c kt ot h et h e o r y , y o u ' l l s e et h a tt h e a f f i n ema t r i xi sc a l c u l a t e df i r s t , t h e nt h eo f f s e t s .I no t h e rwo r d s , f i r s tc o mb i n et h es c a l ea n dr o t a t i o n , t h e nc a l c u l a t et h eo f f s e t c o r r e c t i o n , P· x s.Th i si sh o wt h ea f f i n ep a r a me t e r si nt h eGBAwo r ka n y wa y .Ho we v e r , t h i si sa c t u a l l yo n l yt h ef i r s ts t e p .I fy o uf o l l o wt h a t p r o c e d u r e , y o u ' ds t i l l g e tt h ej a g g e dr e s u l t .Th ereal r e a s o nf o rt h e s ej a g g i e si st h eo r d e ro fc a l c u l a t i o no flxr. // Multiply, then shift to .8 (A and B) lxr= (120*lcf)>>4; // Shift to .8 first, then multiply (C) lxr= 120*(lcf>>4); Ge t t i n glxr=p a/c· xsr e q u i r e st wop a r t s : mu l t i p l i c a t i o nwi t hP e l e me n t sa n dt h es h i f td o wnt o. 8f i x e d s .Yo umi g h te x p e c td o i n gt h es h i f t l a s two u l db eb e t t e rb e c a u s ei th a sah i g h e rp r e c i s i o n .Th ef u n n yt h i n gi st h a ti tdoesn't!Sh i f t i n gp ao rp cd o wnt o8f r a c t i o n a l b i t sb e f o r e t h emu l t i p l i c a t i o ni swh a tg e t sr i do ft h er e ma i n i n gd i s t o r t i o n s , r e v e r s i n gt h eo r d e ro fo p e r a t i o n sd o e s n ' t . Asf o rwh y , I ' mn o t1 0 0 %s u r e , b u tIc a nh a z a r dag u e s s .Th ea f f i n et r a n s f o r ma t i o nt a k e sp l a c ea r o u n dt h eo r i g i no ft h es c r e e n , a n d t op l a c et h eo r i g i ns o me wh e r ee l s ewen e e dt oa p p l yap o s t t r a n s l a t i o nb yx s.Th ec r u c i a l p o i n tIt h i n ki st h a tx si sap o i n ti ns c r e e n s p a c e wh i c hu s e sn o r ma l i n t e g e r s , n o tf i x e dp o i n t s .Ho we v e r , i to n l ya p p l i e st oxsb e c a u s et h a treally r e p r e s e n t sa no n s c r e e no f f s e t ;ysi s a c t u a l l yn o tap o i n to nt h es c r e e nb u tt h ef o c a l d i s t a n c eo ft h ec a me r a .Ont h eo t h e rh a n d , i tmi g h th a v es o me t h i n gt od owi t ht h ei n t e r n a l r e g i s t e r sf o rt h ed i s p l a c e me n t . 20.5.3. The verdict Ob v i o u s l y , t y p eCi st h eo n ey o uwa n t .I tr e a l l yb u g st h eh e l l o u to fmet h a tId i d n ' tt h i n ko fi tmy s e l f .An dt h ef a c tt h a tIdid u s et h e s c a l e r o t a t i o nmu l t i p l i c a t i o nb u ta b a n d o n e di tb e c a u s eIs c r e we du pwi t ht h emu l t i p l i c a t i o nb yt h ep r o j e c t i o nd i s t a n c eD d o e s n ' th e l p e i t h e r( y e s , t h i ss e n t e n c ema k e ss e n s e ) .Th ec o d eo fm7_hbl_cs h o wna b o v ewo r k s , e v e nt h o u g hi to n l yu s e s3 2 b i tma t h .Asl o n ga s y o ud ot h es c a l e r o t a t i o nmu l t i p l i c a t i o nf i r s ta n ds h i f td o wnt o. 8f i x e d sb e f o r ey o umu l t i p l yb y1 2 0i nt h ec a l c u l a t i o no fwxre v e r y t h i n g s h o u l db ef i n e . 20.6. Final Thoughts Th i sh a sb e e no n eo ft h o s eo c c a s i o n st h a ts h o wt h a tp r o g r a mmi n g( e s p e c i a l l yl o wl e v e l p r o g r a mmi n g )i sa smu c ho fas c i e n c ea sa na r t . Ev e nt h o u g ht h et h e o r yf o rt h et h r e emo d e7v e r s i o n swa st h es a me , t h es l i g h td i f f e r e n c e si nt h eo r d e ra n dp r e c i s i o no ft h ec a l c u l a t i o n s file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 190/331 28-03-13 Tonc : GBA Programming in rot13 i nt h ei mp l e me n t a t i o n sma d ef o rv e r yn o t i c e a b l ed i f f e r e n c e si nt h ee n dr e s u l t .Wh e ni tc o me st omo d e7 , c a l c u l a t et h ea f f i n ema t r i x b e f o r et h ec o r r e c t i o no f f s e t .Bu tmo s ti mp o r t a n t l y , t h exo f f s e tf o rt h es c r e e ns h o u l dn o tb ed o n ei nf i x e dp o i n t . Se c o n d l y , t h i swa so n l yt h eb a s i ct h e o r yb e h i n dmo d e7g r a p h i c s .Nos p r i t e s , n op i t c h a n g l ea n dn oh o r i z o n , a n dt a i l o r e dt ot h eGBA h a r d wa r ef r o mt h es t a r t .I nt h en e x tc h a p t e r , we ' l l d e r i v et h et h e o r ymo r ee x t e n s i v e l yf o l l o wi n gs t a n d a r d3 Dt h e o r ywi t hl i n e a ra l g e b r a . Th i sc h a p t e rwi l l a l s os h o wh o wt op o s i t i o ns p r i t e si n3 Da n dh o wt od oo t h e rt h i n g swi t ht h e ml i k ea n i ma t i n gf o rr o t a t i o na n ds o r t i n g , a n d a l s op r e s e n tv a r i a b l e p i t c ha n dah o r i z o n .I ft h i ss o u n d sc o mp l i c a t e d , we l l , Is u p p o s e dt h a ti ti s .I t ' sd e f i n i t e l ywo r t hal o o k , t h o u g h . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 191/331 28-03-13 Tonc : GBA Programming in rot13 21. Mode 7 Part 2 I n t r o d u c t i o n . Ba s i cmo d e7t h e o r y . Ho r i z o na n db a c k d r o p . Th ef l o o r . Sp r i t e s . I mp l e me n t a t i o n . Co n c l u d i n gr e ma r k s . 21.1. Introduction Mo d e7 : p a r t1c o v e r e dt h eb a s i c so fh o wt og e ta na f f i n eb a c k g r o u n dt ol o o kl i k ea3 Dp l a n e , a n dd i s c u s s e ds o meo ft h et r i c k i e rp a r t so f t h ef i x e dp o i n ta r i t h me t i ci n v o l v e d .Ge t t i n gt h eb a s i cl o o ko fa3 Dp l a n ei so n l yt h ef i r s ts t e p . I nt h i sc h a p t e r , we ' l l l o o ki n t ot h eg e n e r a l ma t hi n v o l v e do fc r e a t i n ga3 Dwo r l da n dt r a n s l a t ei tb a c kt oaf o r mwec a nu s ef o rmo d e 7 .Th i si n c l u d e st r a n s l a t i o n si na l l d i r e c t i o n sa n dl o o k i n ga r o u n d( y a w)l i k eb e f o r e , b u ta l s oap i t c ha n g l ef o rl o o k i n gu pa n dd o wn .We ' l l a l s os e eh o wt od e a l wi t ht h eh o r i z o na n du s eab a c k g r o u n df o rt h eb a c k d r o pa b o v et h eh o r i z o n .I ' l l e v e nt h r o wi nab i to ff o g g i n gt o o c c l u d et h ed i s t a n tp a r t so ft h eg r o u n d . I ' l l d i s c u s sa l s owo r k i n gwi t hs p r i t e si n3 Ds p a c e .No tj u s tt h et r a n s f o r ma t i o nf r o m3 Ds p a c et o2 Ds c r e e n , b u ta l s oc u l l i n g , s c a l i n g f o rd i s t a n c e( wh i c hi sn o ta ss i mp l ea so n emi g h tt h i n k ) , a n i ma t i o na n ds o r t i n g .No t et h a tt h i sp a r to ft h ec h a p t e ri sb a s i c3 Ds p r i t e t h e o r y , a n dc a nb ea p p l i e dt o3 Dg a me st h a tu s es p r i t e si ns o mewa y . Th et h e o r yp a r to ft h ec h a p t e ri sg o i n gt ob ev e r yma t h h e a v y , a s3 Dt h e o r ya l wa y si s .Kn o wi n gal i t t l eb i ta b o u tl i n e a ra l g e b r a c e r t a i n l ywo u l d n ' th u r t .Th ef u l l s t o r ya b o u tg e o me t r yi sb e y o n dt h es c o p eo fTo n c , b u tt h i ss t u f fi sq u i t eg e n e r a l ;mo s tb o o k so n3 D p r o g r a mmi n gwi l l h a v eac h a p t e ro ng e o me t r i ct r a n s f o r ma t i o n s , s oy o uc a nl o o ka tt h o s ei fy o ug e tal i t t l el o s th e r e . Th i sc h a p t e rt o u c h e so na l mo s ta l l o ft h et o p i c sc o v e r e ds of a r .I tu s e sa f f i n eo b j e c t s , b a c k g r o u n d s( b o t hr e g u l a ra n da f f i n e ) , i n t e r r u p t s , c o l o re f f e c t sa n daf e wmo r e .I fy o u ru n d e r s t a n d i n go fa n yo ft h e s ei sl a c k i n g , y o uc o u l db ei nf o rar o u g ht i meh e r e . Fig 21.1: m7_ex;wi t hh o r i z o n , s p r i t e s , v a r i a b l ep i t c ha n g l ea n dd i s t a n c ef o g g i n g . Wh a twe ' r eg o i n gt ot r yt od oi sr e c r e a t eas c e n ef r o mt h eSNESMa r i oKa r t( s e ef i g2 1 . 1 ;a p o l o g i e st oNi n t e n d of o ru s i n gt h eg r a p h i c s , b u tId o n ' th a v eal o to fo p t i o n sh e r e:\) .Th i si sj u s taf r e e z e f r a meo ft h eg a me , n o ta c t u a l g a mep l a yi si n v o l v e d , b u tt h i ss h o u l d p r e s e n tan i c et a r g e tt oa i mf o r .Th ec o d ei sd i s t r i b u t e do v e ran u mb e ro ff i l e s : mode7.cf o rt h es i mp l emo d e7f u n c t i o n sa n d mode7.iwram.cf o rt h el e s ss i mp l emo d e7f u n c t i o n sa n di n t e r r u p tr o u t i n e s .Th ec o d eo fd e mo s p e c i f i cc o d ec a nb ef o u n di n m7_ex.c, wh i c hd o e st h es e t u p , i n t e r a c t i o na n dma i nl o o p .Th eb a s i cc o n t r o l sa r ea sf o l l o ws : D-pad A/B L/R Select+A/B Start Lo o k i n g Ba c k / f o r wa r d St r a f i n g Fl o a tu p / d o wn Me n u Mo v e me n ta n dl o o k i n gf o l l o wsFPS/ a i r c r a f tmo t i o n , o ra tl e a s ta swe l l a sc o u l db ee x p e c t e dwi t ht h en u mb e ro fb u t t o n sa v a i l a b l e .Th e r e a r es e v e r a l e x t r ao p t i o n swh i c hh a v eb e e np u ti name n u .Fi r s ti smotion control wh i c hs e t sd i f f e r e n c eme t h o d so fmo v e me n t .Op t i o n ‘ l o c a l ’f o l l o wst h ec a me r aa x i sf o rf l i g h t c o n t r o l s , ‘ l e v e l ’g i v e smo v e me n tp a r a l l e l t ot h eg r o u n d , l i k eFPSsu s u a l l yd o , a n d‘ g l o b a l ’u s e s wo r l da x i sf o rmo v e me n t .Ot h e ro p t i o n si n c l u d et o g g l i n gf o go no ro f fa n dr e s e t t i n gt h ed e mo . 21.2. Basic mode 7 theory Fi g2 1 . 2s h o wswh a twe ' r eu pa g a i n s t : weh a v eac a me r al o c a t e da tacw, wh i c hh a ss o meo r i e n t a t i o nwi t hr e s p e c tt ot h ewo r l d c o o r d i n a t es y s t e m.Wh a tweh a v et od oi sf i n dt h et r a n s f o r ma t i o nt h a tl i n k ss c r e e np o i n tx st owo r l dp o i n tx w.Th e r ea r ean u mb e ro f wa y st od ot h i s .Yo ua l r e a d ys a wo n ei nt h ef i r s tmo d e7c h a p t e r , wh e r eweIh a dt h eGBAh a r d wa r ei nmi n df r o mt h es t a r t .Yo uc o u l d e x t e n dt h i st ot h eg e n e r a l mo d e7c a s e( wi t han o n z e r op i t c h )wi t hs o mee f f o r t .Yo uc o u l da l s ou s ep u r et r i g o n o me t r y , wh i c hi sa file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 192/331 28-03-13 Tonc : GBA Programming in rot13 mi n e f i e l do fmi n u ss i g n sa n dp o t e n t i a l s i n e c o s i n emi x u p s .St i l l , i ti sp o s s i b l e .Wh a tI ' l l u s eh e r e , t h o u g h , i sl i n e a ra l g e b r a .Th e r ea r e s e v e r a l r e a s o n sf o rt h i sc h o i c e .Fi r s t l y , l i n e a ra l g e b r ah a sav e r yc o n c i s en o t a t i o n , s oy o uc a nwr i t ed o wnt h ef i n a l s o l u t i o ni nj u s taf e w l i n e s( i nf a c t , o n c ey o ug e tt h r o u g ht h ed e f i n i t i o n s , t h es o l u t i o nt h a tc o v e r sa l l c a s e sc a nb ewr i t t e nd o wni n2l i n e s ) .Fu r t h e r mo r e , t h e e q u a t i o n sa r ewe l l s t r u c t u r e da n du n i f o r mi na p p e a r a n c e , ma k i n gd e b u g g i n ge a s i e r .Th e nt h e r e ' st h ef a c tt h a ti n v e r t i n gt h ewh o l et h i n gi s v e r ye a s y .An dl a s t l y , i t ' swh a tt r u e3 Ds y s t e msu s et o o , s ot h et h e o r yc a nb ea p p l i e do u t s i d et h emo d e7a r e n aa swe l l .Co n v e r s e l y , i f y o uk n o wb a s i c3 Dt h e o r y , y o u ' l l f e e l r i g h ta th o meh e r e . Fig 21.2: Th eb a s i c3 Ds i t u a t i o n . Th et r i c ki st or e l a t es c r e e npo i n txst owo r l d po i ntxw, t a k i n gt h ec a me r apo s i t i o nacw a n di t so r i e n t a t i oni n t oa c c o u n t . 21.2.1. Definitions Be f o r ey o uc a nd oa n y t h i n g , t h o u g h , y o un e e dt ok n o wexactly wh a twe ' r eg o i n gt ou s e .Th ef i r s tt h i n gt on o t ei st h a tweh a v et woma i n c o o r d i n a t es y s t e ms : t h eworld s y s t e mS w a n dt h ecamera s y s t e mS c.I n s i d et h ec a me r as y s t e mweh a v et womi n o rc o o r d i n a t es y s t e ms , n a me l yt h eprojection s p a c eS pa n dscreen s p a c eS s.No w, f o re v e r yt r a n s f o r ma t i o nb e t we e ns y s t e msS ia n dS jt h ef o l l o wi n gr e l a t i o n h o l d s : (21.1) Mi · x =x −a i j j i j wh e r e h ec o o r d i n a t ev e c t o ri ns y s t e mS i; xi t h ec o o r d i n a t ev e c t o ri ns y s t e mS j; xj t h eo r i g i no fs y s t e mS j, e x p r e s s e di nc o o r d i n a t e so fs y s t e mS i; aji t t h e t r a n s f o r m a t i o n m a t r i x , w h i c h i s b a s i c a l l y t h e m a t r i x f o r me db yt h ep r i n c i p l ev e c t o r so fS j, i nt e r mso fS i. M ij On c ey o ug e to v e rt h ei n i t i a l s h o c ko ft h ema n yi n d i c e s( me h , i ng e n e r a l r e l a t i v i t yy o uh a v es o me t h i n gc a l l e dt h eRi e ma n nt e n s o r , wh i c h h a sfour i n d i c e s ) , y o u ' l l s e et h a tt h i se q u a t i o nma k e ss e n s e .I fy o ud o n ' tg e ti tr i g h ta wa y , t h i n ko ft h e ma sa r r a y sa n dma t r i c e s .An o b s e r v a n tr e a d e rwi l l a l s or e c o g n i s et h es t r u c t u r ei nt h es c r e e n ↔ma pt r a n s f o r ma t i o nweh a df o ra f f i n ema p s : P·q =p − dx.Eq2 1 . 1i sa v e r yg e n e r a l e q u a t i o n , b yt h ewa y , i th o l d sf o re v e r yk i n do fl i n e a rc o o r d i n a t et r a n s f o r ma t i o n .I nf a c t , s y s t e msS ia n dS jd o n ' te v e nh a v e t oh a v et h es a men u mb e ro fd i me n s i o n s ! Ass a i d , weh a v e4s y s t e msi nt o t a l , s oweh a v e4s u b s c r i p t sf o rw( o r l d ) , c( a me r a ) , p( r o j e c t i o n ) , s( c r e e n ) .Re me mb e rt h e s e , f o rt h e y wi l l a p p e a ro nav e r yr e g u l a rb a s i s .Th ef i n a l f o r mso ft h ema t r i c e sa n do r i g i n sd e p e n dv e r ymu c ho nt h ee x a c td e f i n i t i o n so ft h e s e s y s t e ms , s oma k es u r ey o uk n o we x a c t l ywh a te a c hme a n s . 21.2.2. World system Th ef i r s to ft h e s e , t h ewo r l ds y s t e mS w, i se a s yt od e a l wi t h .Th i si ss i mp l yar i g h t h a n d e dCa r t e s i a ns y s t e mwi t hp r i n c i p l ea x e si, j, a n d k, wh i c ha r ei t sx , y -a n dz a x e s , r e s p e c t i v e l y .I nt h er i g h t h a n d e ds y s t e mt h a ti su s e di nc o mp u t e rg r a p h i c s , t h ex a x i s( i)p o i n t st ot h e r i g h t , t h ey a x i s( j)p o i n t su pa n dt h ez a x i s( k)p o i n t sbackward!Th i sme a n st h a ty o u ' r el o o k i n gi nt h en e g a t i v ez d i r e c t i o n , wh i c hma y s e e mwe i r da tf i r s t .I fy o ua b s o l u t e l ymu s th a v eaf o r wa r dp o i n t i n gk, y o uc o u l du s eal e f t h a n d e ds y s t e m.Wh i l et h i su t t e r l yd e s t r o y smy 3 di n t u i t i o n , i fy o uwa n ti tb emyg u e s t .Be f o r ey o ud ot h a t , t h o u g h , r e me mb e rt h a tt h ema pma r k st h ef l o o ro fwo r l ds p a c ea n di nar i g h t h a n d e ds y s t e m, t h et e x t u r ec o o r d i n a t e swi l l ma t c hu pn e a t l yt owo r l dc o o r d i n a t e s . 21.2.3. The camera frame Th et r a n s f o r ma t i o nt ot h ec a me r as y s t e mi sp r o b a b l yt h ema j o rh u r d l ei nt h ewh o l et h i n g .Atl e a s ti two u l db ei fi twa s n ' tf o rma t r i c e s . Re wr i t i n ge q2 1 . 1 , t h et r a n s f o r ma t i o nb e t we e nc a me r aa n dwo r l ds p a c ei sg i v e nb y (21.2) C ·xc=xw −acw Asy o uc a ne x p e c t , t h eo r i g i no fc a me r as p a c ei st h ec a me r ap o s i t i o n , acw.Th ec a me r ama t r i xC i sf o r me db yt h ep r i n c i p l ea x e so f c a me r as p a c e , wh i c ha r eu, v a n dw f o rt h el o c a l x , y -a n dz a x e s , r e s p e c t i v e l y .Th i sme a n st h a tt h ec a me r ama t r i xi sC =[ u v w] . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 193/331 28-03-13 Tonc : GBA Programming in rot13 Th eo r i e n t a t i o no ft h ec a me r awi t hr e s p e c tt owo r l ds p a c ei sd e f i n e db y3a n g l e s : pitch ( r o t a t i o na r o u n dt h ex a x i s ) , yaw ( r o t a t i o n a r o u n dt h ey a x i s )a n droll ( a r o u n dz a x i s ) .Th ec o mb i n a t i o no ft h e s eg i v eC.Tr a d i t i o n a l l y , t h er o t a t i o nd i r e c t i o no ft h e s ei ss u c ht h a ti f y o ul o o kd o wno n eo ft h e s ea x e s , ap o s i t i v ea n g l et u r n st h es y s t e mc o u n t e r c l o c k wi s e .Ho we v e r , I ' l l d ot h ee x a c to p p o s i t e , b e c a u s ei t ma k e san u mb e ro ft h i n g se a s i e r .Ad d i t i o n a l l y , Iwi l l o n l yb eu s i n gt woa n g l e s : p i t c ha n dy a w.Fo rmo d e7i ti si mp o s s i b l et oi n c o r p o r a t e r o l l i n t ot h ep i c t u r e .Wh y ?Lo o ka ti tt h i swa y : i fy o u ' r er o l l e do ny o u rs i d e , t h eg r o u n dwo u l db eo nt h er i g h to rl e f to ft h es c r e e n , wh i c h wo u l dr e q u i r eav e r t i c a l p e r s p e c t i v ed i v i s i o n , wh i c hi si mp o s s i b l et oa c h i e v es i n c ewec a no n l yc h a n g et h ea f f i n ep a r a me t e r sa tHBl a n k . Th e r e f o r e , o n l yp i t c h( θ )a n dy a w( φ)a r ea l l o we d .Iwa n tmyp o s i t i v eθa n dφt ot h e v i e wd o wna n dr i g h t , r e s p e c t i v e l y , me a n i n gIn e e dt h ef o l l o wi n gr o t a t i o nma t r i c e s : 1 0 0 (21.3a) Rx( θ )= 0 c os ( θ )s i n ( θ ) 0 −s i n ( θ )c os ( θ ) c os ( φ) 0 −s i n ( φ) 0 1 0 (21.3b) Ry( φ)= s i n ( φ) 0 c os ( φ) Fig 21.3: Ca me r ao r i e n t a t i o n{ u, v, w}i nwo r l ds pa c e { i, j, k} , g i v e nb ya n g l e sθa n dφ Bu tn o wt h en e x tp r o b l e ma r i s e s : d owed op i t c hf i r s t , o ry a w?Th a tr e a l l yd e p e n d so n wh a tk i n do fe f f e c ty o uwa n tt oh a v eand i nr e l a t i o nt owh a ts y s t e my o ud oy o u r r o t a t i o n .Th e r ei sa c t u a l l yo n l yo n eo r d e rt h a ti sp o s s i b l ef o rt h es a mer e a s o nt h a tr o l l wa s n ' ta l l o we d : y o uc a n n o th a v eav e r t i c a l p e r s p e c t i v e .Wh a tt h i sb o i l sd o wnt oi st h a tu ( t h ex a x i so ft h ec a me r af r a me )must b ep a r a l l e l t ot h eg r o u n dp l a n e , i . e . , u ymu s tb ez e r o . I no r d e rt od ot h a t , y o umu s td op i t c hf i r s t , t h e ny a w.Th i si sd e p i c t e di nf i g2 1 . 3 .Tog e taf e e l f o rt h i s : s t a n du p , t i l ty o u rh e a dd o wn ( p i t c hθ >0 ) , t h e nt u r nt oy o u rr i g h t( y a wφ>0 ) .Th ef u l l c a me r ama t r i xt h e nb e c o me s : c os ( φ) s i n ( φ) · s i n ( θ ) −s i n ( φ) · c os ( θ ) (21.4) C( θ , φ)=Ry( φ)·Rx( θ )= 0 c os ( θ ) s i n ( θ ) s i n ( φ) −c os ( φ) · s i n ( θ )c os ( φ) · c os ( θ ) As i d ef r o mb e i n gc o r r e c t , t h i sma t r i xh a st won i c ep r o p e r t i e s .Fi r s t l y , t h ec o l u mnv e c t o r sa r eo fu n i tl e n g t h .Se c o n d l y , t h ec o mp o n e n t v e c t o r sa r ep e r p e n d i c u l a r .Th i sme a n st h a tC i sa northogonal matrix, wh i c hh a st h ev e r yn i c ef e a t u r et h a tC−1=CT.Th i sma k e st h e wo r l d →c a me r at r a n s f o r ma t i o nar e l a t i v e l ys i mp l eo p e r a t i o n . On el a s tt h i n gh e r e : i fy o uwe r et or o t a t et h ec a me r as y s t e mb y1 8 0 °a r o u n di, t h i swo u l dg i v ey o uaf o r wa r dp o i n t i n gw a n dad o wn wa r d p o i n t i n gv, b o t ho fwh i c hd e c r e a s et h en u mb e ro fa wk wa r dmi n u ss i g n si nl a t e rc a l c u l a t i o n s , a tt h ee x p e n s eo fa na wk wa r dc a me r a f r a me .Wh e t h e ry o uwa n tt od ot h i si su pt oy o u . Matrix transforms and the system they occur in. Is a i dt h a tt omi mi ct h er o t a t i o n so fC y o ut ot i l ty o u rh e a df i r s t( θ ) , t h e nr o t a t ey o u rb o d y( φ) .Yo umi g h tt h i n kt h a ty o uc a n g e tt h es a mee f f e c tb yd o i n gi tt h eo t h e rwa y : t u r nf i r s t , t h e nl o o kd o wn .Ho we v e r , t h i si si n c o r r e c t . I tma yfeel t h es a me , b u ti nt h es e c o n dc a s ey o u ' dn o ta c t u a l l yb eu s i n gt h eRx( θ )t oi n v o k et h et i l t .Ama t r i xi s n ' tat h i n g i ni t s e l f , i t‘ l i v e s ’i nas p a c e .I nt h i sc a s e , b o t hRx( θ )a n dRy( φ)a r ed e f i n e di nt e r mso ft h eworld c o o r d i n a t es y s t e m, a n d wh e na p p l y i n gt h e mt h ed i r e c t i o n sf o l l o wt h ewo r l d ' sa x e s .Th et u r n t h e n t i l to r d e rwo u l du s eRx( θ )i nal o c a l f r a me , wh i c hi s al e g a l o p e r a t i o n , b u tn o tt h eo n et h a tt h ema t hr e q u i r e s . Ik n o wi t ' sas u b t l ep o i n t , b u tt h e r er e a l l yi sa ni mp o r t a n td i f f e r e n c e .Tr yv i s u a l i z i n gi twi t ha9 0 °r o t a t i o ni nb o t ho r d e r s , ma y b et h a t ' dh e l p . 21.2.4. The projection plane Toc r e a t et h ei l l u s i o no fd e p t hwen e e daperspective view.Fo rt h i s , y o un e e dacenter of projection ( COP)a n daprojection plane. Na t u r a l l y , b o t hn e e dt ob ei nc a me r as p a c e .Wh i l ey o u ' r ef r e et oc h o o s et h e s ea n ywa yy o uwa n t , y o uc a ns i mp l i f yma t t e r sb yp l a c i n g t h ec e n t e ro fp r o j e c t i o na tt h eo r i g i no fc a me r as p a c ea n dt h ep r o j e c t i o np l a n ea tad i s t a n c eD i nf r o n to ft h ec a me r a , s ot h a tt h ep l a n ei s g i v e nb yx p=( xp, yp, −D) .Ye s , t h a t ' san e g a t i v ezp, b e c a u s ewe ' r el o o k i n gi nt h en e g a t i v ez d i r e c t i o n .Th ep r o j e c t e dc o o r d i n a t e sa r et h e i n t e r s e c t i o n so ft h el i n eb e t we e nCOPa n dx c, a n dt h ep r o j e c t i o np l a n e .Si n c et h eCOPi sa tt h eo r i g i n , t h er e l a t i o nb e t we e nx ca n dx pi s (21.5) λxp=xc He r eλi sas i mp l es c a l i n gf a c t o r , t h ev a l u eo fwh i c hc a nb ed e t e r mi n e di nav a r i e t yo fwa y s , d e p e n d i n go ft h ei n f o r ma t i o na v a i l a b l ea t t h ep o i n ti ny o u rd e r i v a t i o n s .Fo re x a mp l e , s i n c ezp=−D, b yd e f i n i t i o n , weh a v eλ=−zc/ D.La t e rwe ' l l s e ea n o t h e re x p r e s s i o n .Th e i n t e r e s t i n gt h i n ga b o u tt h i se x p r e s s i o ni st h a tλi sp r o p o r t i o n a l t ot h ed i s t a n c ei nc a me r as p a c e , wh i c hi nt u r nt e l l sy o uh o wmu c ht h e file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 194/331 28-03-13 Tonc : GBA Programming in rot13 c a me r ap o s i t i o ni st ob es c a l e ddown, o rz o o me d .Th i si su s e f u l , s i n c et h es c a l i n g p a r a me t e r so ft h ea f f i n ema t r i xs c a l e sd o wna swe l l .Al s o , t h ed i s t a n c eD a t t e n u a t e st h e s c a l i n g , wh i c hme a n st h a ti ta c t sa safocus length.No t et h a twh e nzc=−D, t h es c a l ei s o n e , me a n i n gt h a tt h eo b j e c t sa tt h i sd i s t a n c ea p p e a ri nt h e i rn o r ma l s i z e . 21.2.5. Viewport and viewing volume Be f o r eIg i v et h el a s ts t e po ft h e t r a n s f o r ma t i o nt ot h es c r e e n , I h a v et os a yaf e wwo r d sa b o u t t h ev i e wp o r ta n dt h ev i e wi n g v o l u me .Asy o uc a ni ma g i n e , Fig 21.4: pe r s pe c t i v epr o j e c t i o n . y o uc a no n l ys e eac e r t a i n p o r t i o no ft h ewo r l d .Yo us e e t h ewo r l dt h r o u g har e g i o nc a l l e d t h eviewport.Th i si sa na r e ao n t h ep r o j e c t i o np l a n e , u s u a l l y r e c t a n g u l a r , t h a td e f i n e st h e h o r i z o n t a l a n dv e r t i c a l b o u n d a r i e so fwh a ty o uc a ns e e . e wi n gf r u s t u mi nc a me r as pa c e . Th eg r e e nr e c t a n g l e I np a r t i c u l a r , y o uh a v eal e f ts i d e Fig 21.5:Vi st h ev i s i b l epa r to ft h epr oj e c t i o npl a n e( i . e . , t h es c r e e n ) . ( L) , r i g h ts i d e( R) , t o p( T)a n d i b o t t o m( B) .Wi t ht h ea x e s d e f i n e da st h e ya r ea n dt h eo r i g i ni su s u a l l yc e n t e r e d( s e ef i g2 1 . 5 , i n s e t ) , weh a v eR>0>L a n dT>0>B.Yu p , i nt h i sp a r t i c u l a rc a s eL i s n e g a t i v e , a n dT i sp o s i t i v e ! Th ewi d t ha n dh e i g h to ft h ev i e wp o r ta r eW =| R−L| a n dH =| B−T| , r e s p e c t i v e l y .To g e t h e rwi t ht h ec e n t e ro fp r o j e c t i o n , t h e v i e wp o r td e f i n e st h eviewing volume ( s e ef i g2 1 . 5 ) .Fo rar e c t a n g u l a rv i e wp o r tt h i swi l l b eap y r a mi d . Mo s to ft h et i mey o uwi l l wa n tb o u n d a r i e si nd e p t ha swe l l , b e c a u s et h i n g st o on e a rwi l l o b s t r u c te v e r y t h i n ge l s ef r o mv i e w( b e s i d e s , d i v i d i n gb y0i sn e v e rg o o d ) , a n dv e r yd i s t a n to b j e c t swi l l b e c o mes os ma l l t h a tt h e ya r eb a r e l yn o t i c e a b l e , a n dwh ywa s t es oma n y c a l c u l a t i o n so nah a n d f u l o fp i x e l s ?Th e s eb o u n d a r i e si nd e p t ha r ec a l l e dt h enear ( N)a n dfar ( F)p l a n e s , a n dwi l l t u r nt h ev i e wi n g v o l u mei naf r u s t u m.Th en u mb e r sf o rt h e s ed i s t a n c e sa r eama t t e ro ft a s t e .Wh a t e v e ry o uu s e , b ea wa r et h a tt h ez v a l u e sa r ea c t u a l l y n e g a t i v e .Iwo u l dp r e f e rt oh a v et h ev a l u e so fN a n dF p o s i t i v e , s ot h a tt h eo r d e ro rd i s t a n c ei s0 >−N>−F. An o t h e rp o i n ti st h en o t i o no ft h efield of view ( FOV) .Th i si st h eh o r i z o n t a l a n g l eαt h a ty o uc a ns e e , me a n i n gt h a t (21.6) t a n ( ½α)=½W/D Ia mt o l dt h a tac o mmo n l yu s e dFOVi sa b o u t9 0 ° , wh i c hwo u l dr e q u i r eD =½W.Wi t hD =1 2 8y o ug e tc l o s ee n o u g ht ot h i s r e q u i r e me n t , wi t ht h ea d d e db e n e f i tt h a ti t ' sap o we ro f2 , b u tt h a t , o fc o u r s e , i sa ni mp l e me n t a t i o nd e t a i l .However, i ts e e mst h a t D =2 5 6i smo r ec o mmo n , s owe ' l l u s et h a ti n s t e a d . 21.2.6. The screen Th el a s ts t e pi st h eo n ef r o mt h ep r o j e c t i o np l a n eo n t ot h es c r e e n .Th i ss t e pi sa l mo s tt r i v i a l , b u tt h e a l mo s tc a nc a u s ey o ual o to ft r o u b l ei fy o u ' r en o tc a r e f u l .Th es i t u a t i o ni ss h o wni nf i g2 1 . 6 , wh e r e y o ua r el o o k i n gt h r o u g ht h ec a me r a .Th ea x e su a n dv a r et h eu pa n dr i g h ta x e so ft h ec a me r a s y s t e m, wh i l et h eg r e e na r r o wsd e n o t et h ex -a n dy a x e so fs c r e e ns p a c e .An di fy o uh a v ep a i d a t t e n t i o nt oa n yo ft h et u t o r i a l s , y o us h o u l dk n o wt h a tt h es c r e e n ' sy a x i sp o i n t sdown.Th i si s b u g f e s tn u mb e r1 .Al s o , t h eo r i g i n so fc a me r aa n ds c r e e ns p a c ed i f f e r .Si n c et h es c r e e n c o r r e s p o n d st ot h ev i e wp o r t , t h eo r i g i no ft h es c r e e ni nc a me r a / p r o j e c t i o ns p a c ei sasp=( L, T, −D) . Bec a r e f u l n o tt or e v e r s et h es i g n sh e r e ;t h a two u l db eb u g f e s tn u mb e r2 .Al s or e me mb e rt h a ts i n c e t h i si si nc a me r as p a c e , Li sn e g a t i v ea n dT i sp o s i t i v e .Ta k i n gb o t ht h ei n v e r t e dv e r t i c a l a x i sa n dt h e Fig 21.6:scr e e ns pa c ev sc a me r as pa c e o r i g i no fs c r e e n s p a c ei nmi n d , weh a v e (21.7) S( 1, −1, 1) · xs=xp−asp Th es c a l i n gma t r i xr e v e r s e st h es i g no ft h ey a x i s .Wec o u l dh a v ea v o i d e dt h ee x t r ama t r i xi fweh a dr o t a t e dt h ec a me r af r a meb y a n o t h e r1 8 0 ° , i nwh i c hc a s ev wo u l dh a v ep o i n t e dd o wna n dw wo u l dh a v ep o i n t e df o r wa r d .Bu tId i d n ' t , s owe ' l l h a v et ol i v ewi t hi t h e r e .Al s o , s i n c et h eo r i g i no ft h es c r e e ni nc a me r as p a c e , i sasp=( L, T, −D) , t h es c r e e np o s i t i o ni sx s=( xs, ys, 0 ) , i no t h e rwo r d szsi s a l wa y sz e r o .I fy o uwa n tt oc h e c kwh e t h e re v e r y t h i n gi sOK, s e ei ft h ec o r n e r so ft h ev i e wp o r tg i v et h er i g h ts c r e e nc o o r d i n a t e s . 21.2.7. Theory summary An dt h a t ' sb a s i c a l l yi t , p h e w.Si n c ei tt o o kt h r e ep a g e st og e th e r e , I ' l l r e p e a tt h emo s ti mp o r t a n tt h i n g s .Fi r s t , t h ema i ne q u a t i o n swen e e d a r e : (21.8a) λ C ·xp =xw −acw file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 195/331 28-03-13 Tonc : GBA Programming in rot13 (21.8b) S( 1, 1, 1)·xs=( xp−asp) wh e r e xw xp xs acw asp C λ c o o r d i n a t e si nwo r l ds p a c e ; c o o r d i n a t e so nt h ep r o j e c t i o np l a n e , x p=( xp, yp, −D) ; c o o r d i n a t e so nt h es c r e e n , x s=( xs, ys, 0 ) ; t h el o c a t i o no ft h ec a me r ai nwo r l ds p a c e ; t h el o c a t i o no ft h es c r e e no r i g i ni nc a me r as p a c es p a c e , asp=( L, T, −D) ; t h ec a me r ama t r i x , a sf u n c t i o no fp i t c hθa n dy a wφ: C =Ry( φ) ·Rx( θ ) ; t h es c a l i n gf a c t o r .I t sv a l u ec a nb ed e t e r mi n e db yt h eb o u n d a r yc o n d i t i o n s . Re me mb e rt h e s ee q u a t i o n sa n dt e r ms , f o rIwi l l r e f e rt ot h e mo f t e n .Th eb r e a kb e t we e ne q2 1 . 8 aa n de q2 1 . 8 bi sb yd e s i g n : a l l t h er e a l i n f o r ma t i o ni si ne q2 1 . 8 a ;e q2 1 . 8 bi sj u s taf i n a l s t e pt h a tn e e d st ob et a k e nt oc o mp l e t et h et r a n s f o r ma t i o n .I nt h er e ma i n d e ro ft h et e x t , Iwi l l ma k ef r e q u e n tu s eo fe q2 1 . 8 aa n dl e a v eo u te q2 1 . 8 bu n l e s sn e c e s s a r y .Ot h e ri n t e r e s t i n gt h i n g st ok n o w: Wo r l ds y s t e mS w ={ i, j, k}a n dc a me r as y s t e mS c={ u, v, w}a r er i g h t h a n d e dCa r t e s i a nc o o r d i n a t es y s t e ms .Ase x p e c t e d , t h e c o l u mn so fc a me r ama t r i xC a r et h ep r i n c i p l ea x e so fS c: C =[ u v w] ; Th ev i e wp o r ta n dv i e wi n gf r u s t u ma r ei nc a me r as p a c e , me a n i n gt h a tt h e i rb o u n d a r i e sa r et o o .Th i sme a n st h a t R >0>L ( h o r i z o n t a l ) T >0>B ( v e r t i c a l ) 0>−N >−F ( d e p t h ) I fweu s et h eGBAs c r e e ns i z ea sab a s i s( W =2 4 0 , H =1 6 0 ) , a n dD =2 5 6 , r e a s o n a b l ev a l u e sf o rt h ev i e wi n gf r u s t u m b o u n d a r i e sa r e L =−1 2 0 R =−1 2 0 T =8 0 B =−8 0 N =2 4 F =1 0 2 4 Bu ty o uc a np i c ko t h e r si fy o uwa n t . 21.3. Horizon and backdrop Ta k et h ee s s e n t i a l mo d e7c a s e : af l o o ri np e r s p e c t i v e .Du et ot h ep e r s p e c t i v ed i v i s i o n , t h ed i s t a n tp a r t so ft h ef l o o rwi l l a p p r o a c ha s i n g l el i n e : t h ehorizon.Si n c et h ema pr e a l l yi sj u s taf l o o r , t h eh o r i z o nr e a l l ywi l l b ej u s tt h a t : o n eh o r i z o n t a l l i n e .Th es p a c ea b o v et h a ti s u s u a l l ye mp t y , b u tt oma k ei tal i t t l el e s sb l a n d , wewi l l u s eabackdrop: ap a n o r a mav i e wo ft h ed i s t a n te n v i r o n me n tt h a tmo v e sa l o n g wi t ht h ec a me r a ' sr o t a t i o n . 21.4. Finding the horizon Ro u g h l yp u t , t h eh o r i z o ni swh e r ez=−∞.I fy o uh a v el i n e so nt h ef l o o r , t h eh o r i z o ni swh e r ea l l p a r a l l e l l i n e ss e e mt ome e t : t h ev a n i s h i n g l i n e .Na t u r a l l y , i fy o uo n l yh a v eaf l o o r , t h e ny o us h o u l do n l yd r a wi tb e l o wt h eh o r i z o na n dt h eg r a p h i c sa b o v ei ts h o u l db ep a r to fa s k y b o x .I ' ms u r ey o u ' v es e e nt h i si nt h eo r i g i n a l Ma r i oKa r ta n do t h e rmo d e7r a c e r s .Si n c ewe ' r el i mi t e dt oar o l l l e s sc a me r a , t h e h o r i z o nwi l l a l wa y sb eah o r i z o n t a l l i n e : o n es c a n l i n eys,h.Tof i n di t , a l l weh a v et od oi st a k et h eyc o mp o n e n to fe q2 1 . 8 aa n dr e a r r a n g e t h et e r mst og e t (21.9a) λ( vyyp, =−acw, h−wyD) y yp, =( w D λ )/vy h y −a c w, y/ An di fwewe r et ot a k eo u rh o r i z o na ti n f i n i t y , t h e mλ=−∞, wh i c hwo u l dr e d u c ee q2 1 . 9t o (21.9b) y p, vy=D t a n ( θ ) h=D wy/ Ho we v e r , y o un e e dt ot h i n ka b o u twh e t h e ry o uwa n tt ou s et h i ss i mp l i f i e de q u a t i o n .Atv e r yl a r g eλ , t h eg a p si nd i s p l a y e dma pp o i n t sa r e s ol a r g et h a ty o u ' r ee f f e c t i v e l ys h o wi n gn o i s e , wh i c hc a nb ev e r yu g l yi n d e e d .Ab e t t e rwa ywo u l db ema k i n gu s eo ft h ef a rc l i p p i n g p l a n ea tzc=−F.I nt h a tc a s e , λ=F/D a n dwec a nu s ee q2 1 . 9t oc a l c u l a t et h eh o r i z o n , wh i c hwi l l b es o me t h i n gl i k e (21.9c) y p, ( F wy−acw, /vy h=D/F · y) Ase x p e c t e d , i fF =−∞t h e ne q2 1 . 9 cr e d u c e st oe q2 1 . 9 b .Re g a r d l e s so fwh e t h e ry o uc h o s eaf i n i t eo fi n f i n i t ezc, t h eh o r i z o nwi l l b ea t s c a n l i n eys,h=T − yp,h. 21.4.1. Using the horizon Th eh o r i z o nma r k st h el i n eb e t we e nt h ema pa n d‘ f a rf a ra wa y ’ : b e t we e nt h ef l o o ra n dt h eb a c k d r o p .Th ef l o o rs h o u l db ea na f f i n e b a c k g r o u n d , o b v i o u s l y ;f o rt h eb a c k d r o p , wewi l l u s ear e g u l a rb a c k g r o u n d , a l t h o u g ht h a t ' sn o tr e q u i r e d .Wh a twen e e dt oawa yt o s wi t c hb e t we e nt h et woa tt h eh o r i z o ns c a n l i n e .Th es i mp l e s twa yi sb yHBl a n ki n t e r r u p t : o n c et h eh o r i z o ns c a n l i n ei sr e a c h e d , ma k et h e s wi t c hb e t we e nf l o o ra n db a c k d r o ps e t t i n g si nt h eBGc o n t r o l r e g i s t e r sa n dp e r h a p si n i t i a t eHDMAf o rt h ea f f i n ep a r a me t e rt r a n s f e r si f y o uc h o s et ou s eDMAf o rt h a t . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 196/331 28-03-13 Tonc : GBA Programming in rot13 Swi t c h i n gb e t we e nt h eb a c k d r o pa n df l o o rb a c k g r o u n d si sa c t u a l l yt r i c k i e rt h a ni ts o u n d s .Yo uc o u l d , f o re x a mp l e , h a v eas e p a r a t e b a c k g r o u n df o re a c ha n de n a b l e / d i s a b l et h e md e p e n d i n go ny o u rn e e d s .Th ep r o b l e mi st h a ti ts e e mst ot a k ea b o u t3s c a n l i n e sb e f o r ea b a c k g r o u n di sf u l l ys e tu pi nh a r d wa r e( s e ef o r u m: 1 3 0 3 ) , s oy o u ' l l s e ec r a pd u r i n gt h a tt i me .I no t h e rwo r d s , t h i ss o l u t i o ni sn og o o d . Ano t h e rwa ywo u l db et oh a v eo n eb a c k g r o u n df o rb o t ha n ds wi t c ht h ev i d e o mo d ef r o m0t o1o r2 .Th i swo n ' tg i v ey o u3l i n e so f g a r b a g e , b u tn o wa n o t h e rp r o b l e ma r i s e s : c h a n c e sa r ev e r yh i g ht h a tt h eb a c k d r o pa n df l o o rh a v ev e r yd i f f e r e n tt i l e sa n dma pa t t r i b u t e s . Th i si se a s yt os o l v et h o u g h : s i mp l yc h a n g et h es c r e e n( a n dc h a r )b a s eb l o c k si nREG_BGxCNT. Fig 21.7: S wi t c hv i d e o mo d ea n db a c k g r o u n dpa r a me t e r sa tt h e h o r i z o n . Fig 21.8: pe e l i n gapa n o r a mi cv i e wf r o mac yl i n d e r . 21.4.2. Making and placing the backdrop Th es p a c ed i r e c t l ya b o v et h eh o r i z o ni sf o rt h eb a c k d r o p .Yo up r o b a b l ywa n tan i c ei ma g eo fad i s t a n tt o wno rt r e el i n eo v e rt h e r e , n o t j u s tab o r i n ge mp t ys k y .Th eb a c k d r o po f f e r sap a n o r a mi cv i e w, wh i c hc a nb ec o n s i d e r e dama pp a i n t e do nt h ei n s i d eo fac y l i n d e ra n d t h e np e e l e do f ft oan o r ma l 2 Ds u r f a c e( s e ef i g2 1 . 8 ) .Th ei d e ai st op u tt h a ts u r f a c eo nab a c k g r o u n da n dt h es c r o l l a r o u n d . Ve r t i c a l l y , t h eb o t t o mo ft h eb a c k g r o u n ds h o u l dc o n n e c tt ot h eh o r i z o n .Be c a u s er e g u l a rb a c k g r o u n d su s ewr a p a r o u n dc o o r d i n a t e s t h i si sa c t u a l l yq u i t ee a s y : p l a c et h eg r o u n d l e v e l o ft h eb a c k d r o pa tt h eb o t t o mo fas c r e e n b l o c ka n ds e tt h ev e r t i c a l o f f s e tt o−ys,h. Ho r i z o n t a l l y , t h e r ea r es e v e r a l i s s u e st ob ea wa r eo f .Th ef i r s ti st h ewi d t ho ft h ema p , wh i c hi ss i mp l yt h ep e r i me t e rP o ft h e c y l i n d e r .Aswes h o u l dh a v eas c r o l l e daf u l l ma p ' swi d t hf o ra3 6 0 °r o t a t i o n , t h ec o r r e c ts c r o l l r a t i op e ru n i ta n g l ei ss i mp l yP/ 2 π=R, t h e r a d i u s .I np r i n c i p l e , Ri sa r b i t r a r y , b u tt h eb e s tr e s u l tc a nb eh a dwh e nt h ef i e l do fv i e wf o r me db yt h ea n g l eo ft h ep a n o r a ma( αp=W/ R) , i se q u a l t ot h ec a me r af i e l d o f v i e wa n g l eαcf r o me q2 1 . 6 .I fa l l i sr i g h twes h o u l dh a v eαp=αc=α. α=2· a r c t a n ( ½W/ D) α=W/ R (21.10) R =½W /a r c t a n ( ½W/ D) 2 ≈D / (1−( ½W/ D) / 3) Th a tl a s ta p p r o x i ma t i o ns t e msf r o mt h ef i r s tc o u p l eo ft e r mso ft h eTa y l o rs e r i e so ft h ea r c t a n g e n t .I n t e r e s t i n g l ye n o u g h , e v e nR ≈D s e e mss o me wh a ta d e q u a t e .An y wa y , f i l l i n gi nW =2 4 0a n dD =2 5 6g i v e sP =1 7 2 0 , wh i c hi s n ' tav e r yc o n v e n i e n tma ps i z e , i si t ?No w, i ti sp o s s i b l et oc r e a t eama po fa n ys i z ea n du p d a t eVRAM i fweg oo u t s i d et h es c r e e n b l o c k ' sb o u n d a r i e s( c o mme r c i a l g a me sd oi ta l l t h et i me ) , b u td o i n gs owo u l dd i s t r a c tf o rt h es u b j e c ta th a n d , s oy o uk n o wwh a t ?We ' r eg o i n gt ob e n dt h er u l e sab i ta n dj u s tf o r c e P =1 0 2 4 . “ Wa i tas e c. . .y o uc a n ' td ot h a t ! ”We l l , y e sIc a na c t u a l l y .I ' mn o tsupposed t od oi t , b u tt h a t ' sa n o t h e ri s s u e .Th ef a c to ft h ema t t e r i st h a t , Id o n ' tt h i n kt h e r ei sasingle mo d e7g a met h a ts c r o l l st h eb a c k d r o pp r o p e r l y !Fo re x a mp l e , t h eMa r i oKa r t ' so f t e nu s emu l t i p l e b a c k g r o u n d swi t hd i f f e r e n ts c r o l l i n gs p e e d si nt h e i rb a c k d r o p s , wh i c hi sa b s o l u t e l yr i d i c u l o u s , ma t h e ma t i c a l l ys p e a k i n g , b e c a u s el o o k i n g a r o u n dd o e s n ' tc h a n g er e l a t i v el i n e so fs i g h t .Bu tIg u e s sn o b o d yn o t i c e do ra tl e a s tn o b o d yc a r e s .Wh a tI ' mt r y i n gt os a yi s : we ' r ei n g o o dc o mp a n y:P So , wej u s td e f i n eap e r i me t e rv a l u ea n dwi t hi tb a c k d r o pma p wi d t ho u r s e l v e s .I nt h i sc a s eI ' mg o i n gt ou s eP =1 0 2 4 , wh i c hi sa n i c er o u n dn u mb e ra n df o rwh i c hwec a nu s ea5 1 2p xwi d et i l e ma pwi l l e f f e c t i v e l ye n du pa sap a n o r a mawi t h1 8 0 °r o t a t i o n a l s y mme t r y .Ta k i n gi n t oa c c o u n tt h ec i r c l ep a r t i t i o n i n go f2 π⇔ 1 0 0 0 0 h , t h es c r o l l i n gv a l u ei ss i mp l yφ* P/ 1 0 0 0 0 h=φ/ 6 4 .We ' l l h a v et o o f f s e tt h i sb yL a swe l l b e c a u s eIwa n tt oma pφ=0t od u en o r t h .Th ef i n a l p o s i t i o no ft h eb a c k d r o pi sg i v e ni n2 1 . 1 1 . (21.11) dx =φ/ 64+L dy =−ys, h file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 197/331 28-03-13 Tonc : GBA Programming in rot13 21.5. The floor 21.5.1. Affine parameters for the floor Eq2 1 . 8d e s c r i b e st h ewo r l d ↔s c r e e nt r a n s f o r ma t i o nb u tt h a ti n f o r ma t i o nu s e s3 Dv e c t o r s , wh i l et h eGBAo n l yh a sa2 ×2a f f i n ema t r i xP a n da2 Dd i s p l a c e me n tv e c t o rdx a ti t sd i s p o s a l .Soweh a v es o mer e wr i t i n gt od o .No w, Ic o u l dg i v ey o ut h ef u l l d e r i v a t i o n , 2 d ↔3 d c o n v e r s i o n sa n da l l , b u ts o me t h i n gt e l l smey o ur e a l l yd o n ' twa n tt os e et h a t .Soi n s t e a d , I ' l l g i v ey o ut h es e to fe q u a t i o n sy o un e e dt o s o l v e , a n dh i n t so nh o wt od ot h a t . λ C ·xp =xw −acw (21.12) S( 1, −1, 1)·xs=( xp−asp) P·q =p − dx Th ef i r s tt woe q u a t i o n sa r ej u s te q2 1 . 8a g a i n , Ij u s tt h e ml i s tf o rc o mp l e t e n e s s .Th el a s te q u a t i o ni st h er e l a t i o nb e t we e ns c r e e np o i n tq a n dma pp o i n tp f o ra na f f i n ema p , a ne q u a t i o nt h a ts h o u l db ef a mi l i a rb yn o w.No w, r e me mb e rt h a to u rma pl i e so nt h ef l o o r , i no t h e r wo r d sp =( xw, zw) .Th e2 Ds c r e e np o i n tq i s , o fc o u r s e , s i mi l a rt ot h e3 Ds c r e e nv e c t o ro fx s.Th eo n l yt h i n gt h a ty o uh a v et or e me mb e r i st h a twh e nwr i t i n gt oREG_BGxY, t h el e f to ft h ec u r r e n ts c a n l i n ei st a k e na st h eo r i g i n , s ot h a te f f e c t i v e l yq =( xs, 0 ) , wh i c hi nt u r n me a n st h a tp ba n dp da r eo fn oc o n s e q u e n c e .Th ev a l u e so ft h eo t h e re l e me n t so fP a r es i mp l yt h ex-a n dzc o mp o n e n t so ft h es c a l e d c a me r ax a x i s , λ u.I fy o uu s et h e s ev a l u e s , y o uwi l l s e et h a te v e n t u a l l yy o uwi l l e n du pwi t ha ne x p r e s s i o nt h a tc a nb e s tb es u mme du p b y : (21.13) dx' =acw +λC ·b wh e r e dx, 0 , dy) dx' =( L, T−ys, −D) b =( Ev e r y t h i n gy o un e e df o rt h ed i s p l a c e me n ti sn e a t l yp a c k e di n t ot h i so n ee q u a t i o n , n o wwen e e dt od i s a s s e mb l ei tt oc o n s t r u c tt h e a l g o r i t h m.Fi r s t , wec a nu s et h eyc o mp o n e n to fdx' t oc a l c u l a t eλ .On c eweh a v et h a twec a nu s ei tt oc a l c u l a t et h eo t h e rt woe l e me n t s , i . e . , t h ea c t u a l a f f i n eo f f s e t s .Th ea f f i n ema t r i xwa sa l r e a d yg i v e ne a r l i e r . Eq2 1 . 1 4g i v e sa l l t h er e l a t i o n se x p l i c i t l y , t h o u g hIh o p ey o u ' l l f o r g i v emewh e nIp r e f e rt h ec o n c i s e n e s so fe q2 1 . 1 3my s e l f . λ =a cw,y/(( ys−T) vy+Dwy) p a =λu x (21.14) p c =λu z dx =a cw,x+λ(Lu x+( T−ys) vx−Dwx) dy =a cw,z+λ(Lu z+( T−ys) vz−Dwz) No t et h a ti fwet a k et h et o pa t0a n dn op i t c h( T=0a n dθ =0 )weh a v ee x a c t l yt h es a mer e s u l ta si nt h ef i r s tmo d e7c h a p t e r , a n di fwe l o o ks t r a i g h td o wn( θ =9 0 ° ) , t h ewh o l et h i n gr e d u c e st oas i mp l es c a l i n g / r o t a t i o na r o u n dp o i n t( −L, T) , wh i c hi se x a c t l yi ts h o u l db e . Eq2 1 . 1 4i st h eg e n e r a l e q u a t i o nf o rmo d e7 ;f o rt h ei mp l e me n t a t i o n , y o uc a no f t e nma k ean u mb e ro fs h o r t c u t st h a ts p e e du pc a l c u l a t i o n , b u twe l l g e tt ot h a tl a t e r . 21.5.2. Distance fogging I nt h er e a l wo r l d , l i g h tc o mi n gf r o mf a ra wa yo b j e c t sh a st ot r a v e l t h r o u g ht h ea t mo s p h e r e , wh i c hs c a t t e r st h ep h o t o n s , a t t e n u a t i n gt h e b e a m.Wh a ty o u ' l l e n du ps e e i n gi sp a r t l yt h eo b j e c ti t s e l fa n dp a r t l yt h ea mb i e n tc o l o r , a n dt h ef u r t h e rt h eo r i g i n a l o b j e c t , t h es ma l l e ri t s c o n t r i b u t i o ni s .Be c a u s es u c he f f e c ti smo s te a s i l yv i s i b l ei nf o gc o n d i t i o n s , I ' l l c a l l t h i se f f e c tfogging. Fo g g i n go f f e r sah i n to fd i s t a n c ea n di n c l u d i n gi tc a ni n c r e a s et h es e n s eo fd e p t h .Al s o , i tc a nh i d eo b j e c t sp o p p i n gi n t ov i e wa s t h e y ' r el o a d e d .GBAwi s e , i tc a nb ei mp l e me n t e db yu s i n gd i f f e r e n ta l p h a b l e n d sa te v e r ys c a n l i n e . Th ef u n d a me n t a l e q u a t i o nf o rt h i si st h ef o l l o wi n gd i f f e r e n t i a l e q u a t i o n : d I =−I k( ν )ρd z wh e r eI i st h ei n t e n s i t y ;k( ν )i st h ea b s o r p t i o nc o e f f i c i e n to ft h eme d i u m, wh i c hd e p e n d so nt h ef r e q u e n c yo ft h el i g h t , νa n dp o s s i b l y p o s i t i o n ;ρi st h ed e n s i t ya n dz i st h ed i s t a n c e .So l v i n gt h i swo u l dl e a dt oa ne x p o n e n t i a l d e c a yo v e rd i s t a n c e .An dId ome a nr e a l d i s t a n c e , wi t hs q u a r e sa n dr o o t sa n de v e r y t h i n g . Fo r t u n a t e l y , wed o n ' th a v et ou s es o me t h i n gt h a tc o mp l i c a t e d ;a l l wer e a l l yn e e di ss o mef u n c t i o n a l r e l a t i o nt h a tg i v e s0a ti n f i n i t ya n d 1c l o s eu p .I n t e r e s t i n g l ye n o u g h , wea l r e a d yh a v es o me t h i n gl i k et h a t , n a me l yλa sf u n c t i o no ft h es c a n l i n e( s e e2 1 . 1 4 ) .Th i si sb a s i c a l l ya h y p e r b o l a , a l l y o uh a v et od ot h e ni sf i d d l ewi t hs c a l e r sa n do f f s e t sab i tt og e ts o me t h i n gt h a tl o o k sn i c e .I nmyc a s e , λ * 6 / 1 6s e e mst o wo r kwe l l e n o u g h . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 198/331 28-03-13 Tonc : GBA Programming in rot13 Fig 21.9: f o go f f( l e f t )a n do n( r i g h t ) . Fi g2 1 . 9s h o wss c r e e n s h o t swi t ha n dwi t h o u tt h ef o g g i n ge f f e c ta ss e e nf r o maf a i r l yh i g ha l t i t u d e .Th ed i s t a n c et ot h ef l o o ri sr e l a t i v e l y s ma l l a tt h eb o t t o mo ft h es c r e e n , s ot h o s ea r es t i l l v e r yv i s i b l e .Att h eh o r i z o n , t h ef l o o ri sc o mp l e t e l yo b s c u r e db yt h eo r a n g ef o g ;wh i c h i sa c t u a l l yag o o dt h i n g , a st h el i n e sn e a rt h eh o r i z o na r eu s u a l l yn o tmu c ht ol o o ka ta n y wa y . Byt h ewa y , n o t et h a tIs a i dorange f o g .I fy o u ' dp a i da t t e n t i o ni nt h eg r a p h i c se f f e c t sc h a p t e rwi l l k n o wt h a tt h eGBAo n l yh a s f a d i n gmo d e sf o rwh i t ea n db l a c k .Ne v e r t h e l e s s , f a d e st oa na r b i t r a r yc o l o ra r ev e r ymu c hp o s s i b l e , b u tI ' l l e x p l a i no n c eweg e tt ot h e i mp l e me n t a t i o n .Wh i l ey o up o n d e ro v e rh o wi tc a nb ed o n e , I ' l l mo v eo nt o3 Ds p r i t e s . 21.6. Sprites Sp r i t e sa n d3 Da r eas t r a n g ec o mb i n a t i o n .Byt h e i rv e r yn a t u r e , s p r i t e sa r e2 Do b j e c t s–l i k es t i c k e r ss t u c ka g a i n s tt h ev i e wp o r t( i . e . , t h e s c r e e n ) .Toma k et h e ma p p e a rp a r to ft h e3 Dwo r l d , y o uh a v et oma k et h e mmo v eo v e rt h es c r e e ni ns u c hawa yt h a tt h e ya p p e a rt o mo v ewi t ht h ewo r l da n ds c a l et h e ma c c o r d i n gt ot h e i rd i s t a n c e .On c ea g a i n , t h eb a s i co ft h i si se q2 1 . 8 , b u tt h e r ei sc o n s i d e r a b l ymo r e t oi t . Fo u rt o p i c smu s tb ec o v e r e dh e r e .Th ef i r s ti ss p r i t epositioning.Eq2 1 . 8wi l l wo r ka tp o i n t / p i x e l l e v e l , a n das p r i t ei sas i mp l e r e c t a n g l e .Wh i l ei t ' sp o s s i b l et or e wr i t et h es p r i t e ' sp i x e l st owo r ka r o u n dt h a t , i tk i n do fd e f e a t st h ep u r p o s eo fu s i n gs p r i t e si nt h ef i r s t p l a c e .I n s t e a d , we ' l l l i n ko n ep o i n to nt h eo b j e c tt ot h ewo r l dc o o r d i n a t eo ft h es p r i t ea n ds e tt h eOAM p o s i t i o na n dma t r i xt o a c c o mmo d a t et h i s .Th i si sb a s i c a l l yt h et h e o r yo fa n c h o r i n gd i s c u s s e di nt h ea f f i n eo b j e c tc h a p t e r . Ne x tu p : s p r i t eculling.On c ey o uh a v et h ec o r r e c tOAM p o s i t i o n sy o uc a n ' tu s et h e ma si s , y o uh a v et oma k es u r et h es p r i t ei so n l y a c t i v ei fi ti sa c t u a l l yv i s i b l ei n s i d et h ev i e wp o r t .I fn o t , i ts h o u l db ed i s a b l e d . Th e nt h e r e ' st h ema t t e ro fs p r i t eanimation.Co n s i d e rTo a d ' sk a r ti nf i g2 1 . 1 0 , wh i c hh a st h ec o r r e c ta n c h o r e dp o s i t i o n , b u tn o ma t t e rwh i c ha n g l ey o ul o o ka ti t , i t ' l l a l wa y ss h o wt h es a mes i d e .Toma k ei tl o o ka si fy o uc a na c t u a l l ymo v ea r o u n dt h eo b j e c t , we ' l l u s ed i f f e r e n tf r a me so fa n i ma t i o nt os h o wd i f f e r e n ts i d e s . La s t l y , s p r i t esorting.Byd e f a u l t , o b j e c t swi l l b eo r d e r e da c c o r d i n gt ot h eo b j e c t s ' n u mb e r s : o b j 0o v e ro b j 1 , o v e ro b j 2 , e t c .Al wa y s l i n k i n gas p r i t et ot h es a meo b j e c tme a n st h a tt h eo r d e rwo u l db ewr o n gi fy o ul o o ka tt h e mf r o mt h eo t h e rs i d e , s owen e e dt os o r tt h e m b yd i s t a n c e . Th o s ea r et h ema i ni s s u e st od e a l wi t h .Th e r ea r eaf e wo t h e r s , l i k ep l a c i n gas h a d o w, a n du s i n gp r e s c a l e do b j e c t st og e ta r o u n dt h e h a r d wa r el i mi t a t i o no f3 2a f f i n ema t r i c e s , b u tt h e s ea r ef a i r l ye a s yi ft h eo t h e rp o i n t sa r ea l r e a d yt a k e nc a r eo f .On et h i n gIwi l l d i s c u s s a swe l l i swh a tIc a l l o b j e c tnormalization: a p p l y i n ga ne x t r as c a l i n gf o ro b j e c t ss ot h a tt h e yd o n ' tg r o wt o ob i gf o rt h e i rc l i p p i n g r e c t a n g l e . Fig 21.10: a n c h o r e ds p r i t e .Th ep o s i t i o ni sg o o d , b u tn oma t t e rh o wy o ut u r n , To a d a l wa y st u r n sa wa y .Ma y b ei t ' st h eh a t . 21.6.1. Positioning and anchoring Po s i t i o n i n gs p r i t e sc o n s i s t so ft wof a c e t s .Th ef i r s ti st ot r a n s f o r mt h es p r i t e swo r l dp o s i t i o nx w t oap o s i t i o no nt h es c r e e nx s.Af t e rt h a t , y o un e e dt ou s et h a tp o i n tt od e t e r mi n et h emo s ta p p r o p r i a t eOAM c o o r d i n a t e s . Th ef i r s tp a r ti sj u s ta n o t h e ra p p l i c a t i o no fe q2 1 . 8a g a i n , o n l yi nr e v e r s e .No r ma l l y , i n v e r t i n g3 Dma t r i xi sap a r t i c u l a r l yu n f u n p r o c e s s , b u tt h ec a me r ama t r i xh a p p e n st ob ea no r t h o n o r ma l ma t r i x .Anorthonormal matrix i sama t r i xo fwh i c ht h ec o mp o n e n t v e c t o r sa r eo r t h o g o n a l ( p e r p e n d i c u l a rt oe a c ho t h e r )a n dh a v eal e n g t ho f1 .Th en e a tt h i n ga b o u ta no r t h o n o r ma l ma t r i xi st h a ti t s −1 file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm T 199/331 28-03-13 Tonc : GBA Programming in rot13 i n v e r s ei ss i mp l yi t st r a n s p o s e : C (21.15) −1 T =C .Th a tl e a d su st ot h ef o l l o wi n ge q u a t i o n s : xp=CT·( xw −acw)/λ xs=S( 1, −1, 1)·( xp−asp) Th eo n l yr e a l u n k n o wnh e r ei sλ , wh i c hwec a nc a l c u l a t eb yu s i n gt h ef a c tt h a tzp=−D.No wl e tt h ed i s t a n c eb e t we e nc a me r aa n d s p r i t eb er =x w −acw;u s i n gC =[ u v w] , wef i n d λ =−w· r /D xp=u· r /λ yp=v· r /λ Fi n d i n gt h es c r e e np o s i t i o no fx w i st r i v i a l a f t e rt h a t .An dn o wt h ea n c h o r i n gp a r t .I n s t e a do fs t i c k e r s , t h i n ko fo b j e c t s a sp i e c e so fp a t e rt ob et a c k e do n t oab o a r d( i . e . , t h es c r e e n ) .Th et a c kg o e st h r o u g ho n es p o to ft h eo b j e c t , a n dt h a t s p o ti sf i x e dt ot h eb o a r d .Th a ts p o ti st h eanchor.Fo ra f f i n eo b j e c t si t ' sn o tq u i t ea ss i mp l ea st h a t , b e c a u s eweh a v e t os p e c i f yOAM c o o r d i n a t e sr a t h e rt h a na n c h o rc o o r d s , s ot h e r ei ss o mema t hi n v o l v e di nh o wt oe x p r e s st h eOAM c o o r d i n a t e sx i nt e r mso ft h et e x t u r ea n c h o rp0a n dt h es c r e e na n c h o rq0.Th i st h e o r ywa sc o v e r e di nt h ea f f i n e o b j e c tc h a p t e r , wh i c hl e dt oe q2 1 . 1 6 .Th eo t h e rq u a n t i t i e st h e r ea r es i z eo ft h eo b j e c t s , s =( w, h) , a n dm wh i c hi s½ Fig 21.11:a f o rn o r ma l a f f i n eo b j e c t sa n d1f o rd o u b l e s i z ea f f i n eo b j e c t s . 3 2 ×3 2s pr i t e , wi t ht h ea n c ho rp0 r e l a t i v et ot he t o pl e f t . (21.16) x =q0−ms −P −1·( p0−½s) No wt h et a s ki st ol i n kt h ed a t aweh a v et ot h i se q u a t i o n .Th es c r e e na n c h o rq0i sj u s tx s.Th et e x t u r ea n c h o rp0i st h e p i x e l i nt e x t u r es p a c ey o uwa n tt ok e e pf i x e da n di sy o u r st oc h o o s e .Fo rt h ek a r t s p r i t e , i tma k e ss e n s et op u ti tn e a rt h eb o t t o mo ft h e k a r t , a si sd e p i c t e di nf i g2 1 . 1 1 .‘ Ve c t o r ’s i sg i v e nb yt h es i z eo ft h eo b j e c t , wh i c hi nt h i sc a s ei s( 3 2 , 3 2 )a n db e c a u s eI ' mc h o o s i n gt o a l wa y su s ed o u b l e s i z eo b j e c t sh e r e , m=1 .Th ePma t r i xi sj u s tas c a l i n gb yλ , u n l e s sy o uwa n tt oa d do t h e rt h i n g sa swe l l .Al l t h a t r e ma i n st h e ni sj u s tt of i l l i ti nt h en u mb e r s . 21.6.2. Sprite culling Culling i st h ep r o c e s sr e mo v i n ga n yp a r to ft h ewo r l dt h a tc a n n o tb es e e n .I n t h i sc a s e , i tme a n sr e mo v i n gt h o s es p r i t e st h a td on o tf a l l wi t h i nt h ev i e wi n g v o l u me .Th i si sav e r ys ma r tt h i n gt od o , a n di tma k e se v e nmo r es e n s ef o r s p r i t e s , b e c a u s en o td o i n gs owo u l ds e r i o u s l ys c r e wt h i n g su pb e c a u s eOAM c o u l d n ' tc o p ewi t ht h ep o s s i b l er a n g eo fx s. Th ef i r s tt h i n gt od owo u l db ead i s t a n c ec h e c k : i ft h eo b j e c ti st o of a ra wa y , i ts h o u l dn o tb es e e n .I t ' sa l s oag o o di d e at oh a v ean e a r p l a n ed i s t a n c ec h e c k . Th e ny o uh a v et ot e s ti tf o ri n t e r s e c t i o n swi t ht h ev i e wp o r t .Ea c hs p r i t ei s b o u n d e db yac e r t a i nr e c t a n g l eo nt h ep r o j e c t i o np l a n ea n di ft h i si sc o mp l e t e l y o u t s i d et h ev i e wp o r t , t h eo b j e c ts h o u l dn o tb er e n d e r e d . Fi g2 1 . 1 2s h o wsaf e we x a mp l e so ft h i s .Ob j e c t sa a n db h a v ea l r e a d yb e e n p r o j e c t e do n t ot h ep r o j e c t i o np l a n e .Ob j e c ta i so u t s i d et h ev i e wp o r t , a n ds h o u l d b ed i s a b l e d .Ob j e c tb i sp a r t i a l l yv i s i b l ea n ds h o u l db er e n d e r e d .Ob j e c tc i sn o t Fig 21.12: Vi e wf r u s t u mwi t hs pr i t e sa, ba n dc. ba ndc a r e p r o j e c t e dy e t , b u tf a l l sb e t we e nt h en e a ra n df a rp l a n ea n ds h o u l da tl e a s tb e v i s i b l e , ai sn o t . t e s t e d( a n dt h e nf o u n df u l l yv i s i b l e ) . I t ' sa c t u a l l ye a s i e rt od ot h ev i e wv o l u mec h e c k si n3 Dc a me r as p a c ei n s t e a do f2 Dp r o j e c t i o ns p a c e .Th eo b j e c tr e c t a n g l ec a ne a s i l yb e T c a l c u l a t e df r o mx c=C · r, t h ea n c h o rp0a n dt h es i z es.Th ev i e wp o r twi l l h a v et ob es c a l e db yλ , a n dt h i sg i v e su st h ef o l l o wi n gr e s t st o p e r f o r m: Object position Depth d =zc=w· r Horizontal l =xc−p 0,x Vertical t =−yc−p 0,y Visible if N ≤d && d <F λ L ≤l +w && l <λ R −λ T ≤t +h && t <−λ B Table 21.1: Ob j e c tr e c ta n dc u l l i n gt e s t si nc a me r as pa c e .No t et h e s i g ns ! I fa l l t h e s ec o n d i t i o n sa r et r u e , t h e nt h eo b j e c ts h o u l db ev i s i b l e .No w, p l e a s en o t et h esigns o ft h et e s t s , p a r t i c u l a r l yi nt h ev e r t i c a l c h e c k s . 21.6.3. Animation Ro t a t i o na n i ma t i o n , t ob ep r e c i s e .Aswes a wi nf i g2 1 . 1 0 , t h es p r i t ewi l l s h o wt h es a mes i d er e g a r d l e s so fwh e r ey o ua r el o o k i n gf r o m. Th i si so n l yl o g i c a l , a st h es p r i t ei sn o ta c t u a l l ya3 De n t i t y .Toma k ei tlook al i t t l emo r e3 D, wen e e dt oh a v ei ma g e so ft h es p r i t et a k e n file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 200/331 28-03-13 Tonc : GBA Programming in rot13 f r o md i f f e r e n tc a me r aa n g l e s , a n dt h e np i c kt h eo n ewen e e dd e p e n d i n go nwh i c ha n g l ewe ' r el o o k i n gf r o m. Fi r s t , f i n d i n gt h ec o r r e c tv i e wa n g l e , ψ.Fi g2 1 . 1 3s h o wst h eg e n e r a l s i t u a t i o n .Th ea n g l ey o un e e di st h ea n g l eb e t we e nt h ev e c t o r b e t we e nt h ec a me r aa n dt h eo b j e c t( r e d , d a s h e d )a n dt h eg l o b a l l o o k i n gd i r e c t i o no ft h eo b j e c t .I n t h ef i g u r e , y o uc a ns e et h eg l o b a l d i r e c t i o na n g l e sf o rt h ec a me r aa n do b j e c t : φca n dφ, o r e s p e c t i v e l y .Al s oi n d i c a t e di st h ea n g l eb e t we e nt h ec a me r ad i r e c t i o na n dt h es p r i t e , α.I fy o ul o o k a tt h e s ea n g l e sc l o s e l y , y o u ' l l s e et h a tφc+α+ψ=φ. I n o t h e r w o r d s : o (21.17) ψ=φo−φc−α =φo−φc−a r c t a n ( xc/−zc) Wh e t h e rt h emi n u s s i g ni n s i d et h ea r c t a n ( )i sn e c e s s a r yd e p e n d so nh o wy o ud e f i n et h et e r msa l l t h et e r ms .Eq2 1 . 1 7i st h ef u l l yc o r r e c tv e r s i o n , b u ti ft h ea r c t a nd o e s n ' ta p p e a l t oy o u , y o u ' l l b e g l a dt ok n o wt h a ti nmo s tc a s e st h eαt e r mc a nb es a f e l yi g n o r e dwi t h o u ta n y o n en o t i c i n g . Fig 21.13: Fi n d i n gt h ev i e wa ng l eψ. No wt h a tweh a v eo u rv i e wi n ga n g l e , wen e e dt ou s ei ts o me h o w.Su p p o s ey o uh a v eN f r a me so f r o t a t i o n , wh i c hd i v i d e st h ec i r c l ei n t oe q u a l p a r t se a c h2 π/ Nr a d i a n swi d e .Tog e tt h es l i c et h a tψi si n , weme r e l yh a v et od i v i d eb yt h e a n g l eo fe a c hs l i c e : i =ψ/ ( 2 π/ N)=N· ψ/ ( 2 π) .I fy o uh a v ed e f i n e dy o u rc i r c l ei np o we r o f t wod i v i s i o n s( wh i c hweh a v e )t h e nt h i sp a r ti s r i d i c u l o u s l ye a s y : j u s tu s ear i g h t s h i f t .On c ey o uh a v et h ef r a me i n d e x , t h er e s ts h o u l db ee a s y .Mo s t l y .Th e r ea r es o mei n t r i c a c i e st h a t t h a tc a nf o gt h i n g su p , b u tt h o s ea r ei mp l e me n t a t i o n d e p e n d e n ta n dwi l l b es a v e df o rl a t e r . 21.6.4. Sprite sorting Di s r e g a r d i n gp r i o r i t yb i t sf o rt h emo me n t , t h eo r d e ro fo b j e c t so n s c r e e ni sd e t e r mi n e db yt h eo b j e c tn u mb e r : al o we rn u mb e rwi l l b ei n f r o n to fh i g h e rn u mb e r s .I n2 Dg a me s , y o uc a no f t e ni g n o r et h i sb e c a u s es p r i t e swi l l b eo nt h es a mel a y e r ;i n3 Dg a me s , y o ur e a l l y , r e a l l yc a n ' t .Ta k ef i g2 1 . 1 4 , f o re x a mp l e .Th ef o u rt h wo mp sh e r eh a v eas p e c i f i co b j e c to r d e r .I nt h el e f tp i c t u r e , t h ec l o s e s tt h wo mp h a p p e n st oh a v et h el o we s to b j e c ta n dt h ev i s u a l o r d e r i n gi sc o r r e c t .Wh e nv i e we df r o mt h eo t h e rs i d e , h o we v e r , ( mi d d l ep i c t u r e )t h i n g s a r eal i t t l ed i f f e r e n t .Th e r ea r et wov i s u a l c u e sf o rd e p t h : s c a l i n g( mo r ed i s t a n c ei ss ma l l e r )a n do c c l u s i o n( d i s t a n c eo b j e c t sa r eo b s c u r e d b yc l o s e ro b j e c t s ) .I nt h emi d d l ep i c t u r e , t h e s et woc o n f l i c tb e c a u s et h ec l o s e s to b j e c th a st h ehighest n u mb e r , ma k i n gt h eo v e r a l l p i c t u r e al i t t l ed i s c o n c e r t i n g .I nt h ep i c t u r eo nt h er i g h t , e v e r y t h i n gl o o k so k a ya g a i n , b e c a u s es t e p swe r et a k e nt oe n s u r et h ec o r r e c to b j e c t o r d e r . Fig 21.14.No n s o r t e do b j e c t sl o o ka l r i g h t( l e f t )f r o mo n ea n g l e , b u tn o tf r o mt h e o t h e rwa y( mi d d l e ) .Yo un e e dt os o r tt h e mt og e tt h ec o r r e c to r d e r( r i g h t ) . Wh a tn e e d st ob ed o n ei ss o r tt h eo b j e c t si nOAM a c c o r d i n gt od e p t h ;ak i n do fZb u f f e rf o ro b j e c t s .Th ed e p t ho fas p r i t ei ss i mp l yzc, a n dwen e e dt of i l l OAM wi t ht h es p r i t e ' so b j e c ta t t r i b u t e si no r d e ro fa s c e n d i n gzc.Fo rg o o dme a s u r e , i t ' sp r o b a b l yag o o di d e at og i v e h i d d e no b j e c t st h ema x i mu md e p t h v a l u ep o s s i b l eo rt ol e a v et h e mo u to ft h es o r t i n gp r o c e s se n t i r e l y . Th e r ea r ema n yp o s s i b l es t r a t e g i e sf o rs o r t i n gt h eo b j e c t s .Myo wnc h o i c ea r i g h tn o wwo u l db et on o ts o r tt h es p r i t e so ro b j e c t s d i r e c t l yb u tt oc r e a t ea nindex table, wh i c hi n d i c a t e st h eo r d e rt h es p r i t e s ' a t t r i b u t e ss h o u l dg oi n t oOAM.Th ep s e u d o c o d ef o rt h i si s g i v e nb e l o w.Wh i c ha l g o r i t h my o uu s et os o r tt h ek e y sd o e s n ' tr e a l l yma t t e ra tt h i st i me , a sl o n ga si td o e st h ej o b .I ' ms u r et h a tf a s t e r me t h o d sc a nb ef o u n d , b u tp r o b a b l ya tt h ee x p e n s eo fmo r ec o d ea n dIwa n tt ok e e pt h i n g sr e l a t i v e l ys i mp l e . // Pseudo code for sorting sprites for OAM void spr_sort() { int ids[N]; // Index table int keys[N]; // Sort keys // Create initial index and sort-key table for ii=0; ii<N; ii++) { ids[ii]= ii; keys[ii]= is_visible(sprite[ii]) ? sprite[ii].depth : DEPTH_MAX; } // Sort keys (i.e., fill ids) id_sort(ids, keys); file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 201/331 28-03-13 Tonc : GBA Programming in rot13 // Fill OAM according to for(ii=0; ii<N; ii++) oam_mem[ii]= sprite[ids[ii]].obj; } 21.6.5. Renormalization Iwo u l d n ' tb es u r p r i s e di fy o u ' v en e v e rh e a r do ft h i st e r mb e f o r e .Normalization me a n st h a ty o us c a l eaq u a n t i t yt oau s e r f r i e n d l yv a l u e –u s u a l l y1 .Yo uh a v ea l r e a d ys c a l e dt h es p r i t eb yaf a c t o rλ , b u tt h a t ' sn o te n o u g h .I nmo s tc a s e s , y o uh a v et os c a l ei tf u r t h e r , i . e renormalize i t .He r e ' swh y . Byd e f i n i t i o n , t h es c a l i n gf a c t o rλwi l l b eo n ewh e nzc=−D.No wc o n s i d e rwh a th a p p e n si fy o ul o o ka tac l o s e ro b j e c t , s a ya t zc=−½D.I nt h i sc a s e , λwi l l b e½a n dt h eo b j e c twi l l b es c a l e db yaf a c t o ro ft wo .I no t h e rwo r d s , i t ' l l a l r e a d yf i l l t h ed o u b l e s i z e c a n v a s .An dh i g h e rs c a l e sa r ep o s s i b l et o o : wi t ht h es u g g e s t e dv a l u e so fD =2 5 6a n dN =2 4 , y o uc o u l de n du pwi t hs c a l i n go f1 0 !Th i s wi l l n o td o . I ti sp o s s i b l et og e ta r o u n dt h i sb ymo v i n gt h en e a r p l a n ef u r t h e ra wa y .Ho we v e r , t h e ny o u ' l l s e eo b j e c td i s a p p e a r i n gi ft h e y ' r es t i l l q u i t ef a ro f f , wh i c hwi l l l o o kj u s ta ss t r a n g ea ss e e i n gt h e mc l i p p e d .Ab e t t e rs o l u t i o ni st og i v et h eo b j e c t sa ne x t r as c a l i n gf a c t o r .I n m7_exIh a v es c a l e dt h eo b j e c t sb ya na d d i t i o n a l f a c t o ro f¼, s ot h a ta3 2 x 3 2s p r i t ei sa c t u a l l yo n l y8 x 8‘ wo r l d ’ p i x e l si ns i z e .Th i s s e e mst owo r ko u tq u i t en i c e l y . Th i sr e n o r ma l i z a t i o nme a n st h a ty o u ' r ea c t u a l l ywo r k i n gwi t htwo separate s c a l i n gf a c t o r s : o n ef o rt h ec o o r d i n a t et r a n s f o r ma t i o n , a n do n ef o rv i s u a l e f f e c t s .I ti st h evisual s c a l i n gy o un e e dt ou s ei np o s i t i o n i n ga n dc u l l i n gt h es p r i t e s , n o tt h et r a n s f o r ma t i o ns c a l i n g ;t h e l a t t e r ' si n f l u e n c es t o p so n c ey o u ' v ef o u n dt h es c r e e n p o s i t i o no ft h ea n c h o r . Th e r e ' sp r o b a b l ya no f f i c i a l t e r mf o rt h i sp r o c e s s , b u tIwo u l d n ' tk n o wwh a ti ti s .I ' mf a mi l i a rwi t ht h ep r o c e s so fr e n o r ma l i z a t i o nf r o m p h y s i c s( af e wDu t c hp r o f e s s o r sg o tt h eNo b e l Pr i z ef o rt h i ss u b j e c taf e wy e a r sb a c k )a n di ts e e me dt of i t .I fy o uk n o wt h eo f f i c i a l t e r m, I ' dl i k et oh e a ri t . Fig 21.15.Ob j e c tr e n o r ma l i z a t i o n .Le f t : n o r ma l ( e w, n o ! ) .Mi d d l e : ×½( h mmm, n o ) . Ri g h t : ×¼( y e a h , t h a t ' si t ) . An dwi t ht h a t , we ' v er e a c h e dt h ee n do ft h et h e o r y .No wt oa c t u a l l yi mp l e me n ta l l o ft h i s . 21.7. Implementation 21.7.1. Design considerations. Mya i mh e r ei sn o tt ome r e l yd i s ho u tac o u p l eo ff u n c t i o n st h a tc a nma k emo d e7h a p p e n , b u ta l s op r o v i d es o me t h i n gt h a tc a nb ee a s i l y mo d i f i e di fn e c e s s a r y .Th ec o d eo ft h em7_exd e moi ss p r e a do v e r4f i l e s : o n ef o rt h et h i n g ss p e c i f i ct ot h ed e moi t s e l fm7_ex.c;a n d t h r e ef o rmo d e7s p e c i f i cs t u f f , mode7.h, mode7.ca n dmode7.iwram.c.Ye s , i wr a mf u n c t i o n st o o ;s o meo ft h e s et h i n g sa r e g o i n gt ob ec o mp u t a t i o ne x t e n s i v ea n dIwa n tt h e ma sf a s ta sp o s s i b l er i g h to f ft h eb a t .I ' ma l s ob o r r o wi n gt h eo b j e c ts o r t e rf r o mt h e p r i o r i t yd e mo . Th e r ea r et h r e ema i na r e a so fi n t e r e s th e r e : t h ecamera, background stuff a n dsprites.Fo re a c ho ft h e s ewewi l l u s eas t r u c t a n d / o ra r r a yt ok e e pt h e i rd a t as oi t ' sn i c ea n dOOPy .Th e r ewi l l a l s ob eas o r to fma n a g e rs t r u c tf o rt h emo d e7s t u f fa sawh o l e .An d , o fc o u r s e , wen e e dc o n s t a n t sf o rt h ev i e wv o l u me , f o c u sl e n g t ha n daf e wo t h e ri t e ms .Ah a n d f u l o ff u n c t i o n swi l l t h e no p e r a t eo nt h e s e i t e mst og i v eu pwh a twen e e d . Constants Th e r ea r e n ' tt o oma n yc o n s t a n t s .Mo s th a v et od owi t ht h ev i e wp o r t , t h eo t h e r swi t ht h ef o c u sa n dr e n o r ma l i z a t i o n . #define M7_D 256 #define M7_D_SHIFT 8 #define M7O_NORM 2 // View frustum limits #define M7_LEFT (-120) #define M7_RIGHT 120 #define M7_TOP 80 #define M7_BOTTOM (-80) #define M7_NEAR 24 file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm //!< Focal length //!< Focal shift //!< Object renormalization shift (by /4) //!< Viewport left //!< Viewport right //!< Viewport top (y-axis up) //!< Viewport bottom (y-axis up!) //!< Near plane (objects) 202/331 28-03-13 Tonc : GBA Programming in rot13 #define M7_FAR 512 //!< Far plane (objects) #define M7_FAR_BG 768 //!< Far plane (floor) Structs and variables Mo d e7wo u l db eawo n d e r f u l p l a c et ou s ec l a s s e s , b u ts i n c eI ' mwo r k i n gi nC, n o tC++, I ' ms t i c k i n gt os t r u c t s .Ap a r tf r o mt h e BG_AFFINEs t r u c tIp r e s e n t e di nt h ea f f i n eb a c k g r o u n dp a g e , y o un e e do n es t r u c tf o rt h ec a me r aa n do n es t r u c tf o rt h emo d e7 o b j e c t s .I ' ma l s ou s i n gamo d e7c o n t a i n e rs t r u c tt ok e e pt r a c ko fa l l t h ep a r t st h a tg oi n t ot h emo d e7f u n c t i o n a l i t y , s ot h a ty o uwo n ' t h a v el o o s eg l o b a l v a r i a b l e sl y i n ga r o u n dt h ep l a c e . Yo u ' r ef r e et oc r e a t ey o u ro wns t r u c t sf o rt h e s e , b u tt h eo n e sIwi l l u s ea r eg i v e nb e l o w.I fy o u ' v eb e e np a y i n ga t t e n t i o n , mo s to ft h e me mb e r ss h o u l db ef a mi l i a r .Oh , t h ePOINTa n dVECTORs t r u c t sa r e2 Da n d3 Dv e c t o r s , o fc o u r s e . //! 3D sprite struct typedef struct M7_SPRITE { VECTOR pos; //!< World position. POINT anchor; //!< Sprite anchor. OBJ_ATTR obj; //!< Object attributes. s16 phi; //!< Azimuth angle. u8 obj_id; //!< Object index. u8 aff_id; //!< OBJ_AFFINE index. TILE *tiles; //!< Gfx pointer. VECTOR pos2; //!< Position in cam space (subject to change) } M7_SPRITE; //! 3D camera struct typedef struct M7_CAM { VECTOR pos; //!< World position. int theta; //!< Polar angle. int phi; //!< Azimuth angle. VECTOR u; //!< local x-axis (right) VECTOR v; //!< local y-axis (up) VECTOR w; //!< local z-axis (back) } M7_CAM; //! One struct to bind them all typedef struct M7_LEVEL { M7_CAM *camera; //!< Camera variables BG_AFFINE *bgaff; //!< Affine parameter array M7_SPRITE *sprites; //!< 3D sprites int horizon; //!< Horizon scanline (sorta) u16 bgcnt_sky; //!< BGxCNT for backdrop u16 bgcnt_floor; //!< BGxCNT for floor } M7_LEVEL; Th e r e ' sn o tmu c hmo r eIh a v et os a ya b o u tt h e s es t r u c t s .Th eM7_SPRITEh a st h ea t t r i b u t e so fi t so b j e c ta same mb e ri t s e l f , r a t h e r t h a na ni n d e xo rp o i n t e rt oa n ys o r to fb u f f e r .Th er e a s o nb e h i n dt h i si se s s e n t i a l l y“ wh yt h eh e l l n o t ” .Be c a u s eIh a v et os o r tt h eo b j e c t s a n y wa y , u s i n ga ne x t r ab u f f e rmi g h tn o tb ewo r t h wh i l e , s oIc h o s et h i s .I ' ma l s ok e e p i n gt r a c ko ft h ep o s i t i o ni nc a me r as p a c eb e c a u s eI n e e di to nmo r et h a no n eo c c a s i o n , a n daTI LEp o i n t e rf o rg r a p h i c s .Th er e a s o nf o rt h i swi l l b e c o mea p p a r e n twh e nwei mp l e me n t a n i ma t i o n . Th eM7_LEVELh o l d sp o i n t e r st ot h ema i nv a r i a b l e sf o rmo d e7( t h ec a me r a , a f f i n ea r r a ya n ds p r i t e s )a swe l l a st h eh o r i z o n s c a n l i n en e e d e dt os wi t c hf r o mb a c k d r o pt of l o o r , a n dt wov a r i a b l e sc o n t a i n i n gt h ed a t ao ft h eb gc o n t r o l r e g i s t e r , a st h i swi l l b ed i f f e r e n t f o rt h eb a c k d r o pa n df l o o r . No wwen e e dt h e s ef o u rv a r i a b l e su s i n gt h e s es t r u c t s .Be c a u s et h e s ea r et e c h n i c a l l yp a r to ft h ed e moi t s e l f , I ' v ep u tt h e mi nm7_ex.c i n s t e a do ft h ema i nmo d e7c o d e , a l t h o u g ht h a tc o d ed o e sr e q u i r ea na c t u a l m7_levelv a r i a b l et oe x i s tf o rt h eHBl a n ki n t e r r u p t . SPR_COUNTi st h en u mb e ro fs p r i t e s , wh i c hi sdefinitely d e mos p e c i f i c .Th e r ea r e1 6 1e n t r i e si nm7_bgaffsr a t h e rt h a nj u s t1 6 0f o r t h es a mer e a s o na si nt h eDMAd e mo : HBl a n ks e t su pt h en e x tl i n e , r a t h e rt h a nt h ec u r r e n to n e , a n dh a v i n gt h i si sb e t t e r( a n df a s t e r ) t h a nt h ea l t e r n a t i v ewi t hi f / e l s eb l o c k s . M7_CAM m7_cam; BG_AFFINE m7_bgaffs[SCREEN_HEIGHT+1]; M7_SPRITE m7_sprites[SPR_COUNT]; M7_LEVEL m7_level; file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 203/331 28-03-13 Tonc : GBA Programming in rot13 Type and order of struct members Myu s u a l a d v i c ei st ou s ei n t sf o ry o u rd a t at y p e s , b u tf o rs t r u c t st h i sma yn o ta l wa y sb et h eb e s tt h i n gt od o .Lo c a l v a r i a b l e s ma yn o tu s eu pme mo r y , b u ts t r u c t sd o .An dwh e ny o uh a v ea r r a y so fs t r u c t s , t h ee x t r as p a c et h a two r d s i z e dme mb e r sc o s t a d d su pq u i c k l y .Soi nt h a tc a s ef e e l f r e et ou s en o n i n t s . Ha v i n gs a i dt h a t , wh e ni t ' st i met ou s et h o s eme mb e r si tc a np a yt oc o p yi t sd a t at oal o c a l 3 2 b i tv a r i a b l e , r a t h e ra nu s i n ga b y t eo rh a l f wo r dme mb e rf o ra l l t h ec a l c u l a t i o n s . Al s o , a n dt h i si svery i mp o r t a n t , y o uwo n ' tb es a v i n ga n ys p a c ei fy o ud o n ' tp a ya t t e n t i o nt ot h eo r d e ro ft h eme mb e r s .An i n twi l l s t i l l r e q u i r ewo r d a l i g n me n t , e v e nwh e ni tc o me sr i g h ta f t e rab y t eme mb e r .Th ec o mp i l e rma ya d dp a d d i n ga f t e rb y t e s a n dh a l f wo r d st oe n s u r et h en e x tme mb e ri sc o r r e c t l ya l i g n e d .I t ' db eb e s ti fy o uo r d e r e dt h eme mb e r si ns u c hawa yt h a t t h e r e ' sa sl i t t l ep a d d i n ga sp o s s i b l e . 21.7.3. Background functions Th e s ea r emyf o u rma i nb a c k g r o u n df u n c t i o n s : void m7_prep_horizon(M7_LEVEL *level).Ca l c u l a t e st h eh o r i z o ns c a n l i n e . IWRAM_CODE void m7_prep_affines(M7_LEVEL *level).Ca l c u l a t e st h ea f f i n ep a r a me t e r sf o rt h ef l o o r , b a s e do nc a me r ap o s i t i o na n do r i e n t a t i o n . . void m7_update_sky(const M7_LEVEL *level).Po s i t i o n st h eb a c k d r o p . IWRAM_CODE void m7_hbl_floor().HBl a n ki n t e r r u p tr o u t i n e .Swi t c h e st omo d e2wh e nn e c e s s a r ya n dc o p i e s a f f i n ep a r a me t e r sa n dc r e a t e sf o ge f f e c t . m7_prep_horizon()a n dm7_update_sky()a r es i mp l ei mp l e me n t a t i o n so fe q2 1 . 9a n de q2 1 . 1 7 , r e s p e c t i v e l y , s oIc a nb e b r i e fwi t ht h e s e . //! Calculate the horizon scanline void m7_prep_horizon(M7_LEVEL *level) { int horz; M7_CAM *cam= level->camera; if(cam->v.y != 0) { horz= M7_FAR_BG*cam->w.y - cam->pos.y; horz= M7_TOP - Div(horz*M7_D, M7_FAR_BG*cam->v.y); } else // looking straight down (w.y > 0) means horizon at -inf scanline horz= cam->w.y > 0 ? INT_MIN : INT_MAX; level->horizon= horz; } //! Update sky-bg position void m7_update_sky(const M7_LEVEL *level) { REG_BG2HOFS= (level->camera->phi>>6)+M7_LEFT; REG_BG2VOFS= -clamp(level->horizon, 0, 228)-1; } Th eh o r i z o nc a l c u l a t i o nma k e su s eo fac l i p p i n gf a r p l a n e , t h o u g ht h i si sn o ts t r i c t l yn e c e s s a r y .I fy o uwa n tt h eh o r i z o na ti n f i n i t y , r e mo v e t h es u b t r a c t i o nb yt h ec a me r a ' sh e i g h ta n du s eM7_FAR_BG=1 .No t et h ec h e c kf o rvy=0 .Asvy=c o s ( θ ) , t h i swi l l b et r u ewh e n l o o k i n gs t r a i g h tu po rs t r a i g h td o wn .Th ed i s t i n c t i o ni si mp o r t a n tb e c a u s es e e st h es k y( n oa f f i n eb g )a n do n es e e so n l yf l o o r( n o b a c k d r o p ) .Te c h n i c a l l yt h e s es h o u l db e±i n f i n i t y , b u ta st h i si sf i x e d p o i n t , INT_MIN/MAXwi l l h a v et od o . Asf o rt h eb a c k d r o pp l a c e me n t : I ' mt a k i n galot o fs h o r t c u t sh e r e .Ama t h e ma t i c a l l yc o r r e c tb a c k d r o pwo u l du s eab a c k g r o u n dma p 1 7 2 0p i x e l swi d e .I tc a nb ed o n e , b u tmo s t l yi t ' sj u s ta n n o y i n g .I n s t e a d , I ' mu s i n ga5 1 2 x 2 5 6 pr e g u l a rb a c k g r o u n da n du s eP =1 0 2 4i nt h e a n g l e →s c r o l l o f f s e tc o n v e r s i o n .Th i sme a n st h ema ps h o wsu pt wi c ei no n e3 6 0 °r o t a t i o na n dt h a tt h edx i sj u s tφ/ 6 4 .Ye s , t h ef l o o ra n d b a c k d r o pf i e l d o f v i e wwi l l b es l i g h t l yo u to fs y n c , b u ty o u ' l l o n l yn o t i c ei fy o uk n o wwh a tt ol o o kf o r , s ot h a t ' sa l r i g h t . St r i c t l ys p e a k i n g , t h ev e r t i c a l o f f s e ts h o u l db eb g He i g h t−h o r i z o n , b u tt h eb g h e i g h tc a nb ei g n o r e dd u et owr a p p i n g .Th er e a s o nI ' m a l s oc l a mp i n gt h eh o r i z o nt ot h es i z eo ft h ev i e wp o r ti sb e c a u s et h eh o r i z o ns c a n l i n ec a nb e c o mev e r yl a r g e–t h et a n ( θ )i ni twi l l a p p r o a c hi n f i n i t ywh e nl o o k i n gu p , r e me mb e r ?I fy o ud o n ' tc l a mpi ty o u ' l l s c r o l l t h r o u g ht h ewh o l eb a c k d r o pma pac o u p l eo ft i me swh e n p a n n i n gu p , wh i c hj u s tl o o k sa wf u l . Preparing the affine parameter table Ca l c u l a t i n gt h ea f f i n ep a r a me t e r sh a p p e n si nm7_prep_affines().Yo uc o u l dt r yt od ot h i si nt h eHBl a n ki s r , b u tb e c a u s ei t r e q u i r e sad i v i s i o n , i two u l ds i mp l yt a k et o ol o n g .Al s o , d o i n gi ti no n es p o ti smo r ee f f i c i e n t , a sy o uo n l yh a v et os e t u pt h ev a r i a b l e s o n c e .Th i sr o u t i n ec a r r i e so u tt h ec a l c u l a t i o n so fe q2 1 . 1 4 .I th a st od oq u i t ean u mb e ro fc a l c u l a t i o n sf o re a c hs c a n l i n e , i n c l u d i n ga d i v i s i o n , s oy o uc a ne x p e c ti tt ob er a t h e rc o s t l y ;wh i c hi swh yI ' mp u t t i n gi ti nI WRAM r i g h tf r o mt h es t a r t . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 204/331 28-03-13 Tonc : GBA Programming in rot13 No w, y o ud o n ' th a v et oc a l c u l a t et h i n g sf o re v e r ys c a n l i n e : j u s tt h eo n e sb e l o wt h eh o r i z o n .Asf o ri mp l e me n t i n ge q2 1 . 1 4i t s e l f : i t t u r n so u tt h a ti two r k smu c hb e t t e ri fy o ut a k et h ec a me r ama t r i xa p a r ta g a i na n dwo r kwi t hs i n e sa n dc o s i n e so fθa n dφ, r a t h e rt h a nt h e n i n ema t r i xe n t r i e s .Th i sn e x tp a r a g r a p hwi l l e x p l a i nh o w, b u tf e e l f r e et os k i pi ta n dg oo n t ot h ec o d e . Re me mb e rt h a tt h ec a me r ama t r i xi sC =Ry( φ) · Rx( θ ) ;a n dt h a tλa n ddx a r ec a l c u l a t e dv i ae q2 1 . 1 3 : dx′ =acw +λ · C· b.Yo uc a n b r e a ku pC c a nc o mb i n ei twi t hb t of o r mb′ =Rx( θ ) · b.Th i sn e wv e c t o rt a k e sc a r eo ft h ep i t c he n t i r e l y–i t ' sa si fweo n l yh a dar o t a t i o n a r o u n dt h ev e r t i c a l a x i s , i . e . , t h ec a s ed i s c u s s e di nt h ep r e v i o u sc h a p t e r .Wi t ht h i sp r e r o t a t i o n , t h ec o d eb e c o me ss i mp l e ra n df a s t e r . IWRAM_CODE void m7_prep_affines(M7_LEVEL *level) { if(level->horizon >= SCREEN_HEIGHT) return; int ii, ii0= (level->horizon>=0 ? level->horizon : 0); M7_CAM *cam= level->camera; FIXED xc= cam->pos.x, yc= cam->pos.y, zc=cam->pos.z; BG_AFFINE *bga= &level->bgaff[ii0]; FIXED yb, zb; // b' = Rx(theta) * (L, ys, -D) FIXED cf, sf, ct, st; // sines and cosines FIXED lam, lcf, lsf; // scale and scaled (co)sine(phi) cf= cam->u.x; sf= cam->u.z; ct= cam->v.y; st= cam->w.y; for(ii= ii0; ii<SCREEN_HEIGHT; ii++) { yb= (ii-M7_TOP)*ct + M7_D*st; lam= DivSafe( yc<<12, yb); // .12f lcf= lam*cf>>8; lsf= lam*sf>>8; // .12f // .12f bga->pa= lcf>>4; bga->pc= lsf>>4; // .8f // .8f // lambda·Rx·b zb= (ii-M7_TOP)*st - M7_D*ct; // .8f bga->dx= xc + (lcf>>4)*M7_LEFT - (lsf*zb>>12); // .8f bga->dy= zc + (lsf>>4)*M7_LEFT + (lcf*zb>>12); // .8f // hack that I need for fog. pb and pd are unused anyway bga->pb= lam; bga++; } level->bgaff[SCREEN_HEIGHT]= level->bgaff[0]; } Web e g i nb yg e t t i n gt h es c a n l i n et ob e g i nc a l c u l a t i n ga t( wh i c hma yb en o t h i n g ) , a n dd e f i n i n glots o ft e mp o r a r i e s .No ta l l o ft h e t e mp o r a r i e sa r en e c e s s a r y , b u tt h e yma k et h ec o d emo r er e a d a b l e .Na me sa s i d e , t h ec o d ewi t h i nt h el o o pi sv e r ys i mi l a rt ot h a to f hbl_mode7_ci nt h ef i r s tmo d e7d e mo , e x c e p tt h a ti nc a l c u l a t i n gλweu s ear o t a t e dysv a l u e , a n di nc a l c u l a t i n gt h eo f f s e t sar o t a t e d zs( =−D)v a l u e .An n n d , t h a t ' si t . Th ec o mme n t sb e h i n dt h ec a l c u l a t i o n si n d i c a t et h ef i x e d p o i n tc o u n to ft h er e s u l t s , wh i c hi nt h i sc a s ec a nb ee i t h e r. 8 fo r. 1 2 f .No w h e a rt h i s : i ti svery i mp o r t a n tt h a tt h es c a l e d( c o ) s i n eo fφ, lcfa n dlsf, u s e1 2b i t so fp r e c i s i o no rmo r e .I ' v et r i e d8 , i t ' sn o tp r e t t y– t h ed i s p l a c e me n t sa r ea l l o f fa tc l o s er a n g e .Se c o n d l y , n o t et h eo r d e ro fmu l t i p l i c a t i o n sa n ds h i f t si nt h ed i s p l a c e me n t s ;i ti sa l s ov e r y i mp o r t a n tt h a tt h e s es t a yt h ewa yt h e ya r e .Pa r t i c u l a r l yt h eo n ewi t hL: t h emu l t i p l i c a t i o nb yM7_LEFTmust h a p p e na f t e rt h es h i f t , t r u s t meo nt h i s . Th el a s ti n t e r e s t i n gp o i n ti st h el i n ea f t e rt h el o o p , wh i c hc o p i e st h ep a r a me t e r sf o rs c a n l i n e0t ot h eb a c ko ft h ea r r a yt oc o mp e n s a t e f o rt h eHBl a n k i n t e r r u p to b i wa ne r r o r . Th i sf u n c t i o ni sp r o b a b l ya sf a s ta sy o uc a nma k ei ti nC, a n di tt h ec o mp i l e rd o e si t sj o bp r e t t ywe l l s ot h e r ei sl i t t l et ob eg a i n e db yg o i n g t oma n u a l a s s e mb l y .Th i sd o e sn o tme a ni td o e s n ' ts t i l l t a k eq u i t es o met i me .Th ed i v i s i o na l o n ec o s t ss o me t h i n gl i k e1 0 0t o4 0 0c y c l e s ( t h ec y c l e c o u n tf o rBI OSd i v i s i o ni sr o u g h l y9 0+1 3 / s i g n i f i c a n tb i t ) .Ato n ed i v i s i o np e rs c a n l i n e , t h i sc a nr e a l l ya d du p .Th eb e s t s t r a t e g yt od e a l wi t ht h i si st onot do it i fy o ud o n ' th a v et o .I fy o uu s eaf i x e dp i t c ha n g l e , y o uc a np r e c a l c u l a t ea l l t h ed i v i s i o n sa n dj u s t l o o kt h e mu p .I fy o umu s th a v eav a r i a b l ep i t c h , y o uc a na l s og ot h et r i gwa y .Lo o kb a c ka tf i g2 1 . 4 .I fβi st h ea n g l eb e t we e n( 0 , yp, −D) a n d( 0 , 0 , −D) , t h e nt a n ( β)=yp/ D.Wi t hag o o dd e a l o ft r i g o n o me t r y , y o uc o u l dr e wr i t et h ef o r mu l af o rλt o (21.18) λ=a cw, D ·c os ( β) / s i n ( θ +β) y/ Yo uc a ng e tβv i aa na r c t a nLUTo f1 6 0e n t r i e s , o n ef o re a c hs c a n l i n e( h e y , y o uc o u l de v e np u tt h a ti n t op d! ) , a n dt h e nu s ea1 / s i n e file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 205/331 28-03-13 Tonc : GBA Programming in rot13 LUT.Yo uh a v et ob ec a r e f u l t ou s el a r g ee n o u g hLUTs , t h o u g h .Si n c et h ea r g u me n t so fLUTsa r ei n t e g e r s , βwi l l b et r u n c a t e d , a n dy o u wi l l l o s ealot o fa c c u r a c yt h o u g ht h i s , e s p e c i a l l yn e a rt h eh o r i z o n .No w, Ih a v e n ' ta c t u a l l yt r i e dt h et r i g wa yy e t , b u tIh a v ed o n es o me b a s i ct e s t si nEx c e l wh i c hwo u l ds u g g e s tt h a twi t ha1 / s i n eLUTo f5 1 2 / c i r c l e , y o u ' dg e tλ e r r o r swe l l o v e r1 0 %n e a rt h eh o r i z o n , a n d e r r o r sa r o u n d1 %e v e r y wh e r ee l s e .Wi t ht h a ti nmi n d , I ' ds u g g e s t1 0 2 4 / c i r c l ea tl e a s t .Ori n t e r p o l a t i n gb e t we e nLUTe n t r i e s , wh i c hy o u c a nd owi t ht o n c l i b ' slu_lerp16()a n dlu_lerp32()f u n c t i o n s . As i d ef r o mg o i n gt r i g g ywi t hi t , y o uc a np r o b a b l ys p e e du pt h ed i v i s i o na swe l l i nan u mb e ro fwa y s .Bu tb e f o r ey o ug oa n do p t i mi z e t h i s , a s ky o u r s e l fi fy o ur e a l l yn e e di tf i r s t .Pr e ma t u r eo p t i mi z a t i o ni st h er o o to fa l l e v i l , a f t e ra l l . Speed-ups for affine calculations Tr i e dt h r e eo p t i mi z a t i o n sr e c e n t l y .Fi r s t , ARM/ I WRAM, wh i c hb r i n g st h et h i n gd o wnt o2 3 k 5 8 kc y c l e s .Th e n , al i t t l e r e f a c t o r i n gt h a tp r e s e n t e di t s e l fi nad i s c u s s i o nwi t hs g e o s : t h ec a me r av e c t o r sc a nr e s o l v et oas ma l l e rs e to fv a r i a b l e s , s a v i n g1 0 2 0 %.Th e n , t h et r i gt h i n g , wh i c hc a nb r i n gt h ewh o l et h i n gd o wnt o1 0 2 0 ko re v e n7 k 1 4 kma x , d e p e n d i n go n wh e t h e ry o ug e tc o s ( β)a n d1 / s i n ( θ +β)v i al a r g el u t s , o rs ma l l e rl u t sa n dl i n e a ri n t e r p o l a t i o n .On c ey o ug e tt h ema t h , s h i f t s , a n ds i g n si no r d e r , i two r k sl i k eac h a r m. The mode 7 HBlank interrupt routine Tok e e pt h i n g ss i mp l e , n e a r l ye v e r y t h i n gt h a th a st oh a p p e nd u r i n gVDr a wh a p p e n si n s i d eo n eHBl a n ki s rc a l l e dm7_hbl_floor(). Ea r l i e rv e r s i o n so ft h i sd e mou s e das y s t e mo fVCo u n t / HBl a n ki n t e r r u p t s , b u tt h a tt u r n e do u tt ob emo r et r o u b l et h a ni t ' swo r t h .Th i si s a l s oa nI WRAM r o u t i n eb e c a u s ei tr e a l l yn e e d st ob ea sf a s ta sp o s s i b l e .Th ei n t e r r u p ts e r v i c er o u t i n ed o e st h ef o l l o wi n gt h i n g s : 1 .Check vcount for floor-range.I ft h i ss c a n l i n ei sn o tp a r to ft h ef l o o r , r e t u r n . 2 .Check vcount for horizon.Atr e a c h i n gt h eh o r i z o ns c a n l i n et h ev i d e omo d es h o u l dc h a n g ea n dREG_BG2CNTs h o u l db es e t t ot h ef l o o r ' ss e t t i n g s . 3 .Copy affine parameters to REG_BG_AFFINE[2].Co p yt h enext s c a n l i n e ' sp a r a me t e r st oREG_BG_AFFINE[2], a s we ' v ea l r e a d yp a s tt h ec u r r e n ts c a n l i n e . 4 .Fogging.Fa d et oo r a n g ei nt h i sc a s e . // from tonc_core.h //! Range check; true if xmin<=x<xmax #define IN_RANGE(x, min, max) ( (x) >= (min) && (x) < (max) ) IWRAM_CODE void m7_hbl_floor() { int vc= REG_VCOUNT; int horz= m7_level.horizon; // (1) Not in floor range: quit if(!IN_RANGE(vc, horz, SCREEN_HEIGHT) ) return; // (2) Horizon: switch to mode 1; set-up bg control for floor if(vc == horz) { BF_SET(REG_DISPCNT, DCNT_MODE1, DCNT_MODE); REG_BG2CNT= m7_level.bgcnt_floor; } // (3) Looking at floor: copy affine params BG_AFFINE *bga= &m7_level.bgaff[vc+1]; REG_BG_AFFINE[2] = *bga; // (4) A distance fogging with high marks for hack-value u32 ey= bga->pb*6>>12; if(ey>16) ey= 16; REG_BLDALPHA= BLDA_BUILD(16-ey, ey); } Po i n t s( 3 )a n d( 4 )c o u l db e n e f i tf r o mab i tmo r ee x p l a n a t i o n .Asme n t i o n e ds e v e r a l t i me sn o w, t h ei s ro fa n ys c a n l i n evc s h o u l ds e t u p t h ep a r a me t e r so fnext s c a n l i n e , wh i c hi swh ywe ' r ec o p y i n gf r o mlevel.bgaff[vc+1]r a t h e rt h a nj u s t[vc].Sc a n l i n ez e r o ' s u s e st h es e tf r o mvc =1 6 0 , wh i c hi sa l r i g h tb e c a u s ewe ' v ec o p i e dz e r o ' sd a t at ot h el a s te l e me n ti nt h ea r r a y .Asu s u a l , s t r u c tc o p i e s f t w. Fo rt h ef o g g i n gIu s ep bwh i c hf i l l e dwi t hλi nm7_prep_affines()f o rt h i sv e r yr e a s o n .As c a l e dλi sn o tt h emo s ta c c u r a t e mo d e l f o rf o g g i n g , b u tt h ee f f e c tl o o k swe l l e n o u g h .Be c a u s et h eb l e n d i n gr e g i s t e r sc a pa t1 6 , In e e dt oma k es u r ei td o e s n ' twr a pa r o u n d a th i g h e rv a l u e s . Th i sstill l e a v e st h eq u e s t i o no fwh a tI ' ma c t u a l l yb l e n d i n gwi t h , a so r a n g ei s n ' tp a r to ft h eGBA' sf a d er e p e r t o i r e .Atl e a s t , n o t file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 206/331 28-03-13 Tonc : GBA Programming in rot13 directly.I ti s , h o we v e r , q u i t ep o s s i b l et ob l e n dwi t ht h eb a c k d r o p , wh i c hj u s ts h o wsb g c o l o r0 .Th i sc o l o rc a nb ea n y t h i n g , i n c l u d i n g o r a n g e . 21.7.4. Sprites and objects Sp r i t ea n do b j e c th a n d l i n gh a sb e e nd i s t r i b u t e do v e rt h ef o l l o wi n gt h r e ef u n c t i o n s : void update_sprites().Th i si st h ema i ns p r i t eh a n d l e r , wh i c hc a l l so t h e rf u n c t i o n st od op o s i t i o n i n g , s o r t i n ga n d a n i ma t i o n . IWRAM_CODE void m7_prep_sprite(M7_LEVEL *level, M7_SPRITE *spr).Th i sc a l c u l a t e st h ec o r r e c t p o s i t i o na n ds c a l eo ft h es p r i t e . void kart_animate(M7_SPRITE *spr, const M7_CAM *cam).Th i ss e l e c t st h ec o r r e c tf r a mef o rr o t a t i n g a r o u n dt h ek a r t s . On l ym7_prep_sprite()i sa c t u a l l yp a r to ft h emo d e7f u n c t i o n s ;t h eo t h e r sc o u l dv e r ywe l l d i f f e rf o re v e r ymo d e7g a mey o uh a v e i nmi n d .Th ema i ns p r i t eh a n d l e r , update_sprites(), i sp r e t t ys i mp l e : i tn e e d st oc a l l m7_prep_sprite()f o re a c hs p r i t ea n d c r e a t et h es p r i t e ' ss o r t i n gk e y , s o r ta l l t h es p r i t e sa n dc o p yt h es o r t e da t t r i b u t e st oOAM.I ta l s oc a l l skart_animate()f o re a c h k a r t s p r i t ef o rt h e i ra n i ma t i o n s ;i fIh a da n i ma t i o n sf o rt h et h wo mp so ro t h e rs p r i t e st h e y ' dp r o b a b l yg oh e r ea swe l l . void update_sprites() { int ii; M7_SPRITE *spr= m7_level.sprites; for(ii=0; ii<SPR_COUNT; ii++) { m7_prep_sprite(&m7_level, &spr[ii]); // Create sort key if(BF_GET2(spr[ii].obj.attr0, ATTR0_MODE) != ATTR0_HIDE) sort_keys[ii]= spr[ii].pos2.z; else sort_keys[ii]= INT_MAX; } // Sort the sprites id_sort_shell(sort_keys, sort_ids, SPR_COUNT); // Animate karts for(ii=0; ii<8; ii++) kart_animate(&spr[ii], m7_level.camera); // Update real OAM for(ii=0; ii<SPR_COUNT; ii++) obj_copy(&oam_mem[ii], &spr[sort_ids[ii]].obj, 1); } Mo s to ft h ec o d eh a st od owi t hs o r t i n gt h es p r i t e s , wh i c hwa sa l r e a d yd e s c r i b e di nt h et h e o r y .Th epos2me mb e ro ft h es p r i t e si ss e t b ym7_prep_sprite()t oc o n t a i nt h es p r i t e ' sp o s i t i o ni nc a me r as p a c e .Th es o r t i n gr o u t i n eid_sort_shell()i st h ei n d e x t a b l e s o r t e rd e s c r i b e di nt h ep r i o r i t ys e c t i o n . I fIh a dwa n t e dt oh a v emo r ea d v a n c e da n i ma t i o no rs p r i t et h i n g s , t h e y ' db ep u th e r ea swe l l .Bu tId i d n ' t , s oIh a v e n ' t . Sprite positioning and scaling Th ef u n c t i o nm7_prep_sprite()c a l c u l a t e st h ec o r r e c to n s c r e e np o s i t i o nf o ras p r i t e , s e t su pt h ea f f i n ema t r i xwi t ht h ep r o p e r ( r e n o r ma l i z e d )s c a l e sa n dh i d e st h es p r i t ei fi tf a l l so u t s i d et h ev i e wv o l u me . Th ef i r s ts t e pi sc o n v e r tt ot h ec o n v e r tt h ewo r l d p o s i t i o no ft h es p r i t et oav e c t o ri nt h ec a me r as p a c e , u s i n gt h ef i r s tp a r to f e q2 1 . 1 5 : x c=CT· r, wi t hr b e i n gt h ep o s i t i o no ft h es p r i t er e l a t i v et ot h ec a me r a : r =x w−acw.Th i si sp u ti n t ov a r i a b l evc, b u twi t ht h e s i g n so fy a n dz s wi t c h e d !Th i sma k e ss u b s e q u e n tc a l c u l a t i o n sal i t t l ee a s i e r .Th i sv e c t o ri sa l s os t o r e di nspr->pos2, wh i c hi su s e di n s o r t i n ge l s e wh e r e . Th es e c o n ds t e pi sc h e c k i n gwh e t h e rt h es p r i t ewo u l da c t u a l l yb ev i s i b l e , u s i n gt h ec o n d i t i o n sf r o mt a b l e2 1 . 1 , wi t ho n ee x c e p t i o n : t h e c h e c k sn o wu s et h erenormalized r e c t a n g l eo ft h es p r i t e .Le a v i n gt h a tp a r to u tc o u l dc r e a t ea r t i f a c t sf o rs o meo r i e n t a t i o n s .Toc a l c u l a t e t h es p r i t er e c t a n g l eI ' mu s i n gt h es i z e so ft h eo b j e c tr e c t a n g l e .I ti sp o s s i b l et og e tat i g h t e rf i ti fy o u ' da l s od e f i n eas p r i t er e c t a n g l e i n d i c a t i n gt h ev i s i b l ep i x e l swi t h i nt h eo b j e c tf r a me , b u tt h a tmi g h tb eg o i n gal i t t l et o of a rh e r e . No t et h a tmo s to ft h ec o d ef r o mt h eb o u n d sc h e c k so ni sd o n ei nado-while(0)l o o p .Th i sp a t t e r ni ss o r to fap o o r ma n ' s try/catchb l o c k–Icould h a v eu s e dgotosh e r e , b u ta st h e y ' r ec o n s i d e r e dh a r mf u l Id e c i d e da g a i n s ti t .An y wa y , a no u t o f b o u n d s ‘ e x c e p t i o n ’h e r ewo u l di n d i c a t et h a tt h es p r i t es h o u l db eh i d d e n , wh i c hi sd o n ei ns t e p( 5 ) . I fwe ' v ep a s s e dt h eb o u n d s c h e c k s , wen e e dt os e t u pt h ea f f i n ema t r i xa n dc a l c u l a t et h eo b j e c t ' sp o s i t i o nv i at h ea n c h o r i n ge q u a t i o n o fe q2 1 . 1 6 . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 207/331 28-03-13 Tonc : GBA Programming in rot13 //! Setup an object's attr/affine with the right attributes /*! \param level Mode 7 level data * \param spr 3D sprite to calculate for */ IWRAM_CODE void m7_prep_sprite(M7_LEVEL *level, M7_SPRITE *spr) { M7_CAM *cam= level->camera; VECTOR vr, vc; // Difference and inverted-cam vector int sx, sy; // Object size RECT rect; // Object rectangle // (1) Convert to camera frame vec_sub(&vr, &spr->pos, &cam->pos); vc.x= vec_dot(&vr, &cam->u); vc.y= -vec_dot(&vr, &cam->v); vc.z= -vec_dot(&vr, &cam->w); spr->pos2= vc; OBJ_ATTR *obj= &spr->obj; sx= obj_get_width(obj); sy= obj_get_height(obj); // --- Check with viewbox --do { // (2a) check distance if(M7_NEAR*256 > vc.z || vc.z > M7_FAR*256) break; // (2b) check horizontal rect.l= vc.x - spr->anchor.x*(256>>M7O_NORM); rect.r= rect.l + sx*(256>>M7O_NORM); if(M7_LEFT*vc.z > rect.r*M7_D || rect.l*M7_D > M7_RIGHT*vc.z) break; // (2c) check vertical rect.t= vc.y - spr->anchor.y*(256>>M7O_NORM); rect.b= rect.t + sy*(256>>M7O_NORM); if(-M7_TOP*vc.z > rect.b*M7_D || rect.t*M7_D > -M7_BOTTOM*vc.z) break; // (3) Set-up affine matrix OBJ_AFFINE *oa= &obj_aff_mem[spr->aff_id]; oa->pa= oa->pd= vc.z>>(M7_D_SHIFT-M7O_NORM); oa->pb= oa->pb= 0; FIXED scale= DivSafe(M7_D<<16, vc.z); // normalized lambda // (.16 / .8) = .8 // (4) anchoring // Base anchoring equation: // x = q0 - s - A(p0 - s/2) // In this case A = 1/lam; and q0 = xc/lam // -> x = (xc - p0 + s/2)/lam - s + screen/2 int xscr, yscr; xscr = spr->anchor.x*256 - sx*128; xscr = (vc.x - (xscr>>M7O_NORM))*scale>>16; xscr += -sx - M7_LEFT; yscr = spr->anchor.y*256 - sy*128; yscr = (vc.y - (yscr>>M7O_NORM))*scale>>16; yscr += -sy + M7_TOP; obj_unhide(obj, ATTR0_AFF_DBL); obj_set_pos(obj, xscr, yscr); // .8 // .0 // .8 // .0 return; } while(0); // (5) If we're here, we have an invisible sprite obj_hide(obj); } file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 208/331 28-03-13 Tonc : GBA Programming in rot13 Kart animation Th eb a s i ct h e o r yf o ra n i ma t i o na r o u n das p r i t ei ss i mp l e , n a me l ye q2 1 . 1 7 : t h ev i e wi n ga n g l eψi st h ed i f f e r e n c eb e t we e nt h eg l o b a l s p r i t e a n g l eφ, a n d t h e c a m e r a a n g l e φ a n d t h e a n g l e t o t h e s p r i t e i n c a m e r a s p a c e α : ψ = φ− φ− α . T h e a n g l e t r a n s l a t e s t o a n a n i m a t i o n o c o c f r a met ou s ea n dy o u ' r ed o n e . I nt h e o r y . Th ep r a c t i c eh a san u mb e ro fs n a r e s , e s p e c i a l l yt h ewa ySMKd o e si t .Fi r s t , l o o ka tf i g2 1 . 1 6 .Th e s e1 2f r a me sa r et h eo n e st h a t Su p e rMa r i oKa r tu s e sf o rTo a d .Th ef i r s tc o mp l i c a t i o ni st h a tt h i si so n l yt h er i g h ts i d eo ft h er o t a t i o n ;t h el e f ts i d ei sd o n ev i ami r r o r i n g . Th a t ' se a s ye n o u g h : j u s tc h a n g et h es i g no fp ao ft h ev i e wa n g l ei sn e g a t i v e . Th es e c o n dp r o b l e mi st h en u mb e ro ft i l e s .1 2f r a me sf o rh a l fac i r c l eme a n s2 4f o rt h ef u l l r o t a t i o n( we l l 2 2a c t u a l l y , a swed o n ' t n e e dt od u p l i c a t et h ef r o n ta n db a c kf r a me s ) .At4 x 4 =1 6t i l e se a c h , t h i sg i v e s3 8 4t i l e sf o rTo a da l o n e( a n do n l yt h er o t a t i o na n i ma t i o na t t h a t ! )Mu l t i p l yb y8f o rt h ef u l l s e to fc h a r a c t e r sa n dy o u ' r eway o u to fVRAM.Th i sme a n st h a ty o uc a n ' tl o a da l l t h ef r a me si n t o VRAM i no n eg oa n du s ea no b j e c t ' st i l e i n d e xf o ra n i ma t i o n : y o uh a v et od y n a mi c a l l yl o a df r a me sy o un e e d .Th i si swh yt h es p r i t e s t r u c th a datilesme mb e r , p o i n t i n gt ot h ef u l l s p r i t es h e e ti nROM. Th et h i r dc o mp l i c a t i o ni st h a tt h ef r a me sa r e n ' tu n i f o r ml yd i v i d e do v e rt h ec i r c l e .I fy o ul o o kc l o s e l y , t h ef i r s t8f r a me sa r ef o ra n g l e s 0 °t h r o u g h9 0 ° , t h er e ma i n i n gf o u rf o r9 0 ° 1 8 0 ° .Th er e a s o nb e h i n dt h i si st h a tmo s to ft h et i mey o u ' l l s e et h ek a r t sf r o mt h eb a c k , s oi t 16 p a y st oh a v emo r ef r a me sf o rt h o s e .No w, i nt h et h e o r ywec o u l dc a l c u l a t et h ea n i ma t i o nf r a meq u i t en i c e l y , n a me l yN· ψ/ 2 .Ho we v e r , t h a tr e l i e do nh a v i n gN e q u a l s l i c e s , wh i c hwed o n ' th a v ea n y mo r e .Ord owe ? We l l n o , wed o n ' th a v ee q u a l s l i c e sa n y mo r e .Bu twec a nmake e q u a l s l i c e sa g a i n , u s i n gas o r to fma p p i n g .Fi g2 1 . 1 7s h o wsc o u l d t h ep r i n c i p l ewo r k s .I nt h ef i g u r et h e r ea r e1 2ma i np a r t i t i o n s( i n s i d ec i r c l e ) , wi t h0 , 1 , 1 0a n d1 1c o v e r i n gmo r ea n g u l a rs p a c et h a n2 9 . Ho we v e r , wec a na l s od i v i d et h ec i r c l ei n t o1 6p a r t s( o u t e rc i r c l e ) , a n du s et h es a mef r a mef o rmu l t i p l ee n t r i e s .Fo re x a mp l e , s l i c e 0o f t h ema i ns e q u e n c ei sn o wc o v e r e db ys l i c e 0a n ds l i c e 1o ft h en e ws e q u e n c e .Wh i l ei t ' sp o s s i b l et ou s ei f / e l s eb l o c k st ot h ema p p i n g , i t ' s e a s i e ro ne v e r y o n et oj u s tu s eaLUTf o ri t .Th i sa c t u a l l yt a k e sc a r eo ft woo t h e rp r o b l e msIh a d n ' tme n t i o n e db e f o r e , n a me l yt h a t mi r r o r i n gwo u l dr e q u i r es o mes o r to fr e v e r s a l o ft h en o r ma l s e q u e n c e , a n dt h ef a c tt h a tt h es l i c e sa c t u a l l yh a v et ob eo f f s e tb yh a l fa s l i c es ot h a ty o ud o n ' th a v eas l i c e s wi t c hwh e nl o o k i n ge x a c t l ya tt h ef r o n to rb a c k , f o re x a mp l e .ALUTs o l v e sa l l t h o s ep r o b l e msi n o n eg o . Fig 21.16: To a d ' sf r a me sf r o md i f f e r e n ta n g l e s . Fig 21.17: Us i n gψf o r1 6 e l e me n tLUTe n t r y, i n s t e a do f 1 2n o n e q u a l pa r t i t i o n s . const u8 cKartFrames[32]= { 0, 1, 2, 3, 4, 5, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 10, 10, 9, 9, 8, 8, 7, 7, 6, 5, 4, 3, 2, 1, 0, }; //! Animate kart sprite void kart_animate(M7_SPRITE *spr, const M7_CAM *cam) { OBJ_ATTR *obj= &spr->obj; if(BF_GET2(obj->attr0,ATTR0_MODE) == ATTR0_HIDE) return; TILE *dst= &tile_mem[4][BF_GET(obj->attr2, ATTR2_ID)]; s16 psi= spr->phi - cam->phi; // Extra arctan angle for correctness if(g_state & STATE_OBJ_VIEW_ATAN) psi -= ArcTan2(spr->pos2.z>>8, spr->pos2.x>>8); memcpy32(dst, &spr->tiles[cKartFrames[(psi>>11)&31]*16], 16*8); OBJ_AFFINE *oa= &obj_aff_mem[spr->aff_id]; if(psi < 0) oa->pa= -oa->pa; } file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 209/331 28-03-13 Tonc : GBA Programming in rot13 Th es n i p p e ta b o v es h o wst h ek a r t ' sa n g l e LUTa n da n i ma t i o nr o u t i n e .Th eLUTh a s3 2e n t r i e s , wi t ht h ef i r s ta n dl a s t7u s i n gs i n g l e c h u n k sa n dt h er e s tb e i n gd o u b l e d .Al s on o t et h a tt h eLUTi ss y mme t r i c , wh i c hi sr e q u i r e df o rt h emi r r o r i n g . Th er o u t i n ei t s e l fi s n ' te x a c t l yp r e t t y , b u ti tg e t st h ej o bd o n e .I tc h e c k swh e t h e rt h es p r i t ei sv i s i b l ef i r s ta n db u g so u ti fi t ' sn o t : n o p o i n ti nd o i n gwo r ki fwec a n ' ts e ei t sr e s u l t s .Th es p r i t e ' si n c a me r aa n g l e , α, r e q u i r e sa na r c t a n .I ' v ea d d e das wi t c hi nt h eme n us oy o u c a ns e et h er e s u l t swi t ha n dwi t h o u tt h eαc o r r e c t i o n , a n dIt h i n ky o u ' l l f i n dt h a tt h ed i f f e r e n c ei sp r e t t ys ma l l .Si n c eIa l wa y su s et h e s a meVRAM f o re a c hs p r i t e , f i n d i n gt h ed e s t i n a t i o no ft h et i l e c o p yi se a s y ;f i n d i n gt h es o u r c ef r a mel o o k sal i t t l eu g l y , b u ti t ' sj u s tt h e ψ→s l i c ec o n v e r s i o na n dt h el o o k u p , r e a l l y . 21.7.5. Rounding up: the main loop and other fluff Th eh a r dp a r t so fmo d e7h a v emo r eo rl e s sb e e nc o v e r e dn o w, wi t ht h ep o s s i b l ee x c e p t i o no ft h ema i nl o o p , wh i c hwe ' l l g e tt oi na mo me n t .Th e r ei s , o fc o u r s e , i n i t i a l i z a t i o no ft h er e g i s t e r s , t h es p r i t e sa n dmo d e7v a r i a b l e s , l o a d i n go fVRAM a n di n p u t , b u tt h a t ' sr a t h e r e a s ya n dt e n d st ov a r yf r o mg a met og a mea n y wa y .Fo rt h o s et h i n g s , p l e a s es e et h ea c t u a l c o d e . The main program flow I nt h es n i p p e tb e l o wy o uc a ns e et h emain()f u n c t i o na n di t sma j o rb r a n c h e s .init_main()s e t su pt h ema i nmo d e7v a r i a b l e s , m7_levelt h r o u g hm7_init(), i n i t i a l i z e st h eVBl a n ka n dHBl a n ki n t e r r u p t sa n dv a r i o u so t h e rt h i n g s .Th ema i nl o o pi sq u i t es h o r t . Th ef u n c t i o ninput()d o e sb o t ht h emo v e me n to ft h ec a me r aa n dme n u . Af t e rt h a tc o met h ea c t u a l mo d e7f u n c t i o n s .m7_prep_horizon()h a st oc o mef i r s t , b u tt h eo r d e ro ft h er e s ti sp r e t t ya r b i t r a r y . Iwo u l ds u g g e s tc a l l i n gm7_prep_affines()l a s t , t h o u g h : i t ' st h emo s tc o s t l yf u n c t i o nh e r e , b u ti t ' db ea l r i g h tt ol e ti tr u ni n t o VDr a wt i me .No tt h a tt h a th a p p e n sh e r e( I ' v ec l o c k e dt h ema i nl o o pt oe n da r o u n ds c a n l i n e1 7 0 2 1 0 ) , b u ti t ' db eo k a yi fi td i d . int main() { init_main(); while(1) { VBlankIntrWait(); input(); m7_prep_horizon(&m7_level); // Switch to backdrop display. if(m7_level.horizon > 0) { BF_SET(REG_DISPCNT, DCNT_MODE0, DCNT_MODE); REG_BG2CNT= m7_level.bgcnt_sky; REG_BLDALPHA= 16; } m7_update_sky(&m7_level); update_sprites(); m7_prep_affines(&m7_level); } return 0; } Movement in 3D Th i si st h el a s tt h i n gIwa n tt oc o v e r : h o wt omo v et h i n g si n3 D.Tob ep r e c i s e : h o wt od od i f f e r e n tme t h o d so fmo t i o ni n3 D;wh i c hI ' m s u r ep e o p l emi g h twa n tt ok n o w. 3 Dmo v e me n ti sa c t u a l l ymu c ht h es a mea s2 Dmo v e me n t , e x c e p twi t ha ne x t r ad i me n s i o n .Th er e a s o nwh yp e o p l es o me t i me sf i n d i td i f f i c u l ti st h a tt h e yt h i n ki nt e r mso fa n g l e s , wh e nwh a tt h e yshould b et h i n k i n gi ni sv e c t o r s .Ve c t o r b a s e dmo v e me n t( o rv e c t o r b a s e da n y t h i n g )u s u a l l yma k e st h i n g smu c he a s i e rt h a nwi t ha n g l e sa n dt r i g o n o me t r y .Th i si sa l s owh yt h et h e o r yo ft h i sc h a p t e rh a s b e e nu s i n gv e c t o r sa n dma t r i c e s . He r eI ' l l l o o ki n t ot h r e ed i f f e r e n tmo d e so fc a me r amo v e me n t s : o n eu s i n gt h ewo r l dc o o r d i n a t es y s t e m, o n eu s i n gt h ec a me r as y s t e m, a n do n es o me wh e r ei nb e t we e ns ot h a ti ts t a y sp a r a l l e l t ot h eg r o u n d .Bu tf i r s t , l e t ' st a k eal o o ka twh a tmo v i n gi nac e r t a i nd i r e c t i o n a c t u a l l ymeans. Ev e r yo b j e c ti n3 Ds p a c eh a si t so wnl i t t l ec o o r d i n a t es p a c e , t h elocal frame.Th i si sd e f i n e da sas e to f3v e c t o r s , d e n o t i n gt h e d i r e c t i o n so ft h el o c a l x, ya n dz d i r e c t i o n s .I nt h ec a s eo ft h ec a me r a , In a me dt h e s eu, va n dw, r e s p e c t i v e l y .Th el o c a l ma t r i xi sj u s t a n o t h e rwa yo fwr i t i n gd o wnt h i ss e to fv e c t o r s .Mo v e me n ti su s u a l l yd e f i n e da ss t e p sa l o n gt h e s ev e c t o r s . Asa ne x a mp l eo ft h i s , c o n s i d e ry o u rh e a dt ob et h ec a me r aa n du s ea r r o wst oi n d i c a t et h el o c a l a x e s : u wo u l ds t i c ko u to fy o u rr i g h t e a r , vo u to ft h et o po fy o u rh e a da n dw o u tt h eb a c k .As t e pr i g h two u l db ea l o n gt h eu d i r e c t i o n , a n do n ef o r wa r da l o n g−w.Ag e n e r a l mo v e me n tc o u l db ewr i t t e na sx s t e p sr i g h t , ys t e p su p , a n dz s t e p sb a c k .x, ya n dz a r eu s e da smultipliers f o rt h ed i r e c t i o nv e c t o r s , a n d t h ef i n a l d i s p l a c e me n ti ng l o b a l s p a c ei sdx =x· u +y· v +z· w. An dwh e r ema t r i c e sc o mei n .Th o s et h r e emu l t i p l i e r sc a nb ewr i t t e nav e c t o rr =( x, y, z) , wh i c hi st h ed i s t a n c ev e c t o ri nlocal file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 210/331 28-03-13 Tonc : GBA Programming in rot13 s p a c e .Th et h r e ed i r e c t i o n sf o r me dama t r i xC, Th ed e f i n i t i o no fdx g i v e na b o v ei sn o t h i n ge l s et h a nt h el o n gwa yo fwr i t i n gd o wn dx =C· r.Th ema t r i xmu l t i p l i c a t i o ni sj u s ts h o r t h a n df o r“ s c a l et h ev e c t o r so fC b yt h ee l e me n t so fr a n da d dt h e ma l l u p ” .No t et h a tt h i s p r o c e d u r ewo u l dwo r kf o ra n yo b j e c t , i na n yo r i e n t a t i o n .Al l y o un e e dt od oi sf i n dt h ec o r r e c tl o c a l ma t r i x . I nmyc a s e , Ic o n s t r u c tv e c t o rr i ninput(), b a s e do nv a r i o u sb u t t o n s .Att h i sp o i n ti td o e s n ' tr e a l l yme a na n y t h i n gy e t .Ea c ho ft h e mo v e me n tme t h o d sh a si t so wns e to fd i r e c t i o n sa n dh e n c ei t so wnma t r i xt h a th a st ob ea p p l i e dt or;Ih a v ef u n c t i o n st h a tc a np e r f o r m t h e ma n da d dt h er e s u l t st ot h ec a me r ap o s i t i o n .Al l o ft h e s ec a nb ef o u n di nt a b l e2 1 . 2a n dt h ec o d eb e l o wi t . Th e‘ l e v e l ’( t h a ti s , l e v e l t ot h eg r o u n d )i sp r o b a b l yt h emo s tc o mmo nf o rc a me r as y s t e msf o rg r o u n d b a s e do b j e c t s , t h o u g hu s i n gt h e l o c a l s y s t e mmi g h tma k es e n s ef o rf l y i n go b j e c t s .Ex p e r i me n twi t ht h e ma n ds e ewh a ty o ul i k e . Method Function Transformation Gl o b a l f r a me m7_translate_global()dx =I ·r =r Lo c a l ( c a me r a )f r a me m7_translate_local() dx =C( θ , φ)·r Le v e l : l o c a l b u tp a r a l l e l t og r o u n dm7_translate_level() dx =Ry( φ)·r Table 21.2: Mo v e me n tme t h o d sa n dt he i ra s s o c i a t e dt r a n s f o r ma t i o n st owo r l d s pa c e . Ne wpo s i t i o no f a no b j e c ti sg i v e nb yxw +=vw. //! Translate by \a dir in global frame void m7_translate_global(M7_CAM *cam, const VECTOR *dir) { vec_add_eq(&cam->pos, dir); } //! Translate by \a dir in local frame void m7_translate_local(M7_CAM *cam, const VECTOR *dir) { cam->pos.x += (cam->u.x * dir->x + cam->v.x * dir->y + cam->w.x * dir->z) >> 8; cam->pos.y += ( 0 + cam->v.y * dir->y + cam->w.y * dir->z) >> 8; cam->pos.z += (cam->u.z * dir->x + cam->v.z * dir->y + cam->w.z * dir->z) >> 8; } //! Translate by \a dir using local frame for x/y, but global z void m7_translate_level(M7_CAM *cam, const VECTOR *dir) { cam->pos.x += (cam->u.x * dir->x - cam->u.z * dir->z)>>8; cam->pos.y += dir->y; cam->pos.z += (cam->u.z * dir->x + cam->u.x * dir->z)>>8; } I fy o u ' r en o tr e a l l yf a mi l i a rwi t hma t r i c e st h e yma ys e e mb r i g h ta n ds c a r y , b u tt h e yc a nb eal i f e s a v e ro n c ey o ug e tu s e dt ot h e mab i t . Th e r ei sareason wh yl a r g e3 Ds y s t e msu s et h e mn o n s t o p ;d o i n ge v e r y t h i n gb yr a wt r i gi sh a r d , v e r yh a r d .Ma t r i c e sa l l o wy o ut owo r k wi t h i nwh a t e v e rc o o r d i n a t es y s t e mi smo s tn a t u r a l t ot h et a s ka th a n d , a n dt h e nt r a n s f o r mt owh a t e v e rs y s t e my o un e e di nt h ee n d .I f y o uh a v ea n ywo r kr e l a t e dt og e o me t r y , l e a r n i n gmo r ea b o u tt h eb a s i c so fl i n e a ra l g e b r a( t h er u l e sf o rv e c t o ra n dma t r i xu s e )i swe l l wo r t ht h ee f f o r t . Side note : centering on a sprite Asa ne x a mp l eo fh o we a s yma t r i c e sc a nma k el i f e , c o n s i d e rt h ei s s u eo fc e n t e r i n gt h ec a me r ao nag i v e ns p r i t ea n dt h e n r o t a t i n ga r o u n di t .Yo uh a v et h ec a me r ama t r i xC, t h ed i s t a n c ey o uwa n tt ov i e wf r o m, Za n dp r e s u ma b l yt h es p r i t ep o s i t i o n , x w.Wh a ty o un e e dt od oi s : mo v et h ec a me r at ot h es p r i t e ' sp o s i t i o n , t h e nt a k eZ s t e p sb a c k .I no t h e rwo r d sacw =x w+C· ( 0 , 0 , Z) , wh i c hb o i l sd o wnt oacw =x w+Zw, On c ey o uk n o wt h ec a me r ama t r i x , p o s i t i o n i n gi tp r a c t i c a l l ywr i t e si t s e l f . 21.8. Concluding remarks I t ' sd o n e , f i n a l l y !Th i sc h a p t e r ' st e x te x p l a i n e dt h emo s ti mp o r t a n te l e me n t so famo d e7g a me : c a l c u l a t i o no ft h ea f f i n ep a r a me t e r s , a d d i n gah o r i z o na n db a c k d r o p , p o s i t i o n i n g , s o r t i n gand a n i ma t i n g3 Ds p r i t e sa n da sab o n u sh o wt ou s ec r e a t ead i s t a n c ef o g g i n ge f f e c t . I nt h ep r e c e d i n gt e x t , I ' v eu s e ds t u f ff r o mj u s ta b o u te v e r ys u b j e c td e s c r i b e di nt h er e s to fTo n c , a n dn o tj u s tt h ee a s yp a r t s .I fy o u ' r e h e r ea n du n d e r s t o o da l l o rmo s to ft h ea b o v e , c o n g r a t u l a t i o n s . Bu ts t i l l I ' v eo mi t t e daf e wt h i n g st h a two u l dma k ei tal i t t l eb e t t e r .Ge t t i n gr i do fa l l t h o s ed i v i s i o n si nt h eλc a l c u l a t i o n s , f o ri n s t a n c e . Org e t t i n ga r o u n dt h e3 2a f f i n eo b j e c tl i mi t a t i o no rp l a c i n gs h a d o wsf o rt h es p r i t e so nt h ef l o o r .No rh a v eIs h o wnh o wt oc o r r e c t l ya l l o w f o rl o o p i n g s , i n s t e a do fc l a mp i n gt h ep i t c ha ts t r a i g h tu po rd o wn .Th e s et h i n g sa r er e l a t i v e l ye a s yt og r a s p , c o n c e p t u a l l y , b u t i mp l e me n t i n gt h e mwo u l dr e q u i r eal o tmo r ec o d e .I fy o uu n d e r s t o o dt h i st e x t , I ' ms u r ey o uc a nf i g u r ei to u to ny o u ro wn . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 211/331 28-03-13 file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm Tonc : GBA Programming in rot13 212/331 28-03-13 Tonc : GBA Programming in rot13 22. Tonc's Text Engine I n t r o d u c t i o n Ba s i cd e s i g n Ti l e ma p p e dt e x t Bi t ma p p e dt e x t Ob j e c tt e x t Re n d e r i n gt ot i l e s Sc r i p t i n g , c o n s o l eI Oa n do t h e rn i c e t i e s Co n c l u s i o n s 22.1. Introduction Th eo t h e rp a g eo nt e x td e s c r i b e dh o wy o uc o u l dg e tt e x to nb a c k g r o u n d sa n do b j e c t s .I two r k e d , b u tt h e r ewe r es e v e r a l l i mi t a t i o n s .Fo r i n s t a n c e , i twa sl i mi t e dt o8 ×8f o n t s , d i d n ' ts u p p o r ta l l v i d e omo d e sa n dh a dn of o r ma t t e dt e x tc a p a b i l i t i e s . To n c ' sTe x tEn g i n e( TTE)r e me d i e sma n yo ft h e s es h o r t c o mi n g s .I nt h i sc h a p t e rI ' l l d e s c r i b et h eg o a l sa n db a s i cd e s i g no ft h e s y s t e ma n ds o meo ft h ei mp l e me n t a t i o nd e t a i l s .I np a r t i c u l a r , I ' l l d e s c r i b eh o wt ob u i l dwr i t e r sf o ru s eo ft h ed i f f e r e n tk i n d so fs u r f a c e s . I ns o mec a s e s , I ' l l o p t i mi z et h el i v i n gh e l l o u to ft h e mb e c a u s ei ti sp o s s i b l ef o rag l y p hr e n d e r e rt ot a k emu l t i p l es c a n l i n e sf o ras i n g l e c h a r a c t e ri fy o ud o n ' tp a ya t t e n t i o n .An dy e s , t h i swi l l b ed o n ei na s s e mb l y . I ' l l a l s os h o wh o wy o uc a na d ds o meb a s i cs c r i p t i n gt oc h a n g ec u r s o rp o s i t i o n s , c o l o r sa n de v e nf o n t sd y n a mi c a l l y .Af e wy e a r sa g o , Wi n t e r mu t ec h a n g e dt h es t a n d a r dCl i b r a r yi nd e v k i t ARM t oma k et h es t d i or o u t i n e sa c c e s s i b l ef o rGBAa n dNDS.I ' l l a l s os h o wh o w y o uc a nma k eu s eo ft h i s . An d , o fc o u r s e , t h e r ewi l l b ed e mo s .Oh , wi l l t h e r eb ed e mo s .Th e r ea r ea b o u t1 0o ft h e mi nf a c t , s oI ' mg o i n gt od ot h i n g sal i t t l eb i t d i f f e r e n t l yt h a nb e f o r e : t h e r ewi l l b eo n ep r o j e c tc o n t a i n i n game n uwi t ha l l t h ee x a mp l e s .No ta l l e x a mp l e swi l l b es h o wnh e r eb e c a u s e t h a t ' dj u s tb et o omu c h . La s t l y , i ti se x p e c t e dt h a tb yn o wy o uh a v ead e c e n tk n o wl e d g eo fGBAp r o g r a mmi n g , s oI ' mg o i n gt ok e e pt h ea mo u n to fGBAs p e c i f i ce x p o s i t i o nt oami n i mu m.Wh e ny o us e ef u n c t i o n su s e dt h a th a v e n ' tb e e nc o v e r e da l r e a d y , t u r nt oGBATe k , t h ep r o j e c t ' sc o d e o rt o n c l i b ' sc o d ef o rd e t a i l s . 22.2. Basic design 22.2.1. TTE Goals Th ef o l l o wi n gl i s th a st h et h i n g sImo s twa n t e di nTTE: A comprehensive and extensible set of glyph writers, usable for all occasions.We l l almost a l l o c c a s i o n s .Th eo l d s y s t e mwo r k e df o rr e g u l a rb a c k g r o u n d s , b i t ma pmo d e sa n do b j e c t s , I ' mn o we x t e n d i n gt h a ts e tt oa f f i n eb a c k g r o u n d sa n dt i l e r e n d e r i n g .I fwh a ty o un e e di s n ' tp r e s e n ti nt h es t a n d a r ds e t , y o uc a ne a s i l yc r e a t ey o u ro wnwr i t e ra n du s et h a to n ei n s t e a d . Th ewr i t e rwi l l a c c e p tUTF8s t r i n g s , me a n i n gy o u ' r en o tl i mi t e dt o2 5 6c h a r a c t e r s . Fonts: arbitrary widths and heights and variable width characters.I n s t e a do fb e i n gl i mi t e dt o8 x 8 @1g l y p h s ;t h e s t a n d a r dwr i t e r si nTTEa r ea b l et ou s ef o n t so fa n ywi d t ha n dh e i g h t( wi t h i nr e a s o n : n os c r e e n f i l l i n gg l y p h sp l e a s e )a n d v a r i a b l ewi d t hf o n t s( a g a i n , wi t h i nr e a s o n : VWFf o rt i l e ma p sma k e sl i t t l es e n s e ) .I np r i n c i p l e , t h e r ea r ep o s s i b i l i t i e st ou s e a r b i t r a r yb i t d e p t h sa swe l l , b u tt h es t a n d a r dr e n d e r e r sa r el i mi t e dt o1 b p p . A simple writer-interface independent of surface details.Fo rt h eo l ds y s t e mIh a dm3_puts(), se_puts(), obj_puts()a n ds u c h .Th i swo r k e d , b u ti tme a n ty o uh a dt ou s es o me t h i n gd i f f e r e n tf o rt h ed i f f e r e n tmo d e s .I nTTE, t h e r e a r ed i f f e r e n ti n i t i a l i z e r sf o rt h ed i f f e r e n tmo d e st os e tu pt h es y s t e m, a n das i n g l es t r i n gwr i t e rtte_write()t h a tj u s t wo r k s . Scripting for text parameters.Byt h a tIme a nt h a ty o uc a nc o n t r o l p a r a me t e r sl i k ep o s i t i o na n do u t p u tc o l o rb yt h es t r i n g s t h e ms e l v e s .Th ef u n c t i o n a l i t yf o rt h i si sp r e t t yb a s i c , b u ti two r k swe l l e n o u g h .No t e : t h i si snot af u l l d i a l o gs y s t e m!Th a ts a i d , i ts h o u l db ep o s s i b l et ob u i l do n ea r o u n di t . printf()support.Fo rr a t h e ro b v i o u sr e a s o n s . 22.2.2. Structures and main components Al l t h er e l e v a n ti n f o r ma t i o nf o rTTEi sk e p tg a t h e r e di nt h r e es t r u c t s : atext context, TTC;afont description, TFont;a n dagraphic surface description, TSurface. Th eTTCs t r u c tc o n t a i n st h ema i np a r a me t e r sf o rt h ee n g i n e : i n f o r ma t i o na b o u tt h es u r f a c ewe r er e n d e r i n gt o , c u r s o rp o s i t i o n s , f o n t i n f o r ma t i o n , c o l o ra t t r i b u t e sa n daf e wo t h e rt h i n g s .I ta l s oc o n t a i n st woc a l l b a c k sf o rd r a wi n ga n de r a s i n gg l y p h s . Th eTFonts t r u c th a sap o i n t e rt ot h eg l y p hd a t a , g l y p h / c e l l d i me n s i o n sa n daf e wo t h e rt h i n g s .Th e r ea r ea l s op o i n t e r st owi d t ha n d h e i g h tt a b l e st oa l l o wv a r i a b l ewi d t ha n dh e i g h tf o n t s .I ' v eh a c k e daTFontc r e a t o ri n t ou s e n t i awh i l eb a c ks ot h a tIc o u l de a s i l yc r e a t e t h e s et h i n g sf r o ms t a n d a r df o n t s , b u ty o uc a na l s oma k ey o u ro wnf r o ms c r a t c h . Th eTSurfaces t r u c ta c t u a l l yh a sn o t h i n gt od owi t ht e x t .I n s t e a d , i t ' sas t r u c td e s c r i b i n gt h ek i n do fs u r f a c ewe ' r er e n d e r i n go n . Th i sc a nb eb i t ma p s , t i l e s , t i l e ma p so rwh a t e v e r .To n c l i bh a sb a s i cp i x e l , l i n ea n dr e c t a n g l er o u t i n e sf o rd e a l i n gwi t ht h e s es u r f a c e s , s oI file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 213/331 28-03-13 Tonc : GBA Programming in rot13 mi g h ta swe l l u s et h e m. //# From tonc_tte.h : main TTE types. typedef struct TFont { const void *data; const u8 *widths; const u8 *heights; u16 charOffset; u16 charCount; u8 charW; u8 charH; u8 cellW; u8 cellH; u16 cellSize; u8 bpp; u8 extra; } TFont; //!< Character data. //!< Width table for variable width font. //!< Height table for variable height font (mostly unused). //!< Character offset //!< Number of characters in font. //!< Character width (fwf). //!< Character height.(fhf). //!< Glyph cell width. //!< Glyph cell height. //!< Cell-size (bytes). //!< Font bitdepth; //!< Padding. Free to use. //! TTE context struct. typedef struct TTC { // Members for renderers TSurface dst; //!< Destination surface. s16 cursorX; //!< Cursor X-coord. s16 cursorY; //!< Cursor Y-coord. TFont *font; //!< Current font. u8 *charLut; //!< Character mapping lut, if any. u16 cattr[4]; //!< ink, shadow, paper and special color attributes. // Higher-up members u16 reserved; u16 ctrl; //!< BG control flags. u16 marginLeft; u16 marginTop; u16 marginRight; u16 marginBottom; s16 savedX; s16 savedY; // Callbacks and table pointers fnDrawg drawgProc; //!< Glyph render procedure. fnErase eraseProc; //!< Text eraser procedure. const TFont **fontTable; //!< Pointer to font table for const char **stringTable; //!< Pointer to string table for } TTC; //# Supporting types //! Glyph render function format. typedef void (*fnDrawg)(int); //! Erase rectangle function format. typedef void (*fnErase)(int left, int top, int right, int bottom); typedef struct TSurface { u8 *data; //!< Surface data pointer. u32 pitch; //!< Scanline pitch in bytes. u16 width; //!< Image width in pixels. u16 height; //!< Image width in pixels. u8 bpp; //!< Bits per pixel. u8 type; //!< Surface type. u16 palSize; //!< Number of colors. u16 *palData; //!< Pointer to palette. } TSurface; TFont details Fi g2 2 . 1s h o wsac h a r a c t e rs h e e tt h a tTFontc a nu s e .Th es h e e ti sama t r i xo fcellsa n de a c hc e l l c o n t a i n sac h a r a c t e r .Th ecellW/H me mb e r sa r et h ed i me n s i o n so ft h e s ec e l l s ;cellSizei st h en u mb e ro fb y t e sp e rc e l l . Ea c hc e l l h a so n eg l y p h , b u tt h ea c t u a l g l y p h sc a nb es ma l l e rt h a nt h ec e l l s( wh i t ev sma g e n t ap a r t s ) .Th i sd o e swa s t eab i to f file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 214/331 28-03-13 Tonc : GBA Programming in rot13 me mo r y , b u ti ta l s oh a ss e v e r a l b e n e f i t s .On eo ft h eb e n e f i t si st h a ty o uc a nu s ecellSizet oq u i c k l yf i n dt h ea d d r e s so fa n yg i v e n g l y p h .Se c o n d , b e c a u s eIwa n tb yf o n t st ob eu s a b l ef o rb o t hb i t ma p sand t i l e s , myg l y p hb o x e swo u l dh a v et ob emu l t i p l e so f8a n y wa y . Ad d i t i o n a l l y , t h i sp a r t i c u l a rf o n twi l l b e1 b p p , me a n i n gt h a te v e nwi t ht h ewa s t e dp a r t sI ' l l s t i l l h a v eav e r yl o wme mo r yf o o t p r i n t( 3 . 5 k B) . Fo rf i x e d wi d t ho rf i x e d h e i g h tf o n t s , me mb e r scharWa n dcharHd e n o t et h ea c t u a l c h a r a c t e rwi d t ha n d h e i g h t .Fo rf o n t so fv a r i a b l ewi d t h s , t h ewidthsme mb e rp o i n t st ot h eab y t e a r r a yc o n t a i n i n gt h ewi d t h so f t h eg l y p h sa n ds o me t h i n gs i mi l a ri st r u ef o rt h eheights.charOffseti st h e( ASCI I )c h a r a c t e rt h ed a t a s t a r t sa t .Fo n ts h e e t so f t e ns t a r ta tas p a c e( ' ' ) , s ot h i st e n d st ob e3 2 .charCounti st h en u mb e ro f c h a r a c t e r sa n dc a nb eu s e di fy o un e e dt oc o p yt h ewh o l es h e e tt oVRAM ( l i k ei nt h ec a s eo ft i l e ma p p i n g ) . Pl e a s en o t et h a th o wt h ed a t ai naTFonti su s e dd e p e n d sa l mo s te n t i r e l yo nt h eg l y p hr e n d e r e r .Mo s t r e n d e r e r st h a tc o mewi t ht o n c l i be x p e c tt h i sf o r ma t : Bi t p a c k e dt o1 bpp, f o rs i z er e a s o n s .An df o rr e n d e r i n gs p e e dt o o , a c t u a l l y , s i n c eme mo r yl o a d sa r e e x p e n s i v e . Ti l e d b y g l y p h .Th ed a t af o re a c hg l y p hi sc o n t i n g e n twi t hcellSizeb y t e sb e t we e ne a c hg l y p h .Th i si s s i mi l a rt oh o w1 Do b j e c two r kwi t ho n ei mp o r t a n td i f f e r e n c e : t h et i l e si ne a c hg l y p ha r ecolumn-major ( t i l e1i su n d e rt i l e0 ) .Th i si nc o n t r a s tt oo b j e c t s , wh i c ht e n dt o b er o wma j o r( t i l e1i st ot h er i g h to ft i l e0 ) .Iwi l l r e f e rt ot h i sf o r ma ta stile-strips.Th er e a s o nb e h i n d t h i sc h o i c ewi l l b eg i v e nl a t e r . Fig 22.1: Ve r d a na9 Th e r ea r ee x c e p t i o n st ot h i s , b u tmo s tr e n d e r e r sp r e s e n t e dh e r ewi l l u s et h i sf o r ma t .I fy o uwa n tt oma k ey o u r o wnr e n d e r e r s , y o u ' r ef r e et ou s ea n yf o r ma tf o rt h ed a t ay o ut h i n ki sa p p r o p r i a t e . c h a r a c t e rs he e t TTC details Th et e x tc o n t e x t , TTC, c o n t a i n st h emo s ti mp o r t a n td a t ao ft h es y s t e m.St a r t i n ga tt h et o p : t h es u r f a c e , dst.Th i sd e f i n e st h es u r f a c e we ' r er e n d e r i n gt o .Th emo s tr e l e v a n ti t e mst h e r ea r ei t sme mo r ya d d r e s s , pitch: t h en u mb e ro fb y t e sp e rs c a n l i n e .Th ep i t c hi savery i mp o r t a n tp a r a me t e rf o rr e n d e r i n g , mo r ei mp o r t a n tt h a nt h ewi d t ha n dh e i g h ti nf a c t .Th es u r f a c ea l s oh a sp a l e t t eme mb e r s , wh i c hc a n b eu s e dt oa c c e s si t sc o l o r s .Mu c hl i k et h eTFontme mb e r s , h o wt h i sd a t ai su s e dl a r g e l yd e p e n d so nt h er e n d e r e r . Th eme mb e r scursorX/Ya r ef o rt h ec u r r e n tc u r s o rp o s i t i o n .Th emarginr e c t a n g l ei n d i c a t e swh i c hp a r to ft h es c r e e ns h o u l db e u s e df o rt e x t .I ft h ec u r s o re x c e e d st h er i g h tma r g i n , i twi l l b emo v e dt ot h el e f tma r g i na n do n el i n ed o wn .Th ema r g i n sa r ea l s ou s e df o r s c r e e n c l e a r sa n dr e t u r n i n gt ot h et o po ft h ep a g e . Th ecattrt a b l ei ss o me t h i n gs p e c i a l .I t se n t r i e sa r ecolor attributes .Pa r a me t e r sf o ri n k( f o r e g r o u n dc o l o r ) , s h a d o w, p a p e r ( b a c k g r o u n dc o l o r )a r ep u th e r e , a l o n gwi t ha‘ s p e c i a l ’f i e l dwh i c hi sv e r ymu c hc o n t e x t s p e c i f i c .No t et h a tt h e s ec o l o ra t t r i b u t e sd on o t n e c e s s a r i l yr e p r e s e n tc o l o r s .I nmo d e s3a n d5t h e y ' r ec o l o r s , b u tf o rmo d e4a n dt i l ewr i t e r st h e y ' r ec o l o ri n d i c e s .Th e r e ' sp r o b a b l ya n i c e rn a mef o rt h i st h a n‘ c o l o ra t t r i b u t e ’ , b u ts o d o myn o ns a p i e n s . Re n d e r i n gg l y p h sa n de r a s i n g( p a r t so f )t h es c r e e ni sd o n et h r o u g ht h ec a l l b a c k sdrawgProca n deraseProc.Th ei d e ai st h a t y o ui n i t i a l i z et h es y s t e mwi t ht h er o u t i n e sa p p r o p r i a t ef o ry o u rt e x tf o r ma ta n dTTEu s e st h e mt od ot h ea c t u a l wr i t i n g .Is h o u l dp o i n to u t t h a tu s i n gc a l l b a c k sf o rr e n d e r i n gas i n g l eg l y p hc a nh a v eas i g n i f i c a n to v e r h e a d , e s p e c i a l l yf o rt h es i mp l e rk i n d so ft e x tl i k et i l e ma p s . 22.2.3. Main TTE variables and functions. Th es t a t eo ft h eTTEs y s t e mi sk e p ti naTTCv a r i a b l ea c c e s s i b l et h r o u g htte_get_context().Al l c h a n g e st ot h es y s t e mg o t h r o u g ht h e r e .I nsome c a s e s , i ti su s e f u l t oh a v et wos e t so fs t a t ea n ds wi t c hb e t we e nt h e mwh e na p p r o p r i a t e( l i k ewh e ny o uh a v et wo s c r e e n s .Yh e l l ot h a r , NDS) .Fo rt h a ty o uc a nu s ette_set_context)t or e d i r e c tt h ep o i n t e r . TTC __tte_main_context; TTC *gp_tte_context= &__tte_main_context; //! Get the master text-system. INLINE TTC *tte_get_context() { return gp_tte_context; } //! Set the master context pointer. void tte_set_context(TTC *tc) { gp_tte_context= tc ? tc : &__tte_main_context; } Top r i n tc h a r a c t e r s , y o uc a nu s ette_putc()a n dtte_write(). //! Get the glyph index of character \a ch. INLINE uint tte_get_glyph_id(int ch) { TTC *tc= tte_get_context(); ch -= tc->font->charOffset; return tc->charLut ? tc->charLut[ch] : ch; } //! Get the width of glyph \a id. file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 215/331 28-03-13 Tonc : GBA Programming in rot13 INLINE uint tte_get_glyph_width(uint gid) { TFont *font= tte_get_context()->font; return font->widths ? font->widths[gid] : font->charW; } //! Render a character. int tte_putc(int ch) { TTC *tc= tte_get_context(); TFont *font= tc->font; // (4) translate from character to glyph index uint gid= tte_get_glyph_id(ch); // (5) get width for cursor update int charW= tte_get_glyph_width(gid); if(tc->cursorX+charW > tc->marginRight) [[ simulate newline ]] // (6) Draw and update position tc->drawgProc(gid); tc->cursorX += charW; return charW; } //! Render a string. /*! \param text String to parse and write. \return Number of parsed characters. */ int tte_write(const char *text) { int ch; uint gid, charW; const char *str= text; TTC *tc= tte_get_context(); while( (ch= *str++) != '\0' ) { // (1) Act according to character type switch(ch) { case '\n': [[ update cursorX/Y for newline ]]; break; case '\t': [[ update cursorX for tab ]]; break; default: // (2) more special thingies if(ch=='#' && str[0]=='{') // (2a) Command sequence { str= tte_cmd_default(str+1); break; } else if(ch=='\\' && str[0]=='#') // (2b) Escaped command ch= *str++; else if(ch>=0x80) // (2c) UTF8 character ch= utf8_decode_char(str-1, &str); // (3) draw character tte_putc(ch); } } return str - text; } I ' v eo mi t t e dt h ec o d ef o raf e wt h i n g sh e r e , t h ei d e as h o u l db ec l e a r .Fi r s t , r e a dac h a r a c t e r .Th e n , c h e c kwh e t h e ri t ' sas p e c i a l c h a r a c t e r ( n e wl i n e , t a b , f o r ma t t i n gc o mma n d )a n di fs o , a c ta c c o r d i n g l y .Be c a u s ette_write()s u p p o r t sUTF8 , wea l s oc h e c kf o rt h a ta n d d e c o d et h es t r i n gf o raf u l l UFT8c h a r a c t e r .Af t e rt h a t ' sa l l d o n e , wep a s st h ec h a r a c t e ro nt otte_putc(), wh i c ht r a n s l a t e si tt oa g l y p hi n d e x , d r a wst h eg l y p ha n da d v a n c e st h ec u r s o r . No t e : t h ewa yd e s c r i b e dh e r ei sa me t h o do fd o i n gt h i n g s ;i t ' sn o tthe me t h o d , b e c a u s et h a td o e s n ' ta c t u a l l ye x i s t .Se v e r a l s t e p sd o n e file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 216/331 28-03-13 Tonc : GBA Programming in rot13 h e r ema yb eo v e r k i l l f o rt h ek i n do ft e x ty o uh a di nmi n d .Fo re x a mp l e , g e t t i n gf r o mt h ec h a r a c t e rt ot h eg l y p hi n d e xi sd o n eb yt h ef o n t ' s c h a r a c t e ro f f s e ta n dap o t e n t i a l c h a r a c t e rl o o k u pt a b l e , n e i t h e ro fwh i c hi ss t r i c t l yn e c e s s a r y .Li k e wi s e , wr a p p i n ga tt h ee d g e sma y a l r e a d yb ed o n ei nt h es t r i n gi t s e l fwi t hn e wl i n ec h a r a c t e r s .Ont h eo t h e rh a n d , y o umi g h tl i k emo r ec o mp l e xwr a p p i n g , t e x ta l i g n me n t , s c r o l l i n g , a n dwh ok n o wswh a te l s e .I fy o uwa n tt h e s et h i n g s , c r e a t i n gy o u ro wnr o u t i n es h o u l d n ' tb et o od i f f i c u l t . 22.2.4. On nomenclature So met e r msIu s ei nTTEh a v eav e r ys p e c i f i cme a n i n g .Be c a u s et h ed i f f e r e n c e sb e t we e nt e r msc a nb es u b t l e , i ti si mp o r t a n tt od e f i n e t h et e r me x p l i c i t l y .Ad d i t i o n a l l y , TTEu s e ss e v e r a l a c r o n y msa n da b b r e v i a t i o n st h a tn e e dt ob ec l a r i f i e d . char(acter) vs glyph index.‘ Ch a r a c t e r ’r e f e r st ot h eASCI Ic h a r a c t e r ;t h e‘ g l y p hi n d e x ’i st h ec o r r e s p o n d i n gi n d e xi nt h e f o n t .Fo re x a mp l e , ‘ A’i sc h a r a c t e r6 5 , b u ti ft h ef o n ts t a r t sa tas p a c e( ' ' , ASCI I3 2 )t h e‘ A’i sg l y p hi n d e x6 5 −3 2 =3 3 .Asa r u l e , v a r i a b l e sn a me dcha r ec h a r a c t e r sa n dgidme a n sg l y p hi n d e x .Th ei n p u to ft h er e n d e r e r si st h eg l y p hi n d e x , a n dn o tt h e c h a r a c t e r . Surface.Su r f a c ei st h et e r mI ' mu s i n gt od e s c r i b ewh a t e v e rI ' mma n i p u l a t i n gt os h o wt e x t .Th i si su s u a l l yVRAM, b u tc a nb e o t h e rt h i n g sa swe l l , l i k eOBJ _ATTRsf o ro b j e c tt e x t . Pitch.Th ep i t c hi sa c t u a l l yac o mmo nt e r mi ng r a p h i c s , b u ts i n c eg r a p h i c st e r msma yn o tb es oc o mmo n , i t ' swo r t hr e p e a t i n g . Te c h n i c a l l y , t h epitch i st h en u mb e ro fs c a n l i n e sb e t we e nr o ws .I ' me x t e n d i n gi tal i t t l et ome a nt h echaracteristic major distance f o rma t r i c e s .Ma t r i c e sa r e2 De n t i t i e s , a n dt h e y ' l l h a v ea d j a c e n te l e me n t si no n ed i r e c t i o na n dal a r g e rd i s t a n c ef o r t h eo t h e r .Th e s eu s u a l l ya r ex a n dy, r e s p e c t i v e l y , b u tn o ta l wa y s .Th eminor d i s t a n c ewi l l b er e f e r r e dt oa sstride. Color vs color attribute.Th e‘ c o l o r ’i st h er e a l 5 . 5 . 5BGRc o l o r ;t h e‘ c o l o ra t t r i b u t e ’i swh a t e v e rt h er e n d e r e rwi l l u s eo n t h es u r f a c e .Th i sc a nb eac o l o r , b u ti tc a na l s ob eap a l e t t ei n d e xo rs o me t h i n ge l s ee n t i r e l y .Th ei n t e r p r e t a t i o ni su pt ot h e r e n d e r e r . Render/Text family.Th i si sac o n c e p t u a l g r o u p n a mef o ras p e c i f i ck i n do ft e x t .Ta b l e2 2 . 1g i v e sa no v e r v i e wo ft h e f a mi l i e sa v a i l a b l e .Th i sl a r g e l yc o r r e s p o n d st ot h eTSurfacet y p e s . Renderer types.Wi t h i ne a c hf a mi l yt h e r ec a nb ed i f f e r e n tr e n d e r e r sf o rd i f f e r e n tk i n d so ff o n t sa n de f f e c t s .Fo re x a mp l e , wh e nr e n d e r i n gt oa n8 b p pb i t ma p( t h eb mp 8f a mi l y ) , y o uc a nh a v ed i f f e r e n tr e n d e r e r sf o rd i f f e r e n tf o n tb i t d e p t h s( 1 b p po r 8 b p p , f o re x a mp l e )o rg l y p hl a y o u t s( b i t ma p p e do rt i l e d ) .Th e yc a nr e n d e rs o mep i x e l st r a n s p a r e n t l y , o ra p p l ya n t i a l i a s i n g .Or a n yc o mb i n a t i o no ft h o s e .Th ep o i n ti st h e r ea r ealot o fo p t i o n sh e r e . Be c a u s eIr e a l l yd o n ' tl i k en a me st h a ts p a nawh o l el i n e , Iwi l l u s ea b b r e v i a t i o n si nt h er e n d e r e r ' sn a met oi n d i c a t ewh a ti t d o e s ;s e et a b l e2 2 . 2f o rwh a tt h e yme a n .Fo rt h emo s tp a r t , t h er e n d e r e r swi l l b ef o rf o n t swi t ha r b i t r a r ywi d t ha n dh e i g h t , wi t h a1 b p pt i l e s t r i p p e dg l y p h s , wi t ht h e ywi l l d r a wt h e mt r a n s p a r e n t l ya n dr e c o l o r i n go fp i x e l s .Th i si si n d i c a t e db y*_b1cts. Family prefix Initializer Re g u l a rt i l e ma p( mo d e0 / 1 ) s e v o i dt t e _i n i t _s e ( i n tb g n r , u 1 6b g c n t , SCR_ENTRYs e 0 , u 3 2c o l o r s , u 3 2b u p o f s , c o n s tTFo n t* f o n t , f n Dr a wgp r o c ) ; Af f i n et i l e ma p( mo d e1 / 2 ) v o i dt t e _i n i t _a s e ( i n tb g n r , u 1 6b g c n t , u 8a s e 0 , u 3 2c o l o r s , u 3 2b u p o f s , c o n s t TFo n t* f o n t , f n Dr a wgp r o c ) ; a s e 4 b p pTi l e s( mo d e s0 / 1 / o b j ) c h r 4 (c/r) v o i dt t e _i n i t _c h r 4 (c/r)( i n tb g n r , u 1 6b g c n t , u 3 2c a t t r s , u 3 2c o l o r s , c o n s tTFo n t * f o n t , f n Dr a wgp r o c ) ; 8 b p pb i t ma p( mo d e4 ) b mp 8 v o i dt t e _i n i t _b mp ( i n tv mo d e , c o n s tTFo n t* f o n t , f n Dr a wgp r o c ) ; 1 6 b p pb i t ma p( mo d e 3 / 5 ) b mp 1 6 v o i dt t e _i n i t _b mp ( i n tv mo d e , c o n s tTFo n t* f o n t , f n Dr a wgp r o c ) ; o b j e c t s o b j v o i dt t e _i n i t _o b j ( OBJ _ATTR* d s t , u 3 2a t t r 0 , u 3 2a t t r 1 , u 3 2a t t r 2 , u 3 2c o l o r s , u 3 2b u p o f s , c o n s tTFo n t* f o n t , f n Dr a wgp r o c ) ; Table 22.1: TTEr e n d e rf a mi l yi n d i c a t o r sa n di n i t i a l i z e r s .4b ppTi l e sc a nb er o wo rc o l u mnma j o r( crh4ro rchr4c) . Code Description b x Bi t d e p t ho fs o u r c ef o n t .( b1=1b p p ) wx Sp e c i f i cwi d t h( w8=wi d t h8 ) h x Sp e c i f i che i g h t( h8=h e i g h t8 ) c Re co l o r i n g .Co l o ra t t r i b u t e sa r ea p p l i e dt ot h ep i x e l si ns o mewa y . t / o Tr a n s p a r e n to rop a q u ep a p e rp i x e l s . s Gl y p h sa r ei nt i l e st r i pf o r ma t . Table 22.2: Re n d e rt ypes u mma r y. La s t l y , an o t eo ns o meo ft h ea b b r e v i a t i o n sIu s ei nt h er e n d e r i n gc o d e .An u mb e ro ft e r msc o meu pa g a i na n da g a i n , a n dI ' v et a k e nt o u s eas h o r t h a n dn o t a t i o nf o rt h e s ei t e ms .Th eb a s i cf o r ma ti sfooXwh e r efooi st h er e l e v a n tb i t ma p / s u r f a c ea n dXi sao n e l e t t e rc o d e f o rt h i n g sl i k ewi d t h , h e i g h t , d a t aa n do t h e r s .Ye s , t h eu s eo fs i n g l e l e t t e rn a me si sf r o wn e du p o na n dId o n ' ta d v o c a t et h e i ru s ei n g e n e r a l , b u tI ' v ef o u n dt h a ti nt h i sp a r t i c u l a rc a s e , i fu s e dj u d i c i o u s l y , t h e yh a v eh e l p e dmer e a dmyo wnc o d e . Term Meaning file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 217/331 28-03-13 Tonc : GBA Programming in rot13 fooW Wi d t ho ffoo fooH He i g h to ffoo fooB Bi t d e p t ho ffoo fooP Pi t c ho ffoo fooD Pr i ma r yd a t a p o i n t e rf o rfoo fooL Se c o n d a r yd a t a p o i n t e rf o rfoo fooS Si z eo ffoo fooN Nu mb e r / c o u n to ffoo Ta bl e2 2 . 3 . Ab b r e v i a t i o n su s e di nr e n d e r i n g c o d e . 22.3. Tilemapped text 22.3.1. Regular tilemap text Ti l e ma p p e dt e x ti st h ee a s i e s tt oi mp l e me n t , b e c a u s ey o ud o n ' tr e a l l yh a v et or e n d e ra n y t h i n ga ta l l .Yo us i mp l yl o a du pa l l t h ef o n t ' st i l e s i n t oac h a r b l o c ka n dp l a c es c r e e n e n t r i e sf o rt h ea c t u a l t e x t . Th ei n i t i a l i z e rf o rr e g u l a rt i l e ma p si stte_init_se().I t ' si d e n t i c a l t otxt_init_se()e x c e p tf o rt h et woe x t r ap a r a me t e r sa t t h ee n d : fonta n dproc.Th e s er e p r e s e n tt h ef o n tt ou s ea n dt h er e n d e r e rt h a td o e st h es u r f a c ema n i p u l a t i o n .Ev e r yi n i t i a l i z e ri nTTE h a st h o s et wop a r a me t e r s .I t ' ss a f et op a s sNULLt ot h e mi fy o u ' r en o ts u r ewh a tt ou s e ;i nt h a tc a s e , t h ed e f a u l to p t i o nf o rt h a tf a mi l y wi l l b eu s e d . I ffonti sNULL, y o u ' l l g e tt h ed e f a u l tf o n t .Th i si se i t h e rsystem8Fontf o rf i x e d wi d t ho c c a s i o n so rverdana9Font ( f i g2 2 . 1 )wh e nv a r i a b l ewi d t hi ss u i t a b l e .Th e s ec a na l s ob er e f e r e n c e dv i afwf_defaulta n dvwf_default, r e s p e c t i v e l y . Ea c hf a mi l ya l s oh a sad e f a u l tr e n d e r e r , #d e f i n e da sfoo_drawg_default, wh e r efoo i st h ef a mi l yp r e f i x .Th ed e f a u l t r e n d e r e r sa r et h eg e n e r a l r o u t i n e s , s u i t a b l ef o ra l l c h a r a c t e rwi d t h sa n dh e i g h t s( f i x e do rv a r i a b l ef o n t s ) .Ofc o u r s e , t h i sd o e sme a nt h a t t h e ywi l l b es l o we rt h a nr o u t i n e swr i t t e nt owo r kwi t has p e c i f i cg l y p hs i z e .Th i si sp a r t i c u l a r l yt r u ef o rt i l e ma p p e dt e x t , a n df o rt h a t r e a s o n ss p e c i f i c_w8h8a n d_w8h16v e r s i o n sa r ea v a i l a b l et h e r ea swe l l . Th ei n i t i a l i z e r st e n dt ob el o n ga n db o r i n g , s oIwo n ' twa s t et o omu c hs p a c eo nt h e mh e r e .Ba s i c a l l y , t h e yc l e a ro u tt h et e x tc o n t e x t , a s s i g ns o mes e n s i b l ev a l u e st ot h ema r g i n sa n ds u r f a c ev a r i a b l e s , s e tu pt h ef o n t , t h er e n d e r e ra n dt h ee r a s e r .Th e ya l s of i l l s o meo ft h e p a l e t t ea n dc o l o ra t t r i b u t e s . Th ec o d eI ' l l s h o wi nt h i sc h a p t e rwi l l mo s t l yb ea b o u tt h er e n d e r e r st h e ms e l v e s .Be l o wy o uc a ns e et h ec o d ef o rt h ed e f a u l ts c r e e n e n t r ywr i t e r , se_drawg_s(), a n dt h eo n es p e c i f i cf o r8 ×8f o n t s , se_drawg_w8h8 //! Character-plot for reg BGs, any sized, vertically tiled font. void se_drawg_s(uint gid) { int ix, iy; // (1) Get main variables. TTC *tc= tte_get_context(); TFont *font= tc->font; uint charW= (font->cellW+7)/8, charH= (font->cellH+7)/8; uint x0= tc->cursorX, y0= tc->cursorY; uint dstP= tc->dst.pitch/2; u16 *dstD= (u16*)(tc->data + (y0*dstP+x0)*2); // (2) Get the base tile index. u32 se= tc->cattr[TTE_SPECIAL] + gid*charW*charH; // (3) Loop over all tiles to draw glyph. for(ix=0; ix<charW; ix++) { for(iy=0; iy<charH; iy++) dstD[iy*dstP]= se++; dstD++; } } //! Character-plot for reg BGs using an 8x8 font. void se_drawg_w8h8(uint gid) { TTC *tc= tte_get_context(); file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 218/331 28-03-13 Tonc : GBA Programming in rot13 uint x0= tc->cursorX, y0= tc->cursorY; uint dstP= tc->dst.pitch/2; u16 *dstD= (u16*)(tc->data + (y0*dstP+x0)*2); dstD[0]= tc->cattr[TTE_SPECIAL] + gid; } Le t ' ss t a r twi t ht h es i mp l e ro n e : se_drawg_w8h8().An8 ×8g l y p ho naGBAt i l e ma ps i mp l yme a n swr i t eas i n g l es c r e e n e n t r yt ot h e r i g h tp l a c e .Th er i g h tp l a c eh e r ei sd e r i v e df r o mt h ec u r s o rp o s i t i o na n dt h es u r f a c ed a t a( tc->dst) .Th e‘ s p e c i a l ’c o l o ra t t r i b u t ei s u s e da samo d i f i e rt ot h eg l y p hi n d e xf o rt h i n g sl i k ep a l e t t es wa p p i n g . No t et h a tt h er o u t i n ej u s th a n d l e sp l o t t i n gt h eg l y p h .Tr a n s f o r mi n gf r o mASCI It og l y p hi n d e xa n dr e p o s i t i o n i n gt h ec u r s o ri sa l l d o n e e l s e wh e r e . Th emo r eg e n e r a l i z e dr o u t i n e , se_drawg_s()i sal i t t l emo r ec o mp l e x .I ts t i l l s t a r t sb yg e t t i n gap o i n t e rt ot h eg l y p h ' sd e s t i n a t i o n , dstD, a n dp i t c h( t h ed i s t a n c et ot h en e x tl i n e ) , dstP.All r e n d e r e r ss t a r twi t hs o me t h i n gl i k et h i s .Al l r e n d e r e r sa l s or e t r i e v et h e c h a r a c t e r ' swi d t ha n dh e i g h t–u n l e s st h es i z e sa r es p e c i f i e di na d v a n c e .Th en a me sIu s ef o rr e n d e r i n ga r ea l wa y st h es a me , s oy o u s h o u l db ea b l et ot e l l wh a tme a n swh a te v e nwh e nt h ef o r mu l a sf o ri n i t i a l i z i n gt h e mc a nb eat a di c k y . An y wa y , a f t e rg e t t i n gt h ep o i n t e ra n dp i t c h , t h et i l e i n d e xf o rt h et o p l e f to ft h eg l y p hi sc a l c u l a t e da n dp u tt h i si n t ose.Af t e rt h a t , wel o o po v e rt h ed i f f e r e n tt i l e so ft h eg l y p hi nb o t hd i r e c t i o n s .No t et h a tt h eo r d e ro ft h el o o pi sc o l u mn ma j o r , n o tr o wma j o r , b e c a u s e t h a t ' st h ewa yt h ed e f a u l tf o n t swe r eo r d e r e d . Asi th a p p e n s , c o l u mn ma j o rr e n d e r i n gt e n d st ob emo r ee f f i c i e n tf o rt e x t , b e c a u s eg l y p h sa r eu s u a l l yh i g h e rt h a nt h e ya r ewi d e . Al s o , f o rt i l e ma pt e x tcharWa n dcharHt e n dt ob es ma l l –o f t e n1o r2 .Th i sme a n st h a ti ti se x t r e me l yi n e f f i c i e n tt ou s el o o p s ;we ' l l s e eh o wi n e f f i c i e n ti ns u b s e c t i o n2 2 . 7 . 5 . .Un r o l l i n gt h e m, l i k ese_drawg_w8h8()a n dse_drawg_w8h16()d o , g i v e samu c h b e t t e rp e r f o r ma n c e . 22.3.2. Regular tilemap example void test_tte_se4() { irq_init(NULL); irq_add(II_VBLANK, NULL); REG_DISPCNT= DCNT_MODE0 | DCNT_BG0; // --- (1) Base TTE init for tilemaps --tte_init_se( 0, // Background number (BG 0) BG_CBB(0)|BG_SBB(31), // BG control (for REG_BGxCNT) 0, // Tile offset (special cattr) CLR_YELLOW, // Ink color 14, // BitUnpack offset (on-pixel = 15) NULL, // Default font (sys8) NULL); // Default renderer (se_drawg_s) // --- (2) Init some colors --pal_bg_bank[1][15]= CLR_RED; pal_bg_bank[2][15]= CLR_GREEN; pal_bg_bank[3][15]= CLR_BLUE; pal_bg_bank[4][15]= CLR_WHITE; pal_bg_bank[5][15]= CLR_MAG; pal_bg_bank[4][14]= CLR_GRAY; // --- (3) Print some text --// "Hello world in different colors" tte_write("\n Hello world! in yellow\n"); tte_write(" #{cx:0x1000}Hello world! in red\n"); tte_write(" #{cx:0x2000}Hello world! in green\n"); // Color use explained tte_set_pos(8, 64); tte_write("#{cx:0x0000}C#{cx:0x1000}o#{cx:0x2000}l"); tte_write("#{cx:0x3000}o#{cx:0x4000}r#{cx:0x5000}s"); tte_write("#{cx:0} provided by \\#{cx:#}."); // --- (4) Init for 8x16 font and print something --GRIT_CPY(&tile_mem[0][256], cyber16Glyphs); // Load tiles tte_set_font(&cyber16Font); // Attach font tte_set_special(0x4100); // Set special to tile 256, pal 4 file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 219/331 28-03-13 Tonc : GBA Programming in rot13 tte_set_drawg(se_drawg_w8h16); // Attach renderer tte_write("#{P:8,80}Also available in 8x16"); key_wait_till_hit(KEY_ANY); } Th ec o d ea b o v ed e mo n s t r a t e saf e wo ft h et h i n g sy o uc a nd owi t hTTEf o rt i l e ma p s .Th ec a l l t otte_init_se()i n i t i a l i z e st h e s y s t e mt od i s p l a yt e x to nBG0 , u s i n gc h a r b l o c k0a n ds c r e e n b l o c k3 1a n dt ou s et h ed e f a u l tf o n ta n dr e n d e r e r .Pa r a me t e rf i v ei st h eb i t u n p a c ko f f s e t ;b ys e t t i n gi tt o1 4 , a l l t h e1 v a l u e dp i x e l si nt h ef o n tmo v et o1 4 +1 =1 5 , t h el a s ti n d e xi nap a l e t t eb a n k .I ' ma l s os e t t i n ga f e wo t h e rc o l o r ss ot h a tt h ep a l e t t ewi l l l o o kl i k ef i g2 2 . 3 b . I ns t e p3 , Ip r i n ts o met e x twi t htte_write().Th ed i f f e r e n tc o l o r sa r ed o n eb yu s i n g#{cx:num}i nt h es t r i n g , wh i c hs e t st h e s p e c i a l c o l o r a t t r i b u t et onum.Mo r eo nt h e s ek i n d so fc o mma n d si ns e c t i o n2 2 . 7 . .Si n c et h eser e n d e r e r sa d dt h i sv a l u et ot h eg l y p h i n d e xf o rt h ef i n a l o u t p u t , i tc a nb eu s e df o rp a l e t t es wa p p i n g . St e p4d e mo n s t r a t e sh o wt ol o a du pa n du s eas e c o n df o n t .Th ecyber16Fonti sar e n d i t i o no ft h e8 ×1 6f o n tu s e di ny eo l d e SNESg a me , Cy b e r n a t o r( s e ef i g2 2 . 2 ) .Th i sf o n twa se x p o r t e da s4 b p pd a t as oIc a nj u s tc o p yi ti n t oVRAM d i r e c t l y , b u tId on e e dt o u s ea no f f s e tb e c a u s eIwa n tt ok e e pt h eo l df o n ta swe l l .Th ec h a r b l o c kn o wh a st wos e t so fg l y p h s( s e ef i g2 2 . 3 c ) . Fig 22.2: Cyb e r n a t o rf o n t : 8 ×1 6 . Fig 22.3b: Pa l e t t e . Fig 22.3a: test_tte_se4o u t pu t . Fig 22.3c: Ti l e s e t . I np r i n c i p l e , a l l In e e dt od ot ou s ead i f f e r e n tf o n ti st os e l e c ti twi t htte_set_font(), b u ts i n c et h et i l e sa r ea ta no f f s e t , Ia l s on e e d t oa d j u s tt h es p e c i a l c o l o ra t t r i b u t e .Th ev a l u eo f0 x 4 1 0 0i su s e dh e r et oa c c o u n tf o rt h eo f f s e t( 0 x 0 1 0 0 )a swe l l a st h ep a l e t t e b a n k ( 0 x 4 0 0 0 ) .I ' ma l s os e l e c t i n gad i f f e r e n tr e n d e r e rf o rt h eo c c a s i o n , a l t h o u g ht h a t ' smo s t l yf o rs h o wh e r eb e c a u s et h ed e f a u l tr e n d e r e rc a n h a n d l e8 ×1 6f o n t sj u s ta swe l l .Af t e rt h a t , Ij u s tc a l l tte_write()a g a i nt op r i n tan e ws t r i n gi nu s i n gt h en e wf o n t . 22.3.3. Affine tilemap text Te x tf o ra f f i n et i l e ma p swo r k sa l mo s tt h es a mea sf o rr e g u l a rt i l e ma p s ;y o uj u s th a v et or e me mb e rt h ed i f f e r e n c e sb e t we e nt h et wo k i n d so fb a c k g r o u n d s , l i k ema ps i z ea n da v a i l a b l eb i t d e p t h .Th ef u n c t i o n s ' p r o t o t y p e sa r et h es a me , e x c e p tt h a tsei sr e p l a c e db yase. I n t e r n a l l y , t h eo n l yr e a l d i f f e r e n c ei si nwh a tt h er e n d e r e r sa r et oo u t p u t , n a me l yb y t e si n s t e a do fh a l f wo r d s .An dh e r ewer u ni n t o t h a tq u a i n tl i t t l ef a c to fVRAM a g a i n : y o uc a n ' twr i t es i n g l eb y t e st oVRAM.Th i sme a n st h a tt h er e n d e r e r swi l l b eal i t t l emo r e c o mp l i c a t e d .Bu to n l yal i t t l e : s i mp l yc a l l ab y t e p l o t t i n gr o u t i n ef o rt h es c r e e n e n t r yp l a c e me n t .Be c a u s ea f f i n ema p sa r ee s s e n t i a l l y 8 b p pb i t ma ps u r f a c e s , Ic a nu s et h es t a n d a r dp l o t t e rf o r8 b p pb i t ma ps u r f a c e s : _sbmp8_plot().As i d ef r o mt h i so n ed i f f e r e n c e , t h e ase_r e n d e r e r sa r ei d e n t i c a l t ot h ese_c o u n t e r p a r t s . //! Character-plot for affine BGs using an 8x8 font. void ase_drawg_w8h8(uint gid) { TTC *tc= tte_get_context(); u8 se= tc->cattr[TTE_SPECIAL] + gid; _sbmp8_plot(&tc->dst, tc->cursorX/8, tc->cursorY/8, se); } //! Character-plot for affine BGs, any sized, vertically oriented font. void ase_drawg_s(int gid) { TTC *tc= tte_get_context(); TFont *font= tc->font; uint charW= (font->cellW+7)/8, charH= (font->cellH+7)/8; file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 220/331 28-03-13 Tonc : GBA Programming in rot13 uint x0= tc->cursorX/8, y0= tc->cursorY/8; u8 se= tc->cattr[TTE_SPECIAL] + gid*charW*charH; int ix, iy; for(ix=0; ix<charW; ix++) for(iy=0; iy<charH; iy++, se++) _sbmp8_plot(&tc->dst, ix+x0, iy+y0, se); } Th ed e mof o ra f f i n ema pt e x ti stext_tte_ase().Th ei d e ai ss i mp l eh e r e : s e tu pt h et e x tf o ra2 5 6 ×2 5 6p i x e l ma p , wr i t es o met e x t o n t oi ta n dr o t a t et h eb a c k g r o u n dt oi l l u s t r a t et h a ti ti si n d e e da na f f i n eb a c k g r o u n d .Th ec e n t e ro fr o t a t i o ni st h e“ o ”a tt h ec e n t e ro ft h e s c r e e n .Top l a c ei tt h e r e , I ' v eu s e dt h e#{P:x,y}c o d e ;t h i ss e t st h ec u r s o rt ot h ea b s o l u t ep o s i t i o ng i v e nb y( x , y ) .Th eo t h e rs t r i n gi s a l s op o s i t i o n e do nt h ema pi nt h i sma n n e r . void test_tte_ase() { irq_init(NULL); irq_add(II_VBLANK, NULL); REG_DISPCNT= DCNT_MODE1 | DCNT_BG2; // Init affine text for 32x32t bg tte_init_ase( 2, // BG number BG_CBB(0) | BG_SBB(28) | BG_AFF_32x32, // BG control 0, // Tile offset (special cattr) CLR_YELLOW, // Ink color 0xFE, // BUP offset (on-pixel = 255) NULL, // Default font (sys8) NULL); // Default renderer (ase_drawg_s) // Write something tte_write("#{P:120,80}o"); tte_write("#{P:72,104}Round, round, #{P:80,112}round we go"); // Rotate it AFF_SRC_EX asx= { 124<<8, 84<<8, 120, 80, 0x100, 0x100, 0 }; bg_rotscale_ex(®_BG_AFFINE[2], &asx); while(1) { VBlankIntrWait(); key_poll(); asx.alpha += 0x111; bg_rotscale_ex(®_BG_AFFINE[2], &asx); if(key_hit(KEY_START)) break; } } Fig 22.4: test_tte_ase. 22.4. Bitmapped text Bi t ma pt e x tr e n d e r i n gi sal i t t l ed i f f e r e n tf r o mma pt e x ta n dc a nr a n g ei nd i f f i c u l t yf r o me a s yt oi n s a n e , d e p e n d i n go ny o u rwi s h e s .Ati t s c o r e , t h o u g h , i t ' sa l wa y st h es a mep r o c e s s : l o o po v e ra l l p i x e l sa n dd r a wt h e mo nt h ed e s t i n a t i o ns u r f a c e .Fo re x a mp l e , ag e n e r i cg l y p h r e n d e r e rt h a td r a wsp i x e l st r a n s p a r e n t l yc o u l dl o o ks o me t h i n gl i k et h i s . // Pseudo code for a general glyph printer. file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 221/331 28-03-13 Tonc : GBA Programming in rot13 void foo_drawg(uint gid) { TTC *tc= tte_get_context(); TFont *font= tc->font; // Drawing with color keying. // Loop over all pixels. If the glyph's pixel is not zero, draw it. // Other wise, nevermind. for(iy=0; iy<tte_get_glyph_height(gid); iy++) { for(ix=0; ix<tte_get_glyph_width(gid); ix++) { u16 color= font_get_pixel(font, gid, ix, iy); if(color != 0) foo_plot(&tc->dst, tc->cursorX+ix, tc->cursorY+iy, color); } } } He r e , fooc a nme a na n yr e n d e r i n gf a mi l y .foo_plot()i sag e n e r a l p i x e l p l o t t e ra n dfont_get_pixel()ap i x e l r e t r i e v e r .Th e i mp l e me n t a t i o n so ft h o s ef u n c t i o n sd e p e n do nt h es p e c i f i c so ft h ef o n ta n ds u r f a c e , b u tt h eg l y p hr e n d e r e rd o e s n ' tn e e dt ok n o wa b o u t t h a t . 22.4.1. Basic bmp16 to bmp16 glyph printer Th i sn e x tf u n c t i o ni sa ne x a mp l eo fa1 6 b p pf o n tt o1 6 b p pb i t ma pp r i n t e r . //! Glyph renderer from bmp16 glyph to bmp16 destination. void bmp16_drawg(uint gid) { // (1a) Basic variables TTC *tc= tte_get_context(); TFont *font= tc->font; u16 *srcD= (u16*)(font->data+gid*font->cellSize), *srcL= srcD; uint charW= tte_get_glyph_width(gid) uint charH= tte_get_glyph_height(gid); uint x0= tc->cursorX, y0= tc->cursorY; uint srcP= font->cellW, dstP= tc->dst.pitch/2; u16 *dstD= (u16*)(tc->dst.data + (y0*dstP + x0)*2); // (2) The actual rendering uint ix, iy; for(iy=0; iy<charH; iy++) { for(ix=0; ix<charW; ix++) if(srcD[ix] != 0) dstD[ix]= srcD[ix]; srcD += srcP; dstD += dstP; } } Bl o c k s1 aa n d1 bs e tu pt h ema i nv a r i a b l e st ou s ei nt h el o o p .Th emo s ti mp o r t a n ta r et h es o u r c ea n dd e s t i n a t i o np o i n t e r s , srcDa n d dstD, a n dt h e i rp i t c h e s , srcPa n ddstP.No t i c et h a tt h es o u r c ep i t c h , srcP, i st h en o tt h ec h a r a c t e rwi d t h , b u tt h ec e l l wi d t h , b e c a u s e t h ef o n t sa r eo r g a n i z e do nac e l l g r i d .Th ec o d ea tp o i n t2s e l e c t i v e l yc o p i e sp i x e l sf r o mt h ef o n tt ot h es u r f a c e . Intermezzo : considerations for performance Yo uma ywo n d e rwh ybmp16_drawg()d o e s n ' tf o l l o wt h ep a t t e r ni nt h ee a r l i e rfoo_drawg()mo r ec l o s e l y .Th ea n s we ri s , o f c o u r s e , p e r f o r ma n c e .An db e f o r ea n y o n eq u o t e sKn u t ho nme : n o te v e r ye f f o r tt oma k ey o u rc o d ef a s ti sp r e ma t u r eo p t i mi z a t i o n .Wh e n y o uc a ni mp r o v et h es p e e do ft h ec o d ewi t h o u ts p e n d i n gt o omu c he f f o r to fal o s so fr e a d a b i l i t y , t h e r e ' sn o tmu c hr e a s o nn o tt o . I nt h i sc a s e , t h eo p t i mi z a t i o n sI ' v ea p p l i e dh e r ef a l l i n t ot woc a t e g o r i e s : l o c a l v a r i a b l e sa n dp o i n t e ra r i t h me t i c .Th e s et e c h n i q u e s–t h a t e v e r yCp r o g r a mme rshould k n o w–ma n a g e dt ob o o s tt h es p e e db yaf a c t o ro f5 . Le t ' ss t a r twi t hp o i n t e r s .I ' mu s i n gp o i n t e rt omya d v a n t a g ei nt wop l a c e sh e r e .Fi r s t , i n s t e a do fu s i n gt h ef o n t d a t ap o i n t e ra n d d e s t i n a t i o np o i n t e rd i r e c t l y , Ic r e a t ep o i n t e r ssrcDa n ddstDa n dd i r e c tt h e mt h et o p l e f t so ft h eg l y p ha n dwh e r ei twi l l b er e n d e r e dt o . Sh o r t c i r c u i t i n gt h ea c c e s s e sl i k et h i sme a n st h a tId o n ' th a v et oa p p l ye x t r ao f f s e t st og e tt owh e r eIwa n tt og oi nt h el o o p .Th i swi l l b e b o t hf a s t e ra n di nf a c tmo r er e a d a b l ea swe l l , b e c a u s et h el o o p swo n ' tc o n t a i na n yn o n e s s e n t i a l e x p r e s s i o n s . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 222/331 28-03-13 Tonc : GBA Programming in rot13 //# Example of a more standard bitmap copier. for(iy=0; iy < charH; iy++) for(ix=0; ix < charW; ix++) dstD[iy*dstP + ix]= srcD[iy*srcP+ix]; As e c o n dp o i n th e r ei su s i n gi n c r e me n t a l o f f s e t si n s t e a do ft h ey*pitch+xf o r m( s e ea b o v e ) .Is u p p o s et h i si smo s t l yama t t e ro f p r e f e r e n c e , b u ta v o i d i n gt h ewh o l l yu n n e c e s s a r ymu l t i p l i c a t i o n sd o e sma t t e r . Th es e c o n do p t i mi z a t i o ni sl o c a l v a r i a b l e s .Byt h i sIme a nl o a d i n gv a r i a b l e st h a tr e s i d ei nme mo r y( g l o b a l sa n ds t r u c tme mb e r s )o ro f t u s e df u n c t i o nr e s u l t si nl o c a l t e mp o r a r i e s .Th i sma ys e e ml i k eas i l l yt h i n gt op o i n to u t , b u tt h ea mo u n to ft i mey o uc a ns a v ewi t ht h i si s a c t u a l l yq u i t eh i g h . Co n s i d e rt h eu s eo ftte_get_glyph_width()h e r e .Iknow t h ewi d t ho fag l y p hwo n ' tc h a n g ed u r i n gt h el o o p , s oc a l l i n ga f u n c t i o nt og e tt h ewi d t hi nt h el o o p c o n d i t i o ni t s e l fi sj u s ts t u p i d .An o t h e re x a mp l eo ft h i swo u l db ec a l l i n gstrlen()wh e nl o o p i n g o v e ra l l c h a r a c t e r si nas t r i n g .Fo rt h o s ewh od ot h i s : NO!Ba dp r o g r a mme r , b a d !Sa v et h ev a l u ei nal o c a l v a r i a b l ea n du s et h a ti n s t e a d . Th eo t h e rp o i n ti st op r e l o a dt h i n g sl i k eg l o b a l sa n ds t r u c t / c l a s sme mb e r si fy o uu s et h e mmo r et h a no n c e .Co n s i d e rt h ef o l l o wi n g c o d e .I t ' st h es a mea st h eo n eg i v e nb e f o r e , o n l yn o wIh a v en o tl o a d e dc h a r a c t e rh e i g h ta n dt h ep i t c h e si n t ol o c a l t e mp o r a r i e s . //# Another bitmap-copy example. DO NOT USE THIS !!! for(iy=0; iy < font->charH; iy++) for(ix=0; ix < charW; ix++) dstD[iy*tc->dst.pitch/2 + ix]= srcD[iy*font->cellW + ix]; Th er e s u l t : t h es p e e do ft h ef u n c t i o nwa shalved!Ie x p e c t e di tt ob es l o we r , b u tt h a tt h i si n n o c u o u s l o o k i n gmo d i f i c a t i o nwo u l da c t u a l l y c o s tmeaf a c t o rt wowa sq u i t eas u r p r i s et ome . Soy e s , s p a my o u rc o d ewi d t hl o c a l sf o rl o o p i n v a r i a n t , me mo r y b a s e dq u a n t i t i e s .Th i sa v o i d st h e mb e i n gl o a d e df r o mme mo r ye v e r y t i me .Asab o n u s , t h el o o p st h e ms e l v e swi nc o n t a i nl e s st e x ta n db emo r eg e n e r a l i z e d , ma k i n gi tmo r er e u s a b l e . Bo t ht h ep o i n t e rwo r ka n dp r e l o a d i n gv a r i a b l e sa r ea c t u a l l yt h ej o bo ft h ec o mp i l e r ' so p t i mi z e r , b u tt h ec u r r e n tv e r s i o no fGCCd o e s n ' t d ot h e s ev e r ywe l l o ra ta l l .Al s o , s o me t i me si tcan't d ot h i so p t i mi z a t i o n .Wh e nf u n c t i o n sa r ec a l l e db e t we e nme mo r yd e r e f e r e n c e s , t h e c o mp i l e rh a st or e l o a dt h ed a t ab e c a u s et h o s ef u n c t i o n sma yh a v ec h a n g e dt h e i rc o n t e n t s .Ob v i o u s l y , t h i swo u l d n ' th a p p e nf o rl o c a l s . Use local variables for struct members and globals St r u c tme mb e r sa n dg l o b a l v a r i a b l e sl i v ei nme mo r y , n o tCPUr e g i s t e r s .Be f o r et h eCPUc a nu s et h e i rd a t a , t h e yh a v et ob e l o a d e df r o mme mo r yi n t or e g i s t e r sf i r s t , a n dt h i so f t e nh a p p e nmo r et i me st h e nn e c e s s a r y .Si n c eme mo r ya c c e s s( e s p e c i a l l y ROM a c c e s s )i ss l o we rt h a nr e g i s t e ra c c e s s , t h i sc a nr e a l l yb o gd o wna na l g o r i t h mi ft h et h i n gi su s e dmo r et h a no n c e .Yo u c a na v o i dt h i sn e e d l e s swo r kb yc r e a t i n gl o c a l v a r i a b l e sf o rt h e m. As i d ef r o mt h es p e e d b o o s t , l o c a l v a r i a b l e sc a nu s es h o r t e rn a me s , r e s u l t i n gi ns h o r t e ra n dmo r er e a d a b l ec o d ei nt h e p a r t st h a ta c t u a l l yd ot h ewo r k .I t ' swi n f r e a k i n ' wi n , b a b y . 22.4.2. Glyph and surface formats Th er e n d e r e rd e s c r i b e da b o v ea s s u me st h a tt h eg l y p h sa r ei nf o r ma t t e da s1 6 b p pb i t ma p s .Ho we v e r , TTE' sd e f a u l tf o n t sa r ei na1 b p p t i l e s t r i pf o r ma t , s oI ' l l h a v et ou s es o me t h i n ge l s e .Be f o r eIg oi n t ot h ed e t a i l so ft h a tf u n c t i o n , I ' dl i k et od i s c u s st h ed i f f e r e n tg l y p h f o r ma t sa n dwh yI ' mu s i n gt i l e s t r i p si n s t e a do fj u s tp l a i nb i t ma p s . Wh e nIs a yg l y p hf o r ma t s , wh a tIr e a l l yme a ni st h eo r d e ri nwh i c hp i x e l sa r ea c c e s s e d .Th r e ek e yv a r i a t i o n se x i s t . Linear o rbitmap l a y o u t .Th i si sas i mp l e , r o wma j o rma t r i x .Th i sg i v e sy o ut wol o o p s ;o n ef o ry a n do n ef o rx. Tiled.I np a r t i c u l a r : 8 ×8t i l e d .Th i si st h es t a n d a r dGBAt i l ef o r ma twh e r ee a c hg r o u po f8 ×8p i x e l sf o r mar o wma j o rma t r i x , a n dt h e nt h et i l e st h e ms e l v e sa r ep a r to fal a r g e rr o wma j o rma t r i xa g a i n .Go i n gt h r o u g ht h i sr e q u i r e dfour l o o p s : t wof o re a c h ma t r i x . Tile-strips.Th i sa l s ou s e s8 ×8t i l e s , b u tt h i st i met h et i l e so r d e r e di nac o l u mn ma j o ro r d e r .I no t h e rwo r d s , t i l e1c o me sb e l o w t i l e0i n s t e a do ft ot h er i g h t .Th i sh a st h er a t h e rn i c ep r o p e r t yt h a tt h er o wsi ns u c c e s s i v et i l e sa r ec o n s e c u t i v e .I te l i mi n a t e st h e b r e a ki nt h ey d i r e c t i o n , r e s u l t i n gi no n l y3l o o p sa n dh a ss i mp l e rc o d et ob o o t . Fi g2 2 . 5s h o wst h e s et h r e el a y o u t s , i n c l u d i n gt h el o o ps t r u c t u r ea n dt h eo r d e ri nwh i c ht h ep i x e l sa r et r a v e r s e df o r1 b p pf o n t s .Th ec a s e i sal i t t l eb i td i f f e r e n tb e c a u s eo ft h eb i t p a c k i n g : 1b p pme a n s8p i x e l sp e rb y t e .Asar e s u l t , t h eb i t ma p xl o o p sh a v et ob eb r o k e nu pi n t o g r o u p so f8 , s ot h a tt h eb i t ma pf o r ma tn o wu s e st h r e en e s t so fl o o p si n s t e a do fj u s tt wo .Th e r ei sn od i f f e r e n c ef o rt h et i l e df o r ma t s , a s t h o s ea r eg r o u p e db y8p i x e l sa n y wa y .No to n l yt h a t , i fy o uwe r et oc a l c u l a t et h et o t a l l o o p o v e r h e a df o rc o mmo n l yu s e dg l y p hs i z e si t t u r n so u tt h a tt h i sa r r a n g e me n ta c t u a l l ywo r k sp a r t i c u l a r l ywe l l f o rt i l e s t r i p s .Th i si sl e f ta sa ne x e r c i s ef o rt h er e a d e r( h i n t : c o u n tt h e n u mb e ro fc o mp a r i s o n s ) . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 223/331 28-03-13 Tonc : GBA Programming in rot13 Fig 22.5: Pi x e l t r a v e r s a l i ng l yph sf o r1 b ppb i t ma p, t i l ea n dt i l e s t r i pf o r ma t s . Th en u mb e r si n d i c a t et h el o o psa n dt h e i r n e s t i n g s . 22.4.3. bmp16_drawg_b1cts : 1- to 16-bpp with transparency and coloring Th en e x tr o u t i n et a k e s1 b p pt i l e s t r i pg l y p h sa n dt u r n st h e mi n t oo u t p u ts u i t a b l ef o r1 6 b p pb i t ma pb a c k g r o u n d s .Th eo u t p u twi l l u s et h e i n ka t t r i b u t ef o rc o l o ra n di twi l l o n l yd r a wap i x e l i ft h eb i twa s1i nt h es o u r c ed a t a , g i v i n gu st r a n s p a r e n c y .Th et h r e ema c r o sa tt h et o p d e c l a r ea n dd e f i n et h eb a s i cv a r i a b l e s , c o mp a r a b l et os t e p1 ai nbmp16_drawg(). void bmp16_drawg_b1cts(uint gid) { // (1) Basic variables TTE_BASE_VARS(tc, font); TTE_CHAR_VARS(font, gid, u8, srcD, srcL, charW, charH); TTE_DST_VARS(tc, u16, dstD, dstL, dstP, x0, y0); uint srcP= font->cellH; dstD += x0; u32 ink= tc->cattr[TTE_INK], raw; // (2) Rendering loops. uint ix, iy, iw; for(iw=0; iw<charW; iw += 8) // loop over tile-strips { dstL= &dstD[iw]; for(iy=0; iy<charH; iy++) // loop over tile-lines { raw= srcL[iy]; for(ix=0; raw>0; raw>>=1, ix++) // loop over tile-scanline (8 pixels) if(raw&1) dstL[ix]= ink; dstL += dstP/2; } srcL += srcP; } } Th er o u t i n es t a r t sb yc a l l st ot h r e ema c r o s : TTE_CHAR_VARS(), TTE_CHAR_VARSa n dTTE_DST_VARS().Th e s ed e c l a r ea n d d e f i n emo s to ft h er e l e v a n tl o c a l v a r i a b l e s , s i mi l a rt os t e p1 ai nbmp16_drawg().No t et h a tt woo ft h ea r g u me n t sh e r ea r ed a t a t y p e i d e n t i f i e r sf o rt h es o u r c ea n dd e s t i n a t i o np o i n t e r s , r e s p e c t i v e l y .srcDa n dsrcLwi l l i n i t i a l l yp o i n tt ot h es t a r to ft h es o u r c ed a t a .Th e o t h e rp o i n t e r s , dstDa n ddstL, p o i n tt ot h es t a r tof the scanline i nt h ed e s t i n a t i o na r e a .Th e yh a v e n ' tb e e nc o r r e c t e df o rt h exp o s i t i o n j u s ty e t ;t h a t ' sd o n er i g h ta f t e ri t . Th er e a s o nI ' mu s i n gt wop a i r so fp o i n t e r sh e r e( ama i ndata p o i n t e r , fooDa n daline p o i n t e rfooL)i sb e c a u s eo ft h ep o i n t e r a r i t h me t i c .Th ed a t a p o i n t e rs t a y sf i x e da n dt h el i n e p o i n t e rmo v e sa r o u n di nt h ei n n e rl o o p . Th et i l e s t r i pp o r t i o no ff i g2 2 . 5i l l u s t r a t e sh o wt h er o u t i n emo v e so v e ra l l t h ep i x e l s .Be c a u s ei t ' sf o ra1 b p pb i t p a c k e df o n ta n d b e c a u s et h e r ea r e8p i x e l sp e rt i l e l i n e , wec a ng e ta ne n t i r el i n e ' swo r t ho fp i x e l si no n eb y t e r e a d .Re n d e r i n gt r a n s p a r e n t l yg i v e su sa n i c ec h a n c ef o ro p t i mi z a t i o na swe l l : i ft h et i l e l i n ei se mp t y( i . e . , raw==0 ) , weh a v en omo r ev i s i b l ep i x e l si nt h a tl i n ea n dwec a nmo v e o nt ot h en e x t .Ag l a n c ea tt h ev e r d a n a9f o n ti nf i g2 2 . 1 , wi l l t e l l y o ut h a ty o uma yb ea b l et os k i p5 0 %o ft h ep i x e l sb e c a u s eo ft h i s . 22.4.4. bmp8_drawg_b1cts : 1 to 8 bpp with transparency and coloring Th e8b p pc o u n t e r p a r to ft h ep r e v i o u sf u n c t i o ni sc a l l e dbmp8_drawg_b1cts(), a n di sg i v e nb e l o w.Th ec o d ei sv e r ys i mi l a rt ot h e 1 6b p pf u n c t i o n , b u tb e c a u s et h ep i x e l sa r en o wb y t e st h e r ea r eaf e wd i f f e r e n c e si nt h ed e t a i l s . void bmp8_drawg_b1cts(uint gid) file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 224/331 28-03-13 Tonc : GBA Programming in rot13 { // (1) Basic variables TTE_BASE_VARS(tc, font); TTE_CHAR_VARS(font, gid, u8, srcD, srcL, charW, charH); TTE_DST_VARS(tc, u16, dstD, dstL, dstP, x0, y0); uint srcP= font->cellH; dstD += x0/2; u32 ink= tc->cattr[TTE_INK], raw, px; uint odd= x0&1; // (2) Source offset. uint ix, iy, iw; for(iw=0; iw<charW; iw += 8) // Loop over strips. { dstL= &dstD[iw/2]; for(iy=0; iy<charH; iy++) // Loop over lines. { raw= srcL[iy]<<odd; // (3) Apply source offset. for(ix=0; raw>0; raw>>=2, ix++) // Loop over pixels. { // (4) 2-bit -> 2-byte unpack, then used as masks. px= ( (raw&3)<<7 | (raw&3) ) &~ 0xFE; dstL[ix]= (dstL[ix]&~(px*255)) + ink*px; } dstL += dstP/2; } srcL += srcP; } } Th eo n l yr e a l d i f f e r e n c ewi t hbmp16_drawg_b1ctsi si nt h ei n n e r mo s tl o o p .Th en o b y t e wr i t ei s s u ef o rVRAM me a n st h a twe n e e dt owr i t et wop i x e l si no n ep a s s .Tod ot h i s , Ir e t r i e v ea n du n p a c kt wob i t si n t ot wob y t e sa n du s et h e mt oc r e a t et h en e wp i x e l sa n d t h ep i x e l ma s k s .Th ef i r s tl i n ei nt h ei n n e rl o o pd o e st h eu n p a c k i n g .I tt r a n s f o r mst h eb i t p a t t e r nabi n t o0000 000a 0000 000b. Bo t hb y t e si nt h i sh a l f wo r da r en o w0o r1 , d e p e n d i n go nwh e t h e ra a n db we r eo no ro f f .Bymu l t i p l y i n gwi t hinka n d2 5 5 , y o uc a ng e t t h ec o l o r e dp i x e l sa n dt h ea p p r o p r i a t ema s kf o ri n s e r t i o n . # 2-bit to 2-byte unpacking. 0000 0000 hgfe dcba p = raw (start) 0000 0000 0000 00ba p &= 3 0000 000b a000 00ba p |= p<<7; 0000 000b 0000 000a p &= ~0xFE; Pr e p a r i n gt h er i g h th a l f wo r di so n l yp a r to ft h ewo r k .I fcursorX( i . e . , x0)i so d d , t h e nt h eg l y p hs h o u l db ep l o t t e dt oa no d ds t a r t i n g l o c a t i o na swe l l .Ho we v e r , t h ed e s t i n a t i o np o i n t e rdstLi sh a l f wo r dp o i n t e ra n dt h e s emu s ta l wa y sb eh a l f wo r da l i g n e d .Tot a k ec a r eo f t h i s , n o t et h a tu n p a c k i n gt h ep a t t e r n‘ abcd efgh’t oa no d db o u n d a r yi se q u i v a l e n tt ou n p a c k i n g‘ a bcde fgh0’t oa ne v e n b o u n d a r y .Th i si se x a c t l ywh a tt h ee x t r as h i f tb yoddi sf o r . 22.4.5. Example : sub-pixel rendering Fo rt h ed e moo ft h i ss e c t i o n , I ' dl i k et ou s eat e c h n i q u ec a l l e dsub-pixel rendering.Th i si same t h o df o re f f e c t i v e l yt r i p l i n gt h eh o r i z o n t a l r e s o l u t i o nf o rr e n d e r i n gb y‘ b o r r o wi n g ’c o l o r sf r o mo t h e rp i x e l s . Co n s i d e rt h el e t t e r‘ A’a ss h o wni nf i g2 2 . 6 a .Asy o uk n o w, e a c hp i x e l i sc o mp o s e do ft h r e ec o l o r s : r e d , g r e e na n db l u e .Th e s ea r e t h es u b p i x e l s .Th el e t t e ro nt h es u b p i x e l g r i dl o o k sl i k ef i g2 2 . 6 b .No t i c eh o wt h ec o l o r sa r es t i l l g r o u p e db yp i x e l s , wh i c ho nt h es u b p i x e l g r i dg i v e sv e r yj a g g e de d g e s .Th et r i c kt os u b p i x e l r e n d e r i n gi st os h i f tg r o u p so fs u b p i x e l sl e f to rr i g h t , r e s u l t i n gi ns mo o t h e r e d g e s( f i g2 2 . 6 c ) .No wc o mb i n et h ep i x e l st oRGBc o l o r sa g a i nt og e tf i g2 2 . 6 d .Zo o me di na si ti si nf i g2 2 . 6 , s u b p i x e l r e n d e r i n gma y n o tl o o kl i k emu c h , b u twh e nu s e di nt h ep r o p e rs i z et h ee f f e c t sc a nb eq u i t es t u n n i n g . Fig 22.6: S u b pi x e l r e n d e r i n g . a: ‘ A’o n8 ×8g r i d . b: a sl e f t , i nR, G, Bs u b g r i d . c: s h i f t i n gr o wst od i s t r i b u t es u b pi x e l s e v e n l y. d: n e wc o l o rd i s t r i b u t i o ni npi x e l s( b l a c k / wh i t ei n v e r t e d ) . Su b p i x e l r e n d e r i n gi s n ' tu s e f u l f o re v e r y t h i n g .Be c a u s ei tmu d d l e st h ec o n c e p to fp i x e l a n dc o l o ral i t t l e , i t ' so n l yu s e f u l f o rg r a y s c a l e file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 225/331 28-03-13 Tonc : GBA Programming in rot13 i ma g e s .Th i sd o e sma k ei tg r e a tf o rt e x t , o fc o u r s e .Se c o n d l y , t h eo r d e ri nwh i c ht h es u b p i x e l sa r eo r d e r e da l s oma t t e r s .Th ep r o c e s s s h o wni nfig 22.6 wi l l wo r kf o rRGBo r d e r e ds c r e e n s , b u two u l df a i l q u i t es p e c t a c u l a r l ywh e nt h ep i x e l sa r eBGRo r d e r e d .Go i n gi n t o a l l t h eg r i t t yd e t a i l si tt o omu c ht od oh e r e , s oI ' l l r e f e ry o ut oh t t p : / / www. g r c . c o m/ c t wh a t . h t m, wh i c he x p l a i n st h ec o n c e p ti nmo r ed e t a i l a n dg i v e saf e we x a mp l e st o o . J a n o S( h t t p : / / www. h a l u z . o r g / y e s h / )h a sc r e a t e dn i c e4 ×8s u b p i x e l f o n tf o ru s eo nGBAa n dNDS( s e e f i g2 2 . 7 ) .Awi d t ho f4i sr e a l l yt i n y ;i t ' si mp o s s i b l et oh a v eg l y p h so ft h a ts i z ewi t hn o r ma l r e n d e r i n ga n ds t i l l h a v er e a d a b l et e x t .Wi t hs u b p i x e l r e n d e r i n g , h o we v e r , i ts t i l l l o o k sg o o da n dn o wy o uc a nh a v ema n ymo r e c h a r a c t e r so nt h es c r e e nt h a nu s u a l . Th eo u t p u to ft h ed e moc a nb es e e ni nf i g2 2 . 8 .Be c a u s es u b p i x e l r e n d e r i n gi ss oc l o s e l yt i e dt ot h e h a r d wa r ey o u ' r ev i e wi n gwi t h , i twi l l p r o b a b l yl o o kc r u mmyo nmo s ts c r e e n so rp a p e r .Yo ur e a l l yh a v et os e ei t Fig 22.7: 4×8s ub pi x e l o naGBAs c r e e nf o rt h ef u l l e f f e c t . f o n t I nt h i sp a r t i c u l a rc a s e , I ' v ec o n v e r t e dt h ef o n tt owo r kwi t hbmp16_drawg(): a1 6 b p pf o n ti nb i t ma p l a y o u t .Cr e a t i n ga n8 b i tv e r s i o nwo u l dn o tb ev e r yh a r de i t h e r .A1 b p pb i t p a c k e df o n two u l do fc o u r s eb e i mp o s s i b l eb e c a u s et h ef o n th a smo r et h a nt woc o l o r s .Toma k es u b p i x e l f o n t sl o o kr i g h t , y o u ' l l a c t u a l l yn e e dal o to fc o l o r s : o n ef o r e a c hc o mb i n a t i o no fR, G, B, a n dwi t hd i f f e r e n c es h a d e so fe a c h .Th a ts a i d , J a n o Sh a sma n a g e dt or e d u c et h ea mo u n to fc o l o r st o2 0 h e r ewi t h o u tt o omu c hl o s si nq u a l i t y .I fa n y o n ewa n t si t , Ia l s oh a v ea1 5 c o l o rv e r s i o nt ou s ewi t h4 b p pf o n t s . //! Testing a bitmap renderer with JanoS' sub-pixel font. void test_tte_bmp16() { irq_init(NULL); irq_add(II_VBLANK, NULL); REG_DISPCNT= DCNT_MODE3 | DCNT_BG2; tte_init_bmp(3, &yesh1Font, bmp16_drawg); tte_init_con(); const char *str= "http://en.wikipedia.org/wiki/Subpixel_rendering :\n" "Subpixel rendering is a way to increase the " "apparent \nresolution of a computer's liquid crystal " "display (LCD).\nIt takes advantage of the fact that " "each pixel on a color\nLCD is actually composed of " "individual red, green, and\nblue subpixel stripes to " "anti-alias text with greater\ndetail.\n\n" " 4x8 sub-pixel font by JanoS.\n" " http://www.haluz.org/yesh/\n"; tte_write(str); key_wait_till_hit(KEY_ANY); } Fig 22.8: S u b pi x e l r e n d e r i n gd e mo 22.5. Object text Ob j e c tt e x ti su s e f u l i fy o uwa n tt h ec h a r a c t e r st omo v ea r o u n dab i t , o ri fy o us i mp l yd o n ' th a v ea n yr o o mo nab a c k g r o u n d .Th e r ea r ea f e wp o s s i b i l i t i e sf o ro b j e c tt e x t .Th emo s to b v i o u so n ei st ol o a da l l t h ec h a r a c t e r si n t oo b j e c tVRAM a n ds e tt h et i l e i n d i c e so ft h e o b j e c t st ou s et h er i g h tt i l e s .Th i si swh a tt h eTTEo b j e c ts y s t e mu s e s . I nma n ywa y s , t h i sk i n do fo b j e c tt e x ti ss i mi l a rt ot i l e ma pt e x t .Th et i l e sa r el o a d e du pf r o n ta n dy o uc h a n g et h er e l e v a n tma p p i n g e n t r i e s( i nt h i sc a s eattr2o ft h eo b j e c t s )t ot h er i g h tn u mb e r .Ofc o u r s e , t h e r ea r es o men o t a b l ed i f f e r e n c e sa swe l l . Fo ro n et h i n g , t h ep o s i t i o n so ft h ec h a r a c t e r smu s tb ewr i t t e nt ot h eo b j e c t s .Bu tn o to n l yt h a t , t h eo b j e c t sa l s on e e dt ok n o wh o wb i g t h e y ' r es u p p o s e dt ob e , a n dwh e t h e rt h e yh a v ea n yo t h e ri n t e r e s t i n gq u a l i t i e sl i k er o t a t i o na n dp a l e t t e s .Fo rt h a tr e a s o n , I ' v ec h o s e nt o u s et h ec o l o ra t t r i b u t e s0 , 1a n d2t os t o r et h eo b j e c ta t t r i b u t e s0 , 1a n d2 . An o t h e rp r o b l e mi swh i c ho b j e c t st ou s ea n dh o wma n y .Th i sl a s to n ec o u l dp r e s e n tab i gp r o b l e m, a c t u a l l y , b e c a u s ey o uma ya l s o wa n tt ou s eo b j e c t sf o rn o r ma l s p r i t e sa n di two u l db ear e a l l yb a di d e ai ft h e ywe r es u d d e n l yo v e r r i d d e nb yt h et e x ts y s t e m. Fo rt h el a t t e ri s s u e , Iu s e( o rp e r h a p sa b u s e )t h edstme mb e ro ft h ec o n t e x t .Ea c hg l y p hi sr e p r e s e n t e db ya no b j e c t , s oI ' l l n e e da n file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 226/331 28-03-13 Tonc : GBA Programming in rot13 o b j e c ta r r a y , b u tI ' md o i n gi twi t hal i t t l et wi s t .I ' mg o i n gt os t a r ta tt h eend o ft h ea r r a y , s ot h a tt h el o we ro b j e c t sc a ns t i l l b eu s e df o r s p r i t e sa sn o r ma l .Es s e n t i a l l y , I ' mu s i n gOAM a sa ne mp t y d e s c e n d i n gs t a c k .I nt h i sa r r a n g e me n t , dst.datap o i n t st ot h et o po ft h e s t a c k( i . e . , t h el a s te l e me n ti nt h ea r r a y ) , dst.pitchi st h ei n d e xt ot h ec u r r e n to b j e c t , a n ddst.widthi st h el e n g t ho ft h es t a c k . Th ed e f a u l tp l o t t e ro fo b j e c t si sobj_drawg.Re me mb e r , dst.pitchi su s e da sa ni n d e xh e r ea n ddst.datai st h et o po ft h e s t a c k , s oanegative i n d e xi su s e dt og e tt h ec u r r e n to b j e c t .Af t e rt h a t , t h ec o o r d i n a t e sa n dt h ec o r r e c tg l y p hi n d e xa r eme r g e dwi t ht h e c o l o ra t t r i b u t e st oc r e a t et h ef i n a l o b j e c t . //! Glyph-plotter using objects. void obj_drawg(uint gid) { TTC *tc= tte_get_context(); TFont *font= tc->font; uint x0= tc->cursorX, y0= tc->cursorY; // (1) find the right object, and increment index. uint id= tc->dst.pitch; OBJ_ATTR *obj= &((OBJ_ATTR*)tc->dst.data)[-id]; tc->dst.pitch= (id+1 < tc->dst.width ? id+1 : 0); // (2) Set object attributes. obj->attr0= tc->cattr[0] + (y0 & ATTR0_Y_MASK); obj->attr1= tc->cattr[1] + (x0 & ATTR1_X_MASK); obj->attr2= tc->cattr[2] + gid*font->cellW*font->cellH/64; } An d , y e s , Ik n o wt h a tt h i su s eo ft h edstme mb e ri ss o me wh a t… u n o r t h o d o x ;b u ti twa s n ' tu s e dh e r ea n y wa ys owh yn o t .Ia m c o n s i d e r i n gu s i n gs o me t h i n gmo r ep r o p e r , b u tn o tj u s ty e t .Al s o , r e me mb e rt h a tt h i ss y s t e ma s s u me st h a tt h ef o n ti sa l r e a d yl o a d e di n t o VRAM a n dt h a tt h i sc a nt a k eu palot o ft h ea v a i l a b l et i l e s .Us i n gt h ev e r d a n a9f o n t , t h a t ' db e2 * 2 4 0=4 8 0t i l e s .Th a t ' sn e a r l yh a l fo f o b j e c tVRAM.As a f e ra l t e r n a t i v ewo u l db et ol o a dt h en e c e s s a r yt i l e sd y n a mi c a l l y , b u tt h a two u l dr e q u i r emo r er e s o u r c ema n a g e me n t . TTE object text is ugly Th ewa yo b j e c tt e x ti sh a n d l e di nTTEwo r k s , b u ti tt h ei mp l e me n t a t i o ni sn o te x a c t l yp r e t t y .Th ewa yI ' mu s i n gTTC.dst h e r ei s , we l l , b a d .Th e r ei sag o o dc h a n c eI ' l l c l e a ni tu pab i tl a t e r , o ra tt h ev e r yl e a s th i d et h ei mp l e me n t a t i o nb e t t e r . 22.5.1. Example: letters. Onna path Th ed e f i n i n gc h a r a c t e r i s t i co fo b j e c t si st h a tt h e y ' r es e p a r a t ef r o mb a c k g r o u n d s ;t h e yc a nmo v ea r o u n dt h es c r e e ni n d e p e n d e n t l y .Ob j e c t t e x ti smo s tl i k e l yu s e df o rt e x tt h a ti sd y n a mi co rh a st ot r a v e l a l o n gs o mes o r to fp a t h .I nt h i sc a s e , I ' l l ma k et h e mf l yo na p a r a me t e r i z e dp a t hc a l l e daLi s s a j o u sc u r v e( s e ef i g2 2 . 9 ) . Th ec o d ei sg i v e nb e l o w.Af t e ri n i t i a l i z i n gt h eu s u a l s u s p e c t s , tte_init_obj()i s c a l l e d .Th eo b j e c ts t a c ks t a r t sa tt h eb a c ko fOAM, wh i c hi sa l s owh a tt h es y s t e md e f a u l t s t oi fNULLp a s s e da st h ef i r s tp a r a me t e r .Th en e x tt h r e ea r et h eo b j e c ta t t r i b u t e s .Be c a u s e Iwa n tt ou s et h ed e f a u l tv a r i a b l ewi d t hf o n t , v e r d a n a9 , t h ea t t r i b u t e ss h o u l db es e tt o8 ×1 6 o b j e c t s .Th eb i t d e p t ho ft h et i l e swi l l a l wa y sb e4t ok e e pt h en u mb e ro fu s e dt i l e swi t h i n l i mi t s .Th er e s to ft h ei n i t i a l i z a t i o ns h o u l db ee a s yt ou n d e r s t a n d . Ma k i n gt h es t r i n gi t s e l fi sd o n ea ts t e p2 .No t et h a tt h es t r i n ga l s os e tt h ep a p e rc o l o r a t t r i b u t e( wh i c hc o r r e s p o n d st oo b j . a t t r 2 )t o0 x 1 0 0 0t oma k et h e“ o mg ”r e d .Af t e rt h e s e f e wl i n e s , t h et e x th a n d l i n gi t s e l fi sc o mp l e t e . I ns t e p3 , t h ec o o r d i n a t e so nt h ep a t ha r ec a l c u l a t e d .Th etp a r a me t e ri n d i c a t e st h eh o w Fig 22.9:Obj e c tt e x to napa t h . f a ra l o n gt h ep a t hwea r e .I ti su s e dt oc a l c u l a t et h ec o o r d i n a t e so fe a c hl e t t e r–t h ef i r s t o n eu s i n gti t s e l f , a n dt h er e s ta r ee s s e n t i a l l yt i me d e l a y e d .Do n ' tb ed i s t r a c t e db yt h ema g i cn u mb e r s : t h eo n l yr e a s o nf o rt h e i rv a l u e si s t oma k et h ee f f e c tl o o ka l r i g h t .Tr yt we a k i n gt h e mal i t t l et os e ewh a tt h e yd oe x a c t l y . // Object text demo void test_tte_obj() { // Base inits irq_init(NULL); irq_add(II_VBLANK, NULL); REG_DISPCNT= DCNT_MODE0 | DCNT_OBJ | DCNT_OBJ_1D; oam_init(oam_mem, 128); VBlankIntrWait(); // (1) Init object text, using verdana 9 (8x16 objects) OBJ_ATTR *objs= &oam_mem[127]; tte_init_obj( objs, // Start at back of OAM ATTR0_TALL, // attr0: 8x16 objects file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 227/331 28-03-13 Tonc : GBA Programming in rot13 ATTR1_SIZE_8, 0, CLR_YELLOW, 0x0E, &vwf_default, NULL); // attr1: 8x16 objects // attr2: nothing special // Yellow ink // ink pixel 14+1 = 15 // Verdana 9 font // Default renderer (obj_drawg) pal_obj_bank[1][15]= CLR_RED; // (2) Write something (and prep for path) const char *str= "Parametrized object text, omg!!!"; const int len= strlen(str); tte_write("Parametrized object text, #{cp:0x1000}omg#{cp:0}!!!"); // Play with the objects int ii, t= 0x9000; while(1) { VBlankIntrWait(); key_poll(); // (3) Make lissajous figure for(ii=0; ii<len; ii++) { int ti= t-0x380*ii; // Get the path param for letter ii obj_set_pos(&objs[-ii], (96*lu_cos( ti)>>12)+120, // y= Ay*cos( t) + y0 (64*lu_sin(2*ti)>>12)+80); // x= Ax*sin(2*t) + x0 } t += 0x00A0; if(key_hit(KEY_START)) break; } } 22.6. Rendering to tiles Us i n gt i l e ma p sf o rt e x ti sn i c e , b u twi l l o n l ywo r ki ft h ed i me n s i o n so ft h eg l y p h sa r emu l t i p l e so f8 .Th e r ea r eaf e wd r a wb a c k si nt e r ms o fr e a d a b i l i t y : n a r r o wc h a r a c t e r ss u c ha s‘ i ’wi l l s e e me i t h e ro v e r l ywi d e , o rb es u r r o u n d e db yma n ye mp t yp i x e l s .Al s o , y o uc a n ' tp u t ma n yc h a r a c t e r so nal i n eb e c a u s et h e r ea r eo n l ys oma n yt i l e s . Va r i a b l e wi d t hf o n t s( vwf;a l s ok n o wna sp r o p o r t i o n a l f o n t s )s o l v et h i sp r o b l e m.Us i n gv a r i a b l e wi d t hf o n t so nb i t ma p si sq u i t ee a s y , a ss h o wni ns e c t i o n2 2 . 4 . .Ho we v e r , u s i n gi ti nt i l e ma pmo d e si sal i t t l et r i c k i e r : h o wd oy o ud r a wo nat i l e ma pwh e r et h et i l e sa r e8 ×8i n s i z e ? We l l , y o ud o n ' t .No te x a c t l y .Th ek e yi sn o tt od r a wt ot h ema p , b u tt ot h et i l e st h a tt h ema ps h o ws . 22.6.1. Basic tile rendering Th eu s u a l wa yt owo r kwi t ht i l e ma p si st h a ty o ul o a du pat i l e s e t , a n dt h e ns e l e c tt h eo n e sy o uwa n tt os h o wu po nt h es c r e e nb yf i l l i n g t h et i l e ma p .I nt h o s ec i r c u ms t a n c e s , t h et i l e s e ti so f t e ns t a t i c , wi t ht h ema pb e i n gu p d a t e df o rt h i n g sl i k es c r o l l i n g .Re n d e r i n gt ot i l e s r e v e r s e st h a tp r o c e d u r e . Fi r s t , y o un e e dt os e tu pama pwh e r ee a c he n t r yp o i n t st oau n i q u et i l e .Th i se s s e n t i a l l yf o r msag r a p h i c a l s u r f a c eo u to ft h et i l e s , wh i c hy o uc a nt h e nd r a wt ol i k ea n yo t h e r .Th emo s to b v i o u swa yt od ot h i si st os i mp l yf i l l u pt h es c r e e n b l o c kwi t hc o n s e c u t i v en u mb e r s ( s e ef i g2 2 . 1 0 a ) .Ho we v e r , ab e t t e rwa yt oma pt h et i l e si sb yma p p i n gt i l e si nc o l u mn ma j o ro r d e r( s e ef i g2 2 . 1 0 b ), f o rt h es a mer e a s o n Ic h o s ei tf o rt h eg l y p hf o r ma t : t h ewo r d si nac o l u mno ft i l e sa r ec o n s e c u t i v e . Fig 22.10a.Ro wma j o rt i l ei n d e x i n g . Fig 22.10b.Co l u mn ma j o rt i l ei n d e x i n g . Pr e p a r i n gt h ema pi st h ee a s yp a r t ;t h ep r o b l e mi sk n o wi n gwh i c hp a r to fwh i c ht i l et oe d i tt op l o tap i x e l .Fi r s t , y o un e e dt os p l i tt h e c o o r d i n a t e si n t ot i l ec o o r d i n a t e sa n dp i x e l i n t i l ec o o r d i n a t e s .Th i sc o me sd o wnt od i v i s i o na n dmo d u l ob y8 , r e s p e c t i v e l y .No t et h a ti n c o l u mn ma j o rmo d ey o uo n l yn e e dt od ot h i sf o rt h ex c o o r d i n a t e .Wi t ht h i si n f o r ma t i o n , y o uc a nf i n dt h er i g h two r d .Th eh o r i z o n t a l i n t i l e file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 228/331 28-03-13 Tonc : GBA Programming in rot13 c o o r d i n a t et e l l sy o uwh i c hn y b b l ei nt h ewo r dt ou p d a t ea n da tt h a tp o i n ti t ' st h eu s u a l b i t f i e l di n s e r t i o n . To n c l i bh a sr o u t i n e sf o rd r a wi n go n t o4 b p p , c o l u mn ma j o rt i l e s( r e f e r r e dt oa schr4c mo d e ) .Th ep l o t t e ra n dt h ema pp r e p a r a t i o n f u n c t i o n sa r eg i v e nb e l o w, a l o n gwi t had e mo n s t r a t i o nr o u t i n et oe x p l a i nt h e i ru s e . //# From tonc_schr4c.c //! Plot a pixel on a 4bpp tiled, column-major surface. void schr4c_plot(const TSurface *dst, int x, int y, u32 clr) { uint xx= x; // fluff to make x unsigned. u32 *dstD= (u32*)(dst->data + xx/8*dst->pitch); uint shift= xx%8*4; dstD[y] = (dstD[y] &~ (15<<shift)) | (clr&15)<<shift; } //! Prepare a screen-entry map for use with chr4c mode. void schr4c_prep_map(const TSurface *srf, u16 *map, u16 se0) { uint ix, iy; uint mapW= srf->width/8, mapH= srf->height/8, mapP= srf->pitch/32; for(iy=0; iy<mapH; iy++) for(ix=0; ix<mapW; ix++) map[iy*32+ix]= ix*mapP + iy + se0; } //# --- Simple test --------------------------------------------------void test_chr4() { // (1) The usual irq_init(NULL); irq_add(II_VBLANK, NULL); REG_DISPCNT= DCNT_MODE0 | DCNT_BG0; REG_BG0CNT= BG_CBB(0) | BG_SBB(31); pal_bg_mem[1]= CLR_RED; pal_bg_mem[2]= CLR_GREEN; pal_bg_mem[3]= CLR_BLUE; pal_bg_mem[4]= CLR_WHITE; // (2) Define a surface TSurface srf; srf_init(&srf, SRF_CHR4C, // Surface type. tile_mem[0], // Destination tiles. SCREEN_WIDTH, // Surface width. SCREEN_HEIGHT, // Surface height. 4, // Bitdepth (ignored due to SRF_CHR4C). pal_bg_mem); // Palette. // (3) Prepare the map schr4c_prep_map(&srf, se_mem[31], 0); // (4) Plot some things int ii, ix, iy; for(iy=0; iy<20; iy++) for(ix=0; ix<20; ix++) schr4c_plot(&srf, ix+3, iy+11, 4); for(ii=0; ii<20; ii++) { schr4c_plot(&srf, ii+4, 12, 1); // Red line schr4c_plot(&srf, ii+4, ii+12, 2); // Green line schr4c_plot(&srf, 4, ii+12, 3); // Blue line } } Th ep i x e l p l o t t e rs t a r t sb yf i n d i n gt h et i l e c o l u mnt h a tt h ed e s i r e dp i x e l i si n .Th ec o l u mn i n d e xi ss i mp l yx/ 8 ;t h i si smu l t i p l i e db yt h ep i t c h t og e tap o i n t e rt ot h et o po ft h ec o l u mn .No t et h a tp i t c hi su s e dal i t t l ed i f f e r e n tt h a nu s u a l .No r ma l l y , i td e n o t e st h en u mb e ro fb y t e st o t h en e x ts c a n l i n e , b u ti nt h i sc a s ei t ' su s e da st h eb y t e o f f s e tt on e x tt i l e c o l u mn .Fo rac o l u mn ma j o rmo d e , t h i sc o me sd o wnt ot h e file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm t h 229/331 28-03-13 Tonc : GBA Programming in rot13 t h height×bpp* 8 / 8 , b u ta l l t h a ti sd o n ei nsrf_init().On c ey o uh a v et h er i g h tt i l e , t h ep i x e l y o uwa n ti si nt h ex%8 n y b b l e , me a n i n g t h er e q u i r e ds h i f tf o rt h ei n s e r t i o ni sx%8 * 4 .Af t e rt h a t , i t ' sj u s tama t t e ro fi n s e r t i n gt h ec o l o r .( Fo rt h ec u r i o u s : I ' mc a s t i n gx t ouns i g n e di n tf i r s t b e c a u s edi v i s i o na n dmo d u l owi l l t h e nb eo pt i mi z e dt os h i f t s / ma s k spr o pe r l y. ) Th eschr4c_prep_map()f u n c t i o nj u s ti n i t i a l i z e st h ema pi nt h eo r d e rg i v e ni nf i g2 2 . 1 0 b .We l l , a l mo s t . I ' ma l s oa d d i n gav a l u et o e a c hs c r e e n e n t r yl i k eIu s u a l l yd of o rp a l e t t e sa n dt i l e o f f s e t s . Th eo u t p u to ftest_chr4()c a nb es e e ni nf i g2 2 . 1 1 a .I t ' sawh i t er e c t a n g l ewi t hr e d , g r e e na n db l u el i n e s , a se x p e c t e d .Fi g2 2 . 1 1 bi s ap i c t u r et a k e nf r o mVBA' st i l ev i e we r , s h o wi n gh o wt h ec o n t e n t so ft h es u r f a c e .Do e s n ' tq u i t el o o kwh a t ' so nt h es c r e e n , d o e si t ?St i l l , i fy o ul o o kc l o s e l y , y o uc a nf i g u r eo u th o wi two r k s .Ea c hs e to f2 0t i l e sf o r mso n et i l e c o l u mno nt h es c r e e n( i n d i c a t e db yy e l l o w b l o c k s ) .Wh e ny o up l a c et h e s et i l e so nt o po fe a c ho t h e r , y o u ' l l s e et h ep i c t u r eo ff i g2 2 . 1 1 ae me r g e . Fig 22.11a: c h r 4 _ t e s t ( ) o u t pu t Fig 22.11b: c h r 4 _ t e s t ( )t i l e s . Th eye l l o wb l o c k si n d i c a t e t i l e sofas i n g l ec o l u mn . 22.6.2. Text rendering on tiles Version 1 : pixel by pixel Th ee a s i e s twa yt or e n d e rg l y p h st ot i l e si st of o l l o wt h et e mp l a t ef r o ms e c t i o n2 2 . 4 . .Th i si sd o n ei nt h ef u n c t i o nb e l o w. //! Simple version of chr4 renderer. void chr4_drawg_b1cts_base(uint gid) { TTE_BASE_VARS(tc, font); TTE_CHAR_VARS(font, gid, u8, srcD, srcL, charW, charH); uint x0= tc->cursorX, y0= tc->cursorY; uint srcP= font->cellH; u32 ink= tc->cattr[TTE_INK], raw; uint ix, iy, iw; for(iw=0; iw<charW; iw += 8) { for(iy=0; iy<charH; iy++) { raw= srcD[iy]; for(ix=0; raw>0; raw>>=1, ix++) if(raw&1) schr4c_plot(&tc->dst, x0+ix, y0+iy, ink); } srcD += srcP; x0 += 8; } } No w, y o uma yt h i n kt h a tt h i sr u n sp r e t t ys l o wl yt h a n k st oa l l t h er e c a l c u l a t i o n si nschr4c_plot().An dy o u ' db er i g h t , b u ti nt r u t h , i t ' s n o ta sb a da sIo r i g i n a l l yt h o u g h t .I ti sp o s s i b l et os p e e di tu pb ys i mp l yi n l i n i n gt h i n g s , b u tt h er e a l g a i nc o me sf r o md r a wi n gp i x e l si n p a r a l l e l . Version 2 : 8 pixels at once. I n s t e a do fp l o t t i n gp i x e l i n d i v i d u a l l y , y o uc a na l s op l o tmu l t i p l ep i x e l ss i mu l t a n e o u s l y .Th ebmp8_drawg_b1cts()r e n d e r e rwes a w e a r l i e rd i dt h i s : i tu n p a c k e d2p i x e l sa n dd r e wt o g e t h e r .I nt h ec a s eo f4 b p pt i l e s , y o uc a nu n p a c kt h es o u r c eb y t ei n t oo n e( 3 2 b i t )wo r d a n dp l o teight p i x e l sa to n c e .Th eo n l yd o wn s i d ei st h a ty o u ' l l p r o b a b l yh a v et os p l i ti to v e rt wot i l e s . Th en e x tf u n c t i o ni sTTE' sma i ng l y p hr e n d e r e rf o rt i l e s , a n di ti sad o o z y .Th e r ea r et wos t a g e sf o rt h er e n d e r i n gi nt h ei n n e rl o o p : b i tu n p a c k i n gt h es o u r c eb y t e , rawa n ds p l i t t i n gt h ep r e p a r e dp i x e l pxi n t ot woa d j a c e n tt i l e s .Th e s ec o r r e s p o n dt os t e p s3a n d4 , r e s p e c t i v e l y . No r ma l l y , t h eb i t u n p a c ki sd o n ei nal o o p , b u ts o me t i me si t ' sf a s t e rt od oi ti no t h e rwa y s .Fo rd e t a i l s , s e emyd o c u me n to nb i tt r i c k s . Th ef i r s tf i v el i n e so fs t e p3d ot h eu n p a c k i n g .Fo re x a mp l e , i tt u r n sab i n a r y0001 1011i n t oah e x a d e c i ma l 0x00011011.Th i si s t h e nmu l t i p l i e db y1 5a n dinkt og i v et h ep i x e l ma s kpxmaska n dt h ec o l o r e dp i x e l spx, r e s p e c t i v e l y . St e p4d i s t r i b u t e st h ewo r dwi t ht h ep i x e l so v e rt wot i l e si fn e c e s s a r y .I ns t e p1 , l e f ta n dr i g h ts h i f t swe r ep r e p a r e dt os u p p l yt h eb i t o f f s e t sf o rt h i sp r o c e d u r e .No w, f o rl a r g e rg l y p h st h i swi l l me a nt h a tc e r t a i nd e s t i n a t i o nwo r d sa r eu s e dt wi c e , b u tt h i sc a n ' tb eh e l p e d ( a c t u a l l yi tc a n , b u tt h ep r o c e d u r ei su g l ya n dp o s s i b l yn o two r t hi t ) .Ana l t e r n a t i v et ot h i si su s i n gt h ed e s t i n a t i o no n c ea n dr e a d( a n d u n p a c k / c o l o r )t h es o u r c et wi c e ;h o we v e r , a sVRAM i sc o n s i d e r a b l yf a s t e rt h a nROM Id o u b tt h i swo u l db eb e n e f i c i a l . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 230/331 28-03-13 Tonc : GBA Programming in rot13 //! Render 1bpp fonts to 4bpp tiles; col-major order. void chr4c_drawg_b1cts(uint gid) { // Base variables. TTE_BASE_VARS(tc, font); TTE_CHAR_VARS(font, gid, u8, srcD, srcL, charW, charH); uint x= tc->cursorX, y= tc->cursorY, dstP= tc->dst.pitch/4; uint srcP= font->cellH; // (1) Prepare dst pointers and shifts. u32 *dstD= (u32*)(tc->dst.data + (y + x/8*dstP)*4), *dstL; x %= 8; uint lsl= 4*x, lsr= 32-4*x, right= x+charW; // Inner loop vars. u32 px, pxmask, raw; u32 ink= tc->cattr[TTE_INK]; const u32 mask= 0x01010101; uint iy, iw; for(iw=0; iw<charW; iw += 8) // Loop over strips { // (2) Update and increment main data pointers. srcL= srcD; srcD += srcP; dstL= dstD; dstD += dstP; for(iy=0; iy<charH; iy++) // Loop over scanlines { raw= *srcL++; if(raw) { // (3) Unpack 8 bits into 8 nybbles and create the mask raw |= raw<<12; raw |= raw<< 6; px = raw & mask<<1; raw &= mask; px = raw | px<<3; pxmask= px*15; px *= ink; // (4a) Write left tile: dstL[0] = (dstL[0] &~ (pxmask<<lsl) ) | (px<<lsl); // (4b) Write right tile (if any) if(right > 8) dstL[dstP]= (dstL[dstP] &~ (pxmask>>lsr) ) | (px>>lsr); } dstL++; } } } chr4c_drawg_b1cts()i sp r e t t yf a s t .I tc e r t a i n l yi sf a s t e rt h a nt h ee a r l i e rv e r s i o nb ya b o u t3 3 %.I t ' sa c t u a l l ye v e nf a s t e rt h a nt h e b mp 8r e n d e r e r , b u to n l yb yas l i mma r g i n . Ofc o u r s e , y o uc a na l wa y sg oo n eb e t t e r .Th ev a r i o u ss h i f t sa n dc o n d i t i o n a l sma k ei tp e r f e c tf o rARM c o d e , r a t h e rt h a nTh u mb . An dt oma k es u r ei tg o e se x a c t l ya c c o r d i n gt op l a n , I ' md o i n gt h i si na s s e mb l y . Version 3: ARM asm Th en e x tf u n c t i o ni schr4_drawg_b1cts_fast(), t h eARM a s s e mb l ye q u i v a l e n to fv e r s i o n2 .Th e r e ' sa na l mo s to n e t o o n e c o r r e s p o n d e n c eb e t we e nt h eCa n da s ml o o p s , s oj u s tl o o pt ot h eCv e r s i o nf o rt h ee x p l a n a t i o n . Sp e e d wi s e , t h ea s mv e r s i o ni smuch b e t t e rt h a nt h eCv e r s i o n .Ev e ni nROM, wh i c hi svery b a df o rARM c o d e , i ti ss t i l l f a s t e rt h a n t h eTh u mbv e r s i o n .Th e r ea r eo n eo rt wot i n yd e t a i l sb ywh i c hy o uc a ns p e e dt h i st h i n gu p , b u tb ya n dl a r g et h i ss h o u l db ei tf o rf o n t so f a r b i t r a r yd i me n s i o n s .Ofc o u r s e , i fy o uh a v ef i x e ds i z e sf o ry o u rf o n ta n dd on o tr e q u i r er e c o l o r i n go rt r a n s p a r e n c y , t h i n g swi l l b eal i t t l e d i f f e r e n t . // Include TTC/TFont member offsets plz. #include "tte_types.s" /* IWRAM_CODE void chr4c_drawg_b1cts_fast(int gid); file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 231/331 28-03-13 Tonc : GBA Programming in rot13 */ .section .iwram, "ax", %progbits .arm .align .global chr4c_drawg_b1cts_fast chr4c_drawg_b1cts_fast: stmfd sp!, {r4-r11, lr} ldr ldr r5,=gp_tte_context r5, [r5] @ Preload dstBase (r4), dstPitch (ip), yx (r6), font (r7) ldmia r5, {r4, ip} add r3, r5, #TTC_cursorX ldmia r3, {r6, r7} @ Get srcD (r1), width (r11), charH (r2) ldmia r7, {r1, r3} @ Load data, widths cmp r3, #0 ldrneb r11, [r3, r0] @ Var charW ldreqb r11, [r7, #TF_charW] @ Fixed charW ldrh r3, [r7, #TF_cellS] mla r1, r3, r0, r1 @ srcL ldrb r2, [r7, #TF_charH] @ charH ldrb r10, [r7, #TF_cellH] @ cellH @ Positional issues: dstD(r0), lsl(r8), lsr(r9), right(lr), cursorX mov r3, r6, lsr #16 @y bic r6, r6, r3, lsl #16 @x add mov mla r0, r4, r3, lsl #2 r3, r6, lsr #3 r0, ip, r3, r0 @ dstD= dstBase + y*4 and add mov rsb r6, r6, #7 lr, r11, r6 r8, r6, lsl #2 r9, r8, #32 @ x%7 @ right= width + x%8 @ lsl = x%8*4 @ lsr = 32-x%8*4 ldr ldrh r6,=0x01010101 r7, [r5, #TTC_ink] @ --- Reg-list for strip/render loop --@ r0 dstL @ r1 srcL @ r2 scanline looper @ r3 raw @ r4 px / tmp @ r5 pxmask @ r6 bitmask @ r7 ink @ r8 left shift @ r9 right shift @ r10 dstD @ r11 charW @ ip dstP @ lr split indicator (right edge) @ sp00 charH @ sp04 deltaS = cellH-charH (delta srcL) cmp r11, #8 @ Prep for single-strip render suble sp, sp, #8 ble .Lyloop @ Prep for multi-strip render sub r3, r10, r2 mov r10, r0 stmfd sp!, {r2, r3} @ Store charH, deltaS b .Lyloop @ --- Strip loop --.Lsloop: @ (2) Update and increment main data pointers. file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 232/331 28-03-13 Tonc : GBA Programming in rot13 ldmia add mov add sub sp, {r2, r3} r10, r10, ip r0, r10 r1, r1, r3 lr, lr, #8 @ Reload charH and deltaS @ (Re)set dstD/dstL @ --- Render loop --.Lyloop: @ (3) Prep px and pxmask ldrb r3, [r1], #1 orrs r3, r3, r3, lsl #12 beq .Lnopx @ Skip if no pixels orr r3, r3, r3, lsl #6 and r4, r3, r6, lsl #1 and r3, r3, r6 orr r3, r3, r4, lsl #3 rsb mul r5, r3, r3, lsl #4 r4, r3, r7 @ (4a) Render to left tile ldr r3, [r0] bic r3, r3, r5, lsl r8 orr r3, r3, r4, lsl r8 str r3, [r0] @ (4b) Render to right tile cmp lr, #8 ldrgt r3, [r0, ip] bicgt r3, r3, r5, lsr r9 orrgt r3, r3, r4, lsr r9 strgt r3, [r0, ip] .Lnopx: add subs bne r0, r0, #4 r2, r2, #1 .Lyloop @ Test for strip loop subs r11, r11, #8 bgt .Lsloop add ldmfd bx sp, sp, #8 sp!, {r4-r11, lr} lr @ EOF 22.6.3. Multi-color and shaded fonts. Bi t p a c k e df o n t swi l l g i v ey o umo n o c h r o meg l y p h s .I fy o uwa n tmo r ec o l o r s–f o rs h a d i n go ra n t i a l i a s i n g–y o u ' l l n e e dt ou s emo r eb i t s . Th ec o d ef o rt h i si sn e a r l yi d e n t i c a l t ot h e1 b p pb i t p a c k e dv e r s i o n ;t h emo s ti mp o r t a n td i f f e r e n c e sb e i n gad i f f e r e n ts o u r c ed a t a t y p ea n d a na l t e r n a t i v eme t h o df o rf i n d i n gt h er i g h tma s k .Oh , a n dy o uwo n ' th a v et ou n p a c kt h eb i t sa n y mo r e , o fc o u r s e . Th ef o l l o wi n gs n i p p e ts h o wsh o wy o uc a nma k eat r a n s p a r e n c yma s ko u to fawo r do f4 b i tp i x e l s .Es s e n t i a l l y , y o uma s ka l l t h eb i t s o fan y b b l et o g e t h e ra n dma s ko u tt h eo t h e rb i t so ft h a tn y b b l e .Th i sg i v e s0i ft h ewh o l en y b b l ewa se mp t y , o r1i si twa s n ' t .Th i sc a n t h e na g a i nb emu l t i p l i e db y1 5t og i v et h ep r o p e rma s k . // Create pixel mask from 8x 4 bits u32 *srcL= ...; // Source is now 32bit. raw = *srcL++; pxmask = raw; pxmask |= pxmask>>2; pxmask |= pxmask>>1; pxmask &= 0x11111111; pxmask *= 15; // Source word: 8x 4 bits // bit0 = bit0 | bit2 // bit0 = bit0 | bit1 | bit2 | bit3; // bit0 is 0 only if bits 0-3 were all 0 Shaded characters No , n o ts h a d yc h a r a c t e r s ;s h a d ed c h a r a c t e r s .Wh a ty o u ' l l o f t e ns e ei ng a me si st h a tt h et e x th a se i t h e ra no u t l i n eo rab i to fs h a d i n go n o n es i d e .Wh i l ei ti sp o s s i b l et oc r e a t es h a d i n gwi t ha1 b p pf o n t , i t ' se a s i e rt os i mp l yb u i l di ti n t ot h ef o n ti t s e l f( s e ef i g2 2 . 1 2 ) .Be c a u s e t h i sme a n smo r ec o l o r st h a n1 b p pc a nh a n d l e , y o uma yb et e mp t e dt ou s ea2 b p pf o n th e r e .Ho we v e r , u n l e s sy o ua r ereally s t r e s s e df o r file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 233/331 28-03-13 Tonc : GBA Programming in rot13 me mo r y , i t ' smo r ec o n v e n i e n tt ou s e4 b p ph e r ea swe l l . Att h a tp o i n t , y o uc a nf o l l o wt h ep r o c e d u r ed e s c r i b e de a r l i e r .Bu tb yc l e v e r l yu s i n gt h eb i t st h a tma k eu pt h es h a d i n g , y o uc a na l l o w t h es h a d o wc o l o rt ob ev a r i a b l ea swe l l .Fo re x a mp l e , y o uc a nd e s i g n a t eb i t0a st h e' i n k ' b i t , b i t1a st h e' s h a d o w' b i ta n di fn e c e s s a r yb i t 2a st h e' p a p e r ' b i t .Th e nraw&0x11111111g i v e st h e' i n k ' ma s k , a n d(raw>>1)&0x11111111g i v e st h e' s h a d o w' ma s k ;t h e s e c a nt h e nb eu s e dt oa p p l yc o l o r sa n dt oc r e a t et h ef u l l ma s k .Th ef o l l o wi n gi sad e mo n s t r a t i o no fh o wt h i sc a nb ed o n e .No t et h a te a c h l i n eh e r ec o r r e s p o n d se x a c t l yt oo n eARM i n s t r u c t i o n , s ot h i ss h o u l db ea ne f f i c i e n tme t h o d .We l l , i nARM c o d ea n y wa y . // Use bits 0 and 1 from each nybble to create masks and apply colors. u32 *srcL= ...; raw = *srcL++; px = raw & 0x11111111; raw = raw>>1 & 0x11111111; pxmask = px | raw; pxmask *= 15; // Source word: 8x 4 bits // Bit 0 for ink pixels // Bit 1 for shadow pixels // Mask of ink and shadow bits px px // Color with ink // Add shadow pixels = px * ink; += raw* shadow; Th echr4c_drawg_b4cts()r e n d e r e ru s e st h i sme t h o dt oc o l o rb o t ht h ei n ka n ds h a d o wp i x e l s .I t ' s e s s e n t i a l l ychr4c_drawg_b4cts()e x c e p tf o rt h et h i n g si nb o l da n dt h er e mo v a l o ft h eb i tu n p a c k i n g . Al s on o t et h e‘ n o p i x e l ’c o n d i t i o nh e r e .I fpxmaski sz e r o , t h e r e ' sn o t h i n gt od o ;a n ds owewo n ' t . Fig 22.12:Ve r da n a9, wi t hs h a d e . //! 4bpp font, tilestrips with ink/shadow coloring. void chr4c_drawg_b4cts(uint gid) { TTE_BASE_VARS(tc, font); TTE_CHAR_VARS(font, gid, u32, srcD, srcL, charW, charH); uint x= tc->cursorX, y= tc->cursorY; uint srcP= font->cellH, dstP= tc->dst.pitch/4; // (1) Prepare dst pointers and shifts. u32 *dstD= (u32*)(tc->dst.data + (y+x/8*dstP)*4), *dstL; x %= 8; uint lsl= 4*x, lsr= 32-4*x, right= x+charW; // Inner loop vars u32 amask= 0x11111111; u32 px, pxmask, raw; u32 ink= tc->cattr[TTE_INK]; u32 shade= tc->cattr[TTE_SHADOW]; uint iy, iw; for(iw=0; iw<charW; iw += 8) // Loop over strips { srcL= srcD; srcD += srcP; dstL= dstD; dstD += dstP; for(iy=0; iy<charH; iy++) { raw= *srcL++; // Loop over scanlines // (3a) Prepare pixel mask px = (raw & amask); raw = (raw>>1 & amask); pxmask= px | raw; if(pxmask) { px *= ink; // (3b) Color ink pixels px += raw*shade; // (3c) Color shadow pixels pxmask *= 15; // (3d) Create mask // (4a) Write left tile: dstL[0] = (dstL[0] &~ (pxmask<<lsl) ) | (px<<lsl); // (4b) Write right tile (if any) if(right > 8) dstL[dstP]= (dstL[dstP] &~ (pxmask>>lsr) ) | (px>>lsr); } dstL++; file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 234/331 28-03-13 Tonc : GBA Programming in rot13 } } } 22.6.4. Tips for fast tile rendering I ' v ed o n eaf a i rb i to fp r o f i l i n gf o rt h e s et i l er e n d e r e r sa n dt h i n kIh a v ead e c e n tk n o wl e d g eo fwh i c ht e c h n i q u e swi l l b ee f f i c i e n ta n d wh i c hwo n ' t .Th e s ea r es o meo fmyo b s e r v a t i o n s . Profile.Be f o r ec o n j u r i n gu pt r i c k yr o u t i n e s , ma k es u r et h eo r i g i n a l s i mp l ev e r s i o nwa r r a n t so p t i mi z i n gand t h a tt h ec l e v e r r o u t i n ei sa c t u a l l yf a s t e r . Render transparently.No w, y o u ' dt h i n kt h a tt h i swo u l db es l o we r , b u ti tma yn o tb e .Th et h i n ga b o u tt r a n s p a r e n tt e x ti st h a t t h e r ea r emu c hl e s sf o r e g r o u n dp i x e l st h e nt h e r ea r eb a c k g r o u n dp i x e l s , s ot h en u mb e ro fp i x e l st or e n d e ri sl o we ra swe l l . Don't buffer.Myf i r s tt r i a l sh a ds e p a r a t es t a g e sf o ru n p a c k i n g / c o l o r i n ga n di n s e r t i n gi n t oVRAM.I tp u tt h ep r e p a r e dp i x e l s i n t oa nI WRAM b u f f e r , t h e nc o p i e dt h a tt oVRAM.I fIr e c a l l c o r r e c t l y , c o mb i n i n gt h el o o p sa n dt o s s i n gt h eb u f f e rs a v e dme 3 0 %. Parallelize.Th er o a dt og e t t i n gt h er i g h td a t ai sl o n g .I th e l p si fy o ud o n ' th a v et ot r a v e l i tt h a tmu c h .Th a ts a i d , i fy o uh a v e ma n ye mp t yp i x e l s , d r a wi n g8o ft h e ma to n c ema yb eawa s t eo fe f f o r t .Th i swi l l d e p e n do nt h ef o n t . ARM code is teh r0xx0rz.Th e r ea r el o t so fs h i f t s , ma s k sa n dq u a n t i t i e si nt h e s er o u t i n e s .Th i sma k e st h e mp a r t i c u l a r l ya p t f o rARM c o d ei n s t e a do fTh u mb .I nf a c t , e v e ni nROM wi t hi t s1 6 b i tb u s , t h eARM v e r s i o n sb e a to u tt h eTh u mb c o mp i l e d o n e s .Ha v i n gs a i dt h a t… Do not let GCC use constant masks in ARM.Th e r ei sa nu n f o r t u n a t eb u gi nt h eARM o p t i mi z e rc o n c e r n i n gANDi n g l i t e r a l s( l i k e0 x 1 1 1 1 1 1 1 1 ) .I n s t e a do fe mi t t i n gas i mp l eldr+andp a i r , i twi l l g e tc l e v e ra n da v o i dt h el o a db ys p l i t t i n gt h ema s k o u to v e rmu l t i p l eb y t e s i z ema s k s .Soi n s t e a do fo n ei n s t r u c t i o ni nt h ei n n e r l o o p , y o un o wh a v ef o u r .Pe r h a p se v e nmo r e , d e p e n d i n go nh o wma n ye x t r ar e g i s t e r st h i st a k e s .No t e , t h i sonly h a p p e n sf o rc o n s t a n t sa n do n l yf o rARMc o mp i l e dc o d e .A wo r k a r o u n di st oh a v et h ema s ki nag l o b a l v a r i a b l et ob el o a d e db e f o r et h el o o p s .Th i si si np a r twh yI ' v eh a n d a s s e mb l e d s o meo ft h er o u t i n e s . Code for special case if you can.I fy o uo n l yh a v eo n ef o n ta n dd o n ' tr e q u i r et h i n g sl i k ec o l o r i n g , y o uc a nc o d ef o rt h a tc a s e o n l ya n dp o t e n t i a l l ys a v emu c ht i me .Us i n gc o n s t a n t sf o rs o u r c ea n dd e s t i n a t i o nd i me n s i o n si n s t e a do fu s i n gt h eo n e si n me mo r ywi l l a l s oh e l pal i t t l e . Use column-major accessing.Th er o u t i n e sp r e s e n t e da b o v er e q u i r ee x t r ac o d et omo v ef r o mo n et i l e r o wt oa n o t h e r .I fy o u u s et h et i l e si nac o l u mn ma j o rl a y o u t , y o uwo n ' th a v et od ot h i s . Pl e a s ea p p l yt h es t a n d a r dd i s c l a i me rt ot h i sl i s t .I ' v ef o u n dt h e s et e c h n i q u e st owo r kf o rmyc a s e s , b u tt h e ywo n ' ta p p l yt oe v e r yc a s e . Fo re x a mp l e , o t h e rs y s t e ms( * c o u g h *NDS)wi l l h a v ed i f f e r e n tCPUa r c h i t e c t u r e sa n dme mo r yc h a r a c t e r i s t i c s , a n dt h a two u l da f f e c t t h es p e e d . 22.6.5. Colored text on a dialog window. Th es i t u a t i o nd e p i c t e di nf i g2 2 . 1 3s h o u l db ef a mi l i a r .Th ek e yp o i n th e r ei st h a tt h e r ei sa b a c k g r o u n dma p , a n dad i a l o gb o xwi t ht e x ti ni t .Th i st e x ti ss t a t i c , b u tt h ep o s i t i o ni nt h e t o p l e f tc o r n e ri sc o n t i n u a l l yu p d a t e da sy o us c r o l l a l o n gt h ema p . Th ec o r ef u n c t i o nf o rt h i sd e moi stest_tte_chr4().Th ef i r s tt h i n gi td o e si sc a l l tte_init_chr4c()t oi n i t i a l i z et h et e x ts y s t e mf o rc h r 4 c mo d e .Th et h i r da r g u me n ti s t h eo f f s e tf o rma p e n t r i e s : 0xF000me a n i n gi tu s e ss u b p a l e t t e1 5 .Th ef o u r t hi sawo r d f o rt h ec o l o ra t t r i b u t e s : 1 3f o rt h ei n k , 1 5f o rt h es h a d o wa n d0f o rt h eo t h e r s .Fo rt h i s d e mo n s t r a t i o n , I ' mu s i n gt h e4 b p pv e r s i o no fv e r d a n a 9( s e ef i g2 2 . 1 2 )a n dt h ef a s ta s s e mb l y v e r s i o nt or e n d e rt h eg l y p h s . Fig 22.13: Te x to nt i l e s . St e p2l o a d st h eb a c k g r o u n dma pa n dt h ed i a l o gb o x .No t et h a tt h ed i a l o gb o xi sc o p i e d t ot h et i l e st h a tt h et e x ti sr e n d e r e dt o .Wh e nt h et e x ti sp r i n t e d , t h i swi l l s h o wt h a tt h eg l y p h si n d e e da r er e n d e r e dt r a n s p a r e n t l y .Th i s d o e smo r eo rl e s sme a nt h a tIc a n ' tu s et h es t a n d a r de r a s e r , b e c a u s et h a t ' dwi p et h eb o xa swe l l . Th ed i a l o gt e x ti sd r a wni ns t e p3 .Th ecia n dcst a g ss e tt h ei n ka n ds h a d o wc o l o ra t t r i b u t e s , r e s p e c t i v e l y .Th i sma k e st h es t r i n g “ a r r o ws ”u s ec o l o r s1a n d2( we l l , 0 x F1a n d0 x F2 ) , a n ds of o r t h . //! Set up a rectangle for text, with the non-text layers darkened for contrast. void win_textbox(uint bgnr, int left, int top, int right, int bottom, uint bldy) { REG_WIN0H= left<<8 | right; REG_WIN0V= top<<8 | bottom; REG_WIN0CNT= WIN_ALL | WIN_BLD; REG_WINOUTCNT= WIN_ALL; REG_BLDCNT= (BLD_ALL&~BIT(bgnr)) | BLD_BLACK; REG_BLDY= bldy; REG_DISPCNT |= DCNT_WIN0; file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 235/331 28-03-13 Tonc : GBA Programming in rot13 tte_set_margins(left, top, right, bottom); } //! Test chr4 shaded text renderer void test_tte_chr4() { irq_init(NULL); irq_add(II_VBLANK, NULL); REG_DISPCNT= DCNT_MODE0 | DCNT_BG0 | DCNT_BG2; // (1) Init for text tte_init_chr4c( 0, // BG number. BG_CBB(0)|BG_SBB(10), // BG control. 0xF000, // Screen-entry base bytes2word(13,15,0,0), // Color attributes. CLR_BLACK, // Ink color &verdana9_b4Font, // Verdana 9, with shade. (fnDrawg)chr4c_drawg_b4cts_fast); // b4cts renderer, asm version tte_init_con(); // Initialize console I/O // (2) Load graphics LZ77UnCompVram(dungeon01Map, se_mem[12]); LZ77UnCompVram(dungeon01Tiles, tile_mem[2]); LZ77UnCompVram(dungeon01Pal, pal_bg_mem); GRIT_CPY(&tile_mem[0][16*30], dlgboxTiles); GRIT_CPY(pal_bg_bank[15], dlgboxPal); // (3) Create and print to a text box. win_textbox(0, 8, 160-32+4, 232, 160-4, 8); CSTR text= "#{P}Scroll with #{ci:1;cs:2}arrows#{ci:13;cs:15}, " "quit with #{ci:1;cs:2}start#{ci:13;cs:15}\n" "Box opacity with #{ci:3;cs:4}L/R#{ci:7;cs:9}"; tte_write(text); // Reset margins for coord-printing tte_set_margins(8, 8, 232, 20); int x=128, y= 32, ey=8<<3; REG_BG2HOFS= x; REG_BG2VOFS= y; // Invisible map buildup! REG_BG2CNT= BG_CBB(2) | BG_SBB(12) | BG_REG_64x64; REG_DISPCNT= DCNT_MODE0 | DCNT_BG0 | DCNT_BG2 | DCNT_WIN0; while(1) { VBlankIntrWait(); key_poll(); // (4) Scroll and blend x = clamp(x + key_tri_horz(), 0, 512+1-SCREEN_WIDTH); y = clamp(y + key_tri_vert(), 0, 512+1-SCREEN_HEIGHT); ey= clamp(ey+ key_tri_shoulder(), 0, 0x81); REG_BG2HOFS= x; REG_BG2VOFS= y; REG_BLDY= ey>>3; // (5) Erase and print new position. tte_printf("#{es;P}%d, %d", x, y); if(key_hit(KEY_START)) break; } } I ' l l c l o s eo f ft h i ss e c t i o nwi t hawo r do nt h et e x tb o x .I fy o ul o o kc a r e f u l l y , y o u ' l l s e et h a ti t ' ss e mi t r a n s p a r e n t .Or , t ob ep r e c i s e , t h et e x t file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 236/331 28-03-13 Tonc : GBA Programming in rot13 a n dt h eb o xi t s e l fa r ea tn o r ma l i n t e n s i t y , b u tt h eb a c k g r o u n dt h a ti tc o v e r si sd a r k e rt h a nu s u a l .Twot h i n g sa r en e c e s s a r yf o rt h i sn i c e , l i t t l ee f f e c t . Ani n s i d ea n do u t s i d ewi n d o wmu s tb ed e f i n e d .Bo t hwi n d o wss h o u l dc o n t a i na l l l a y e r s , b u tt h ei n n e rwi n d o wmu s tb es e tt o u s eb l e n d i n g( WI N_BLD) .Th i se n a b l e sb l e n d i n gf o rt h ei n s i d eo n l y . Th eb l e n d i n gmo d es h o u l db es e tt of a d e t o b l a c k( BLD_BLACK)f o ra l l l a y e r se x c e p tt h eb a c k g r o u n dwi t ht h et e x tb o x . Th i si swh a twin_textbox()i sf o r .Th ef u n c t i o na l s os e t st h ema r g i n ss ot h a tt h et e x two u l dwr a pn i c e l yi n s i d et h eb o x . 22.7. Scripting, console IO and other niceties 22.7.1. TTE formatting commands Th eTTEc o n t e x tc o n t a i n sme mb e r st h a tc o n t r o l p o s i t i o n i n g , c o l o r s , f o n t sa swe l l a saf e wo t h e rt h i n g s .Th e r ea r et woa p p r o a c h e st o c h a n g i n gt h e s ep a r a me t e r s .Th ef i r s ti st oh a r dc o d ec h a n g e si nt h es t a t et h r o u g hd i r e c tme mb e ra c c e s so rf u n c t i o n sl i k e tte_set_ink().Th i swo r k sn i c ea n df a s t , b u ti s n ' tv e r yf l e x i b l e .Th es e c o n di st ou s eformatting tags i nt h es t r i n g st h e ms e l v e s– t h es y s t e mp a r s e st h es t r i n gf o rt h e s et a g sa n di n t e r p r e t st h e ma c c o r d i n g l y .Th i si sb a s i c a l l yaf o r mo fs c r i p t i n g . Th et a g st h a tTTEu s e sl o o kl i k et h i s : #{tag0:args; tag1:args} Th ec o d ei t s e l fi ss t a r t swi t h#̀{' a n de n d swi t h` }' .Ea c hc o mma n dc o n s i s t so fat a g , f o l l o we db yac o l o na n dc o mma s e p a r a t e d a r g u me n t swh e na p p r o p r i a t e .Mu l t i p l ec o mma n d sc a nb es e p a r a t e db yas e mi c o l o n .Fo re x a mp l e ,#̀{es; P:10,16}' wo u l dc l e a r t h es c r e e na n ds e tt h ec u r s o rt o( 1 0 , 1 6 ) . No w, Ic o u l ds h o wy o uh o wt op a r s et h i s , b u tt h ep a r s e rc u r r e n t l yi nu s ef o rt h i si s , we l l , l e t ' sj u s ts a yi t ' sl o n ga n dv e r yu g l y . Es s e n t i a l l y , i t ' sama s s i v es wi t c h b l o c k( s o me t i me sadouble s wi t c h b l o c k )wi t hs t u f fl i k et h i s : char *tte_cmd_default(const char *str) { int ch, val; char *curr= (char*)str, *next; TTC *tc= tte_get_context(); while(1) { ch= *curr; next= curr+1; // (1) Check first character switch(ch) { // (2) --- Absolute Positions --case 'X': tc->cursorX= curr[1]==':' ? strtol(curr+2, &next, 0) : tc->marginLeft; break; // If there's an argument ... // set cursor X to arg // else move to start of line. ... more cases ... // (3) Find EOS/EOC/token and act on it curr= tte_cmd_next(next); if(curr[0] == '\0') return curr; else if(curr[0] == '}') return curr+1; } } Li k eIs a i d , u g l y ;b u ti t ' l l h a v et od of o rn o w.Th ei n c o mi n gp o i n t e rp o i n t st ot h ef i r s tc h a r a c t e rp a s tt h e' #{' .Th ec o mma n dt a g sa r ea l l s i n g l eo rd o u b l e l e t t e r e d ;t h es wi t c hl o o k sf o rar e c o g n i z e dl e t t e ra n da c t sa c c o r d i n g l y . On eo ft h et a g si s' X' , wh i c hs e t st h ea b s o l u t eXc o o r d i n a t eo ft h ec u r s o r .Th etc->cursorXwi l l b es e tt ot h ea r g u me n ti fi ti s p r e s e n t , o rt ot h el e f tma r g i ni fi ti sn o t .No t et h eu s eo fstrtol()h e r e .Th i si sav e r yi n t e r e s t i n gf u n c t i o n .No to n l yd o e si two r kf o r b o t hd e c i ma l a n dh e xs t r i n g s , b u tt h r o u g ht h es e c o n da r g u me n ty o uc a nr e t r i e v eap o i n t e rt or i g h ta f t e rt h en u mb e ri nt h es t r i n g . Al t e r n a t i v e swo u l db esscanf()o ratoi(), b u tstrtol()i sn i c e r . Af t e rh a n d l i n gat a g , i t ' l l l o o kf o rmo r et a g s , o re x i ti ft h ee n dd e l i mi t e ro re n do fs t r i n gi sf o u n d . Ta b l e2 2 . 4s h o wst h ea v a i l a b l et a g s .No t et h a tt h e ya r ec a s e s e n s i t i v ea n ds o mei t e msc a nd omo r et h a no n et h i n g , d e p e n d i n go nt h e n u mb e ro fp a r a me t e r s . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 237/331 28-03-13 Tonc : GBA Programming in rot13 Code Description P Re s e tp o s i t i o nt ot o p l e f tma r g i n . Pr Re s t o r ec u r s o rp o s i t i o n( s e ea l s oPs) . Ps Sa v ec u r s o rp o s i t i o n . P: x, y Se tc u r s o rt oc o o r d i n a t e s( x, y) . X Re s e tcursorXt ol e f tma r g i n . X: x Se tcursorXt ox. Y Re s e tcursorYt ot o pma r g i n . Y: y Se tcursorYt oy. c [ i s p x ] : cattr Se ti n k( ci) , s h a d o w( cs) , p a p e r( cp)o rs p e c i a l ( cx)c o l o ra t t r i b u t et ocattr. e [ s l b f ] Er a s et h es c r e e nb e t we e nma r g i n s( es) , t h ec u r r e n tl i n e( el) , t h ec u r r e n tl i n e u pt ot h ec u r s o r( eb;b a c k wa r d s ) , t h ec u r r e n tl i n ef r o mt h ec u r s o r( ef; f o r wa r d s ) . e r : l, t, r, b Er a s ear e c t a n g l eg i v e nb y( l, t)t o( r, b) . f : idx Se tfontt oTTC.fontTable[idx]. m[ l t r b ] : value Se tl e f t( ml) , t o p( mt) , r i g h t( mr)o rb o t t o m( mb)ma r g i nt ovalue. m: l, t, r, b Se tma r g i n st or e c t a n g l e( l, t)-( r, b) p : dx, dy Mo v et h ec u r s o rb y( dx, dy) . s : idx Pr i n tt h eidx' t hs t r i n gi nTTC.stringTable. w: count Wa i tf o rcount f r a me s . x : dx Mo v et h ec u r s o rt ot h er i g h tb ydx. y : dy Mo v et h ec u r s o rd o wnb ydy. Table 22.4: Av a i l a b l eTTEf o r ma t t i n gt a g s . Is h o u l dp o i n to u tt h a ta tp r e s e n tt h ec o mma n d sa r es t i l l f r a g i l e , s ob ec a r e f u l wi t ht h i ss t u f f .Fo re x a mp l e , t h ep o s i t i o n i n gc o mma n d swi l l s i mp l ymo v et h ec u r s o r , b u tn o tc l i pt ot h ema r g i n s .Al s ot a k ec a r ewi t ht h ef o n ta n ds t r i n gc o mma n d s( fa n ds, r e s p e c t i v e l y ) . tte_cmd_default()d o e s n ' tt e s twh e t h e rt h ei n d e xi so u to ft h eb o u n d so ft h ea r r a y s , s oy o uc o u l de n du pwi t h… o d dt h i n g s .At s o mep o i n t , Ih o p et of i xt h e s et h i n g s , b u ti t ' sn o tap r i o r i t yr i g h tn o w.I fa n y o n eh a ss o me t h i n gmo r er o b u s tt h a tIc a nu s e , p l e a s es p e a k u p . TTE formatting commands : caveat emptor. Th ec u r r e n tc o mma n d si nTTEa r e n ' te x a c t l yi d i o t p r o o fy e t .I fy o us t i c kt os e n s i b l et h i n g s , i ts h o u l dwo r kq u i t en i c e l y .Bu ti t i ss t i l l e a s yt os h o o ty o u r s e l fi nt h ef o o ti fy o u ' r en o tc a r e f u l . 22.7.2. Using console I/O So me t h i n gl i k ette_write()i sn i c ef o rp u r es t r i n g s , b u twh a two u l dr e a l l yh e l pi si fy o uh a ds o me t h i n gl i k eprintf().I nt h eo l d d a y s( p r e 2 0 0 6 ) , printf(), putca n do t h e rc o n s o l eo u t p u tf u n c t i o n swe r eu n a v a i l a b l e , b u tWi n t e r mu t ea d d e dame c h a n i s mt o d e v k i t Ar m' ss t a n d a r dCl i b r a r yt h a ta l l o wsi to nc o n s o l e sa swe l l . Th ek e yt ot h i si st h edevoptab_ts t r u c t , d e f i n e di nsys/iosupport.h.Th i sc o n t a i n sat a b l eo ff u n c t i o np o i n t e r st od e v i c e o p e r a t i o n s .Th ep o i n t e rwe ' r ei n t e r e s t e di nh e r ei swrite_r;t h i si st h ef u n c t i o nt h a tprintf()e ta l .c a l l f o rt h ef i n a l o u t p u t . // Partial devoptab_t definition typedef struct { const char *name; int structSize; int (*open_r)(struct _reent *r, void *fileStruct, const char *path, int flags,int mode); int (*close_r)(struct _reent *r,int fd); int (*write_r)(struct _reent *r,int fd,const char *ptr,int len); ... } devoptab_t; Th ek e yt oma k i n gt h es t a n d a r dc o n s o l er o u t i n e swo r ko naGBAi st or e d i r e c tt h ed e f a u l twrite_rf o rc o n s o l eo u t p u tt oo n eo fo u r o wnma k i n g .Be f o r ee x p l a i n i n gh o wt h i swo r k s , Iwa n ty o ut ou n d e r s t a n dt h a tt h i sc o me sv e r yc l o s et ob l a c kma g i c .I ti n v o l v e s d e s c e n d i n gt ot h er o o t so ft h el i b r a r ya n dt h e r ei sn e x tt on od o c u me n t a t i o na b o u th o wt h i ss t u f fwo r k s .Th i ss t o r yi st h ec l o s e s tt h i n gI c o u l df i n dt oaf u l l d e s c r i p t i o n : h t t p : / / www. e mb e d d e d . c o m/ s t o r y / OEG2 0 0 2 0 1 0 3 S0 0 7 3 , b u tt h i si s n ' th i g ho ne x p l a n a t i o n se i t h e r . Top u ti ti no t h e rwa y : y o u ' r ei nac a v e ;i t ' sp i t c hb l a c ka n dt h e r ea r eg r u e sa b o u t . file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 238/331 28-03-13 Tonc : GBA Programming in rot13 No wt h a tt h a t ' sd o n e , l e t ' sc o n t i n u e .Th ef i r s ts t e pi sc r e a t i n go u rr e p l a c e me n twr i t e r .I nTTE' sc a s e , t h i si stte_con_write().I ti s a l mo s ti d e n t i c a l t otte_write(), b u th a st of i ti nt h ef o r ma tg i v e nb ydevoptab_t.write_r.I tc o me sd o wnt ot h i s : //! internal output routine used by printf. /*! \param r Reentrancy parameter. \param fd File handle (?). \param text Text buffer containing the string prepared by printf. \param len Length of string. \return Number of output bytes (?) \note \a text is NOT zero-terminated!!!!one! */ int tte_con_write(struct _reent *r, int fd, const char *text, int len) { // (1) Safety checks if(!sConInitialized || !text || len<=0) return -1; int ch, gid, charW; const char *str= text, *end= text+len; // (2) check for end of text while( (ch= *str) != 0 && str < end) { str++; switch(ch) { // (3) --- VT100 sequence ( ESC[foo; ) --case 0x1B: if(str[0] == '[') str += tte_cmd_vt100(str); break; //# (4) Other character cases. See tte_write() } } return str - text; } Wh i l eI ' v ea d d e dd o c u me n t a t i o nf o rt h ea r g u me n t sh e r e , i t ' smo s t l yb a s e do ng u e s s wo r k .Th erp a r a me t e rc o n t a i n sr e e n t r a n c y i n f o r ma t i o n , u s e f u l i fy o uh a v emu l t i p l et h r e a d s .Si n c et h eGBAi sas i n g l e t h r e a ds y s t e m, t h i ss h o u l dn o tc o n c e r nu s .Ib e l i e v efdi sa f i l eh a n d l eo fs o mes o r t , b u ts i n c ewe ' r en o twr i t i n gt of i l e st h i sa g a i nd o e sn o tc o n c e r nu s . Th er e a l a r g u me n t so fi n t e r e s ta r etexta n dlen.Th etexta r g u me n tp o i n t st ot h eb u f f e rwi t ht h es t r i n gt or e n d e r .I nt h ec a s eo f printf(), i t ' st h es t r i n gafter f o r ma t t i n g : a l l c o d e sl i k e%da r ea l r e a d yd o n e .An dn o wf o rt h emo s ti mp o r t a n tp a r t : texti snot n u l l t e r mi n a t e d .Th i si swh yt h e r e ' sal e n g t hv a r i a b l ea swe l l . Asf a ra sIc a nt e l l , printfu s e sal a r g eb u f f e r( a p p r o x i ma t e l y1 3 0 0b y t e s )o nt h es t a c kt owh i c hi twr i t e st h ef o r ma t t e dn u mb e r s . Th i sb u f f e ri s n ' tc l e a r e dy o uc a l l i ta g a i n , o rt e r mi n a t e db y‘ \ 0 ’wh e ns e n tt ot h ewr i t e r .Th i sh a st h ef o l l o wi n gc o n s e q u e n c e s : 1 3 0 0b y t e si saf a i rb i to fI WRAM.Ma k es u r ey o uh a v ee n o u g hr o o mf o ri t .Donot c a l l printf()f r o mi n t e r r u p t s , a st h e r o u t i n ei ss l o wa n dt h et h i n g sc a ns t a r tt on e s ta n dc l o b b e re v e r y t h i n g . Do n ' tf o r g e tt h elenp a r a me t e r .Ast h eb u f f e ri s n ' tz e r o e d , r e mn a n t so fo l dd a t ama ys t i l l b et h e r e , a n dy o ug e tc r a p . Th e r e ' sa na d d i t i o n a l p o t e n t i a l d a n g e rwi t hr e s p e c tt op a r s i n go ff o r ma t t i n gc o mma n d sh e r e .Wh e ns t r i n g se x c e e dt h eb u f f e r l e n g t h , Ii ma g i n et h a ti t ' sb r o k e nu pi n t os ma l l e rc h u n k s .Id o n ' tk n o wwh a twi l l h a p p e ni ft h eb r e a ko c c u r si nt h emi d d l eo fa c o mma n d , b u tId o u b ti t ' sg o o d .Ofc o u r s e , y o us h o u l d n ' th a v es t r i n g st h a tl o n ga n y wa y , a st h es c r e e ni s n ' tb i ge n o u g ht of i t t h e m. As i d ef r o mt h a t , tte_con_write()i ss t r a i g h t f o r wa r d .Ass a i d , t h ec o n t e n t so ft h el o o pa r en e a r l yi d e n t i c a l t ot h eo n ei n tte_write().Th eo n l yr e a l d i f f e r e n c ei sp o i n t3 .Th i si sat e s tf o rVT1 0 0f o r ma t t i n gs t r i n g s , wh i c hwi l l b ec o v e r e di nt h en e x t s u b s e c t i o n . Toma k eu s eo ft h en e wwr i t e r , y o uh a v et oh o o ki ti n t ot h ed e v i c el i s ts o me h o w.Fi r s t , c r e a t eadevoptab_ti n s t a n c ewh i c ht h e wr i t e ri nt h er i g h tp l a c e .Th e r ei sal i s to fd e v i c eo p e r a t i o n sc a l l e ddevoptab_list.Th ed e v i c e so fi n t e r e s ta r et h es t r e a msstdout a n dstderr, wh i c ha r ee n t r i e sSTD_OUTa n dSTD_ERRi nt h el i s t .Si mp l yp o i n tt h e s ee n t r i e st oy o u ro wns t r u c t . As e c o n di t e mi st os e tt h eb u f f e r sf o rt h e s es t r e a ms .I ' mn o ts u r et h i si sr e a l l yn e c e s s a r y , b u tt h a t ' sh o wi t ' sd o n ei nl i b g b aa n di t s a u t h o rk n o wst h i ss y s t e mb e s ts oI ' mn o tg o i n gt oa r g u eh e r e .Th ef u n c t i o nf o rt h i si ssetvbuf().Yo uf i n dt h er e q u i r e di n i t i a l i z a t i o n s t e p sb e l o w. static int sConInitialized= 0; file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 239/331 28-03-13 Tonc : GBA Programming in rot13 const devoptab_t tte_dotab_stdout= { "ttecon", 0, NULL, NULL, tte_con_write, NULL, NULL, NULL }; //! Init stdio capabilities for TTE. void tte_init_con() { // attach our operations to stdout and stderr. devoptab_list[STD_OUT] = &tte_dotab_stdout; devoptab_list[STD_ERR] = &tte_dotab_stdout; // Set buffers. setvbuf(stderr, NULL , _IONBF, 0); setvbuf(stdout, NULL , _IONBF, 0); sConInitialized = 1; } Ca l l i n gtte_init_con()a c t i v a t e ss t d i o ' sf u n c t i o n a l i t ys oy o uc a nu s eprintf()a n ds u c h .No t et h a tt h er a wprintf()i s r a t h e rh e a v ya n di ta l s oh a sf l o a t i n gp o i n to p t i o n s , wh i c ha r er a r e l yu s e di naGBAe n v i r o n me n t , i fe v e r .Fo rt h a tr e a s o n , y o u ' l l u s u a l l y u s ei t si n t e g e r o n l yc o u s i n , iprintf().Al s on o t et h a tTTE' si mp l e me n t a t i o ni sdifferent f r o ml i b g b a ' s , a n dt h et wos h o u l dn o tb e c o n f u s e d .Fo rt h a tr e a s o n , I ' v eh i d d e nt h eiprintf()n a meb e h i n datte_printfma c r o . Th ef o l l o wi n gi sas h o r te x a mp l eo fi t su s e .I ' mu s i n gtte_printf()h e r e , b u tprintf()o riprintf()wo u l dh a v ewo r k e d j u s ta swe l l . #include <stdio.h> #include <tonc.h> int main() { REG_DISPCNT= DCNT_MODE0 | DCNT_BG0; // Init BG 0 for text on screen entries. tte_init_se_default(0, BG_CBB(0)|BG_SBB(31)); // Enable TTE's console functionality tte_init_con(); tte_printf("#{P:72,64}"); tte_printf("Hello World!"); // Goto (72, 64). // Print "Hello world!" while(1); return 0; } Printf bagage Aswo n d e r f u l a sprintf()i s , t h e r ea r es o med o wn s i d e st oi tt o o .Fi r s t , i t ' sav e r yh e a v yf u n c t i o nt h a tc a l l sq u i t eal a r g e a mo u n to ff u n c t i o n swh i c ha l l h a v et ob el i n k e di n .Se c o n d , i ti sp r e t t yd a mns l o w.Be c a u s ei tc a nd os omu c h , i th a st oc h e c k f o ra l l t h e s ed i f f e r e n tc a s e s .Al s o , f o rt h es t r i n gt od e c i ma l c o n v e r s i o ni tu s e sd i v i s i o n s , wh i c hi sr e a l l yb a df o rt h eGBA. Bea wa r eo fh o wmu c hprintf()c o s t s .I fi tt u r n so u tt ob eab o t t l e n e c k , t r yma k i n gy o u ro wns l i mme dd o wnv e r s i o n . Ad e c e n tsprintf()a l t e r n a t i v ei sposprintf(), c r e a t e db yDa nPo s l u n s . 22.7.3. VT100 escape sequences Ev e r yb o o ko nCwi l l t e l l y o ut h a ty o uc a np l a c et e x to nac o n s o l es c r e e n .Wh a tt h e yu s u a l l yd o n ' tt e l l y o ui st h a t , i ns o mee n v i r o n me n t s , y o uc a nc o n t r o l f o r ma t t i n ga swe l l .On es u c he n v i r o n me n ti st h eVT1 0 0 , wh i c hu s e descape sequences t oi n d i c a t ef o r ma t t i n g .Th e l i b r a r i e st h a td e v k i t Pr od i s t r i b u t e sf o rv a r i o u ss y s t e msu s et h e s es e q u e n c e s , s oi t ' sag o o di d e at os u p p o r tt h e ma swe l l . Th eg e n e r a l f o r ma tf o rt h ec o d e si st h i s : file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 240/331 28-03-13 Tonc : GBA Programming in rot13 CSI n1;n2 ... letter CSI h e r ei st h eASCI Ic o d ef o rt h ecommand sequence indicator, wh i c hi nt h i sc a s ei st h ee s c a p ec h a r a c t e r( 2 7 , 0 x 1 Bo r0 3 3 ) f o l l o we db y' [ ' .Th el e t t e ra tt h ee n dd e n o t e st h ek i n do ff o r ma t t i n gc o d e , a n dn1, n2 … a r et h ef o r ma t t i n gp a r a me t e r s .Wi k i p e d i ah a sa n i c eo v e r v i e wo ft h es t a n d a r ds e th e r ea n dt h e r e ' sa n o t h e ro n ea th t t p : / / l o c a l . wa s p . u wa . e d u . a u / ~p b o u r k e / d a t a f o r ma t s / v t 1 0 0 / .No t et h a t n o ta l l o ft h ec o d e sa r es u p p o r t e di nt h ed e v k i t Pr ol i b r a r i e s .Th eo n e sy o u ' l l e n c o u n t e rmo s ta r et h ef o l l o wi n g : ESC[ dyA Mo v ec u r s o ru pdy r o ws . ESC[ dyB Mo v ec u r s o rd o wndy r o ws . ESC[ dxC Mo v ec u r s o rr i g h tdx c o l u mn s . ESC[ dxD Mo v ec u r s o rl e f tdx c o l u mn s . ESC[ y; xH Se tc u r s o rt oc o l u mnx, r o wy. ESC[ 2 J Er a s es c r e e n . 0 .Er a s et oe n do fl i n e . 1 .Er a s et os t a r to fl i n e . 2 .Er a s ewh o l el i n e . ESC[ nK ESC[ y; xf AsESC[ y; xH ESC[ s Sa v ec u r s o rp o s i t i o n . ESC[ u Re s t o r ec u r s o rp o s i t i o n . Table 22.5: Co mmo nVT1 0 0s e q u e n c e s I fy o uc o mp a r et h i sl i s tt ot a b l e2 2 . 4 , y o u ' l l s e et h a tmo s to ft h e s ec o d e sh a v ec o r r e s p o n d i n gTTEc o mma n d s .Yo uc a nu s ee i t h e r , b u ti f y o up l a nt oma k es o me t h i n gt h a t ' ss u p p o s e dt ob ec r o s s p l a t f o r m, u s et h eVT1 0 0c o d e s . Deviations from the standard I ' mt r y i n gt ok e e pmyi mp l e me n t a t i o na sc l o s et ot h es t a n d a r da sp o s s i b l e .Th i si sma i n l yb e c a u s eTTEu s e so t h e rt h i n g sj u s t 8 x 8c h a r a c t e r so nar e g u l a rb a c k g r o u n d .I np a r t i c u l a r , s c r o l l i n gi sa b s e n th e r ea n dt h e r ea r en oc o l o rc o d e s .Ye t . 22.7.4. UTF-8 Yo uma yh a v eh e a r do fal i t t l et h i n gc a l l e dASCI I .Th i si s( o rwa s ;I ' mn o ts u r e )t h es t a n d a r de n c o d i n gf o rc h a r a c t e rs t r i n g s .Ea c h c h a r a c t e ri s1b y t el o n g , g i v i n g2 5 6n u mb e r sf o rl e t t e r s , n u mb e r se tc e t e r a .Fi g2 2 . 1a n df i g2 2 . 1 2c o n t a i nc h a r a c t e r3 2t o2 5 5 , a st h e y u s u a l l ya p p e a ro nWi n d o ws .ASCI Iwo r k sf i n ef o rWe s t e r nl a n g u a g e sb u ta r ec o mp l e t e l yi n a d e q u a t ef o rl a n g u a g e sl i k eJ a p a n e s e , wh i c h h a v et h o u s a n d so fc h a r a c t e r s .Tor e me d yt h i s , t h e yc a meu pwi t hUn i c o d e , wh i c hh a s1 6b i t sp e rc h a r a c t e r . Ani n t e r me d i a t eb e t we e nt h i si sUTF8 .Th i ss t i l l u s e s8 b i tc h a r a c t e r sf o rt h el o we r1 2 8ASCI Ic o d e s , b u tb y t e so v e r0 x 8 0d e n o t e t h es t a r to famu l t i b y t ec o d e , wh e r ei ta n daf e wo ft h ef o l l o wi n gc h a r a c t e r sf o r mas i n g l e , l a r g e rc h a r a c t e ro fu pt o2 1b i t s . UTF8i san i c ewa yo fh a v i n gy o u rc a k ea n de a t i n gi tt o o : y o uc a ns t i l l u s en o r ma l c h a r a c t e r sf o rLa t i nc h a r a c t e r s , me a n i n gi t ' l l s t i l l wo r kwi t hASCI Ip r o g r a ms , b u ty o ua l s oh a v eame t h o do fr e p r e s e n t i n gb i g g e rn u mb e r s . String (binary) Number (binary) Range (hex) 0 z z z z z z z 0 z z z z z z z 0 x 0 0 0 0 0 0-0 x 0 0 0 0 7 F( 7b i t ) 1 1 0 y y y y y1 0 z z z z z z 0 0 0 0 0 y y yy y z z z z z z 0 x 0 0 0 0 8 0-0 x 0 0 0 7 FF( 1 1b i t ) 1 1 1 0 x x x x1 0 y y y y y y1 0 z z z z z z x x x x y y y yy y z z z z z z 0 x 0 0 0 8 0 0-0 x 0 0 FFFF( 1 6b i t ) 1 1 1 1 0 www1 0 x x x x x x1 0 y y y y y y1 0 z z z z z z0 0 0 wwwx xx x x x y y y yy y z z z z z z0 x 0 1 0 0 0 0-0 x 1 0 FFFF( 2 1b i t ) Ta b l e2 2 . 6 . UTF8t ou 3 2c o n v e r s i o nt a b l e . Ta b l e2 2 . 6s h o wst h ec o n v e r s i o nwo r k s .I fab y t ei sl o we rt h a n1 2 8 , i t ' sas i mp l eASCI Ic h a r a c t e r .I fi t ' sh i g h e r , i tc a nf a l l i n t ot h r e e c l a s s e so fmu l t i b y t en u mb e r s .Th er a n g eo ft h eb y t ed e t e r mi n e st h en u mb e ro fb y t e sf o rt h ewh o l et h i n g ;o n c ey o uk n o wt h a t , y o un e e d t og r a bt h ea p p r o p r i a t eb i t p a t t e r n sf r o mt h e s eb y t e sa n dj o i nt h e mi n t oas i n g l en u mb e ra st h et a b l ei n d i c a t e s .Fo rmo r ed e t a i l s , Iwi l l r e f e ry o ut ot h ewi k i p e d i ap a g e . Be l o wy o uc a nf i n dar o u t i n et h a tr e a d sa n dd e c o d e sas i n g l eu t f 8c h a r a c t e rf r o mas t r i n g .Ye s , i t ' sac l u s t e r f * * ko fc o n d i t i o n s , b u t t h a t ' sn e c e s s a r yt oc h e c kwh e t h e ra l l t h ec h a r a c t e r sr e a l l yf o l l o wt h ef o r ma t ;a n di fi td o e s n ' t , i t ' l l i n t e r p r e tt h ef i r s tb y t eo ft h er a n g ea s a ne x t e n d e dASCI Ic h a r a c t e r .I fy o uwa n t , y o uc a no mi ta l l t h eìf((*src>>6)!=2) break;' s t a t e me n t s . //! Retrieve a single multibyte utf8 character. uint utf8_decode_char(const char *ptr, char **endptr) { uchar *src= (uchar*)ptr; uint ch8, ch32; // Poor man's try-catch. file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 241/331 28-03-13 Tonc : GBA Programming in rot13 do { ch8= *src; if(ch8 < 0x80) { ch32= ch8; } else if(0xC0<=ch8 && ch8<0xE0) { ch32 = (*src++&0x1F)<< 6; ch32 |= (*src++&0x3F)<< 0; } else if(0xE0<=ch8 && ch8<0xF0) { ch32 = (*src++&0x0F)<<12; ch32 |= (*src++&0x3F)<< 6; ch32 |= (*src++&0x3F)<< 0; } else if(0xF0<=ch8 && ch8<0xF8) { ch32 = (*src++&0x0F)<<18; ch32 |= (*src++&0x3F)<<12; ch32 |= (*src++&0x3F)<< 6; ch32 |= (*src++&0x3F)<< 0; } else break; // 7bit // 11bit if((*src>>6)!=2) break; // 16bit if((*src>>6)!=2) if((*src>>6)!=2) break; break; // 21bit if((*src>>6)!=2) if((*src>>6)!=2) if((*src>>6)!=2) break; break; break; // Proper UTF8 char: set endptr and return if(endptr) *endptr= (char*)src; return ch32; } while(0); // Not really UTF: interpret as single byte. src= (uchar*)ptr; ch32= *src++; if(endptr) *endptr= (char*)src; return ch32; } Bo t htte_write()a n dtte_write_con()u s eutf_decode_char()wh e nt h es t r i n gr e q u i r e si t .Th el a r g e rc h a r a c t e r sc a n b eu s e dt oa c c e s sl a r g e rf o n ts h e e t s .Yo uc o u l du s et h el a r g e rs h e e t sf o rb e t t e rl a n g u a g es u p p o r t , o rp e r h a p st oe x t e n dt h es t a n d a r ds e t o fc h a r a c t e r swi t ha r r o ws , a n do t h e rt y p e so fs y mb o l s . Th e r ei s , h o we v e r , o n ec a t c ht ou s i n gUTF8wi t hs t d i o .I n t e r n a l l y , s t d i oi sr e a l l yp i c k ya b o u twh a t ' sa c c e p t a b l e .Fo re x a mp l e , t h e c o p y wr i t es y mb o l , ©i se x t e n d e dn u mb e r0 x A9 .I nn o n UTF8 , y o uc o u l du s ej u s t0 x A9i nas t r i n ga n di t ' du s et h er i g h ts y mb o l . Ho we v e r , 0 x A9a l o n ewo u l d n ' tf i ta n yo ft h ef o r ma t sf r o mTa b l e2 2 . 6 , s oi t ' sa ni n v a l i dc o d ei nUTF8 .Wh i l e utf8_decode_char()i sf o r g i v i n gi nt h i sc a s e , s t d i oi s n ' t , a nwi l l i n t e r p r e ti ta sat e r mi n a t o r .I no t h e rwo r d s , b ec a r e f u l wi t h e x t e n d e dASCI Ic h a r a c t e r ;y o uhave t ou s et h ep r o p e rUTF8f o r ma t si fy o uwa n tt ou s et h es t d i of u n c t i o n s . Printf, UTF-8, and extended ASCII Aso fd e v k i t Ar mr 2 2 , printf()a n dt h eo t h e rs t d i of u n c t i o n su s et h eUTF8l o c a l e .Th i se f f e c t i v e l yme a n st h a ty o uc a n n o t u s ec h a r a c t e r sl i k e‘ ©’a n d‘ è ’d i r e c t l yl i k ey o uu s e dt oi no l d e rv e r s i o n s .Yo un e e dt ou s et h ef u l l mu l t i b y t eUTF8n o t a t i o n s . 22.7.5. Profiling the renderers I t ' sa l wa y sag o o di d e at os e eh o wf a s tt h et h i n g sy o uma k ea r e .Th i si sp a r t i c u l a r l yt r u ewh e nt h ef u n c t i o n sa r ec o mp l e x , l i k emo s to f t h eb i t ma pa n dt i l er e n d e r e r sa r e . Ta b l e2 2 . 7l i s t st h ec y c l e sp e rg l y p hf o rt h ema j o r i t yo ft h ea v a i l a b l er e n d e r e r s .Th e s eh a v eb e e nme a s u r e dwi t ht h es t r i n g( a n d l i b r a r yc o d e )i nROM wi t ht h ed e f a u l twa i t s t a t e s , u n d e rO2o p t i mi z a t i o n .Th ef o n tu s e dwa sv e r d a n a9 , wi t hh a sac e l l s i z eo f8 x 1 6 , me a n i n gi tc a nb eu s e df o rb o t hf i x e da n dv a r i a b l ewi d t h swi t he a s e .Th et e s ts t r i n gwa sa1 9 4c h a r a c t e rl i n ef r o mPo r t a l : “ Pl e a s en o t et h a tweh a v ea d d e dac o n s e q u e n c ef o rf a i l u r e .An yc o n t a c twi t ht h ec h a mb e rf l o o rwi l l r e s u l ti na n ' u n s a t i s f a c t o r y ' ma r ko ny o u ro f f i c i a l t e s t i n gr e c o r df o l l o we db yd e a t h .Go o dl u c k ! ” Renderer Cycles/char file:///H:/dev/gba/proj/tonc/bak/tonc-chrome.htm 242/331 28-03-13 Tonc : GBA Programming in rot13 n u l l 2 2 1 s e _d r a wg 5 9 5 s e _d r a wg _w8 h 1 6 3 7 0 a s e _d r a wg 7 7 3 a s e _d r a wg _w8 h 1 6 4 5 8 c h r 4 _d r a wg _b 1 c t s _b a s e 3 0 4 9 c h r 4 _d r a wg _b 1 c t s 2 0 4 4 c h r 4 _d r a wg _b 1 c t s _f a s t 6 3 1 b mp 8 _d r a wg _b 1 c t s _b a s e 2 8 7 5 b mp 8 _d r a wg _b 1 c t s 2 0 7 8 b mp 8 _d r a wg _b 1 c t s _f a s t 6 1 9 b mp 1 6 _d r a wg _b 1 c t s _b a s e 2 4 5 6 b mp 1 6 _d r a wg _b 1 c t s 1 5 0 3 o b j _d r a wg 4 2 3 Table 22.7: Re n d e r e rt i me s . Co n d i t i o n s : 1 9 4 c h a r s , v e r d a n a9 , ROM c o d e , d e f a u l twa i t s , O2 . Fi r s t , n o t et h eg r e a td i f f e r e n c e si nv a l u e s : f r o mh u n d r e d sf o rt h et i l e ma p sa n do b j e c t st othousands i nt h ec a s eo fb i t ma p sa n dt i l e r e n d e r e r s .An dt h i si sp e rc h a r a c t e r , s owr i t i n gl a r g es wa t so ft e x tc a nl e a dt os i g n i f i c a n ts l o wd o wn . Th enull()r e n d e r e ri sad u mmyr e n d e r e r , u s e dt of i n dt h eo v e r h e a do ft h eTTEs y s t e m.2 0 0i s n ' ta c t u a l l yt h a tb a d , a l l t h i n g s c o n s i d e r e d( r e me mb e r : ROM c o d e ) .Th a ts a i d , n o wc o mp a r et h i sn u mb e rt ot h er e g u l a rt i l e ma pt i me : t h eo v e r h e a di st a k e su pa s i g n i f i c a n tf r a c t i o no ft h et i meh e r e .Al s on