Vous êtes sur la page 1sur 5

Introduction

Theworldssmallest32bitCPUwithGCCtoolchain.
TheZPUisasmallCPUintwoways:ittakesupverylittleresourcesandthearchitectureitselfissmall.ThelattercanbeimportantwhenlearningaboutCPUarchitecturesand
implementingvariationsoftheZPUwhereaspectsofCPUdesignisexamined.InacademiastudentscanlearnVHDL,CPUarchitectureingeneralandcompleteexercisesin
thecourseofayear.
ThecurrentZPUinstructionsetandarchitecturehasnotchangedforthelastcoupleofyearsandcanbeconsideredquitestable.Thereisalotofdiscussionaboutvarious
modificationstotheZPUarchitectureinthezylinzpumailinglist,butcurrentlynoactualmodificationsareplannedastheimprovementsthathavebeenidentifiedarerelatively
slight(<30%performance/sizeimprovement).
ThereareahandfulofimplementationsoftheZPU.Mostoftheseusuallyhavesomestrongpointsandthereissomemovementinthedirectionofconsolidatingimprovements
intoafewofficiallyrecommendedZPUimplementations.
ForthosethatareinterestedintheZylinZPU,Irecommendjoininguponthezylinzpumailinglistandparticipatinginthediscussionthere.Thezylinzpuisafriendlyplace
wherepeopleofdifferentskills,hardware,software,toolsmeettoexchangeideasabouttheZPUandmicroprocessorarchitectureingeneral.
Sincerely,
yvindHarboe
ZylinAS
License
TheprojectincludesHDL,GCCtoolchainandeCosHAL.
TheZPUhasaBSDlicensefortheHDLandGPLfortherest.ThisallowsuserstoimplementanyversionoftheZPUtheywantincommercialproducts,butifimprovements
aredonetothearchitectureassuch,thentheyneedtobecontributedback.
PerJan1.2008,ZylinhastheCopyrightfortheZPU,i.e.ZylinisfreetodecidethattheZPUshallhaveaBSDlicenseforHDL+GPLfortherest.
Features
Smallsize:(Seeperformancesummary)
Codesize80%ofARMThumb
GCCtoolchain(GDB,newlib,libstdc+)
eCosembeddedoperatingsystemsupport
Status
HDLworks
GCCtoolchainworks
eCosHALworks
...butthereisalongTODOlist
Expectchurnasweconvergeontoashorterlistofimplementations.
Downloadsourcecode
TheZPUHDLsourcecodeisavailableasaGITrepositoryfromhttp://repo.or.cz/w/zpu.git.YoucandownloadthelatestsourcecodeasasnapshotwithoutinstallingGIT.
PreviouslytheZPUrepositorywashostedasaCVSrepositoryatwww.opencores.org,butthatZPUCVSrepositoryisthereonlyforhistoricalreferenceatthispoint.Once
www.opencores.orggrowsaGIThostingservice,theplanistoreplicatetheGITrepositorythere.
TheGCCZPUtoolchainisavailablefromhttp://repo.or.cz/w/zpugcc.git.TheZPUGCCtoolchainisBIG(over100MBytes).
GIT
FormoreadvanceduseofGIT,youwillneedtohitthebooksandreadupontheGITdocumentation.
Thatsaid,youcanask"silly"newbiequestionsaboutGITonthezylinzpumailinglistandyoushouldreceivesomefriendlyproddingintherightdirectionw.r.t.findingreading
material.
Gettinghelpmailinglist
Theplacetogethelpisthezylinzpumailinglist
TheZPUisanopensourceprojectandifyoudemonstratethatyouhavemadeanefforttoreadthedocumentationandgoogled,thenyouwillnormallygetsomehelpfromthis
listifyouaskclearquestions.
Architecture
TheZPUisazerooperand,orstackbasedCPU.Theopcodeshaveafixedwidthof8bits.
Example:

IM 5 ; push 5 onto the stack


LOADSP 20 ; push value at memory location SP+20
ADD ; pop 2 values on the stack and push the result

Ascanbeseen,alotofinformationispackedintothe8bits,e.g.theIMinstructionpushesa7bitsignedintegerontothestack.
ThechoiceofopcodesisintimatelytiedtotheGCCtoolchaincapabilities.

/* simple program showing some interesting qualities of the ZPU toolchain */


void bar(int);
int j;
void foo(int a, int b, int c)
{
a++;
b+=a;
j=c;
bar(b);
}
foo:
loadsp 4 ; a is at memory location SP+4
im 1
add
loadsp 12 ; b is now at memory location SP+12
add
loadsp 16 ; c is now at memory location SP+16
im 24 ; j is at absolute memory location 24.
; Notice how the ZPU toolchain is using link-time relaxation
; to squeeze the address into a single no-op
store
im 22 ; the fn bar is at address 22
call
im 12
return ; 12 bytes of arguments + return from fn
Instructionset
AbasesetofinstructionsmustbeimplementedinRTL,buttherestmaybeimplementedasRTLorasmicrocode.Thisallowsatradeoffofcoresizevscodesizeand
performance.
TheinstructionsthatmaybeimplementedinRTLormicrocodearereferredtoasemulatedinstructions.Themicrocodeisincrt0.s.Theimplementationdetermineswhich
instructionsrunasmicrocode.
Alloperationsare32bitwide.
TODOIsthetablebroken?Fixit.
Name Opcode Description Definition
BREAKPOINT 00000000
Thedebuggersetsamemorylocationtothisvalue
tosetabreakpoint.OnceaJTAGlikedebugger
interfaceisadded,itwillbeconvenienttobeable
todistinguishbetweenabreakpointandan
illegal(possiblyemulated)instruction.
Noeffectonregisters
IM 1xxxxxxx
Pushes7bitsignextendedintegerandsetsthea
instructiondecodeinterruptmaskflag(IDIM).
IftheIDIMflagisalreadyset,thisinstructionshifts
thevalueonthestackleftby7bitsandstoresthe
7bitimmediatevalueintothelower7bits.
UnlessaninstructionislistedastreatingtheIDIM
flagspecially,itshouldbeassumedtoclearthe
IDIMflag.
Topusha14bitintegerontothestack,usetwo
consecutiveIMinstructions.
Ifmultipleimmediateintegersaretobepushed
ontothestack,theymustbeinterleavedwith
anotherinstruction,typicallyNOP.
pc <= pc + 1
idim <= 1
if (idim=0) then
sp <= sp - 1;
for i in wordSize-1 downto 7 loop
mem(sp)(i) <= opcode(6)
end loop
mem(sp)(6 downto 0) <= opcode(6 downto 0)
else
mem(sp)(wordSize-1 downto 7) <= mem(sp)(wordSize-8 downto 0)
mem(sp)(6 downto 0) <= opcode(6 downto 0)
end if

STORESP 010xxxxx
PopvalueoffstackandstoreitintheSP+xxxxx*4
memorylocation,wherexxxxxisapositive
integer.
LOADSP 011xxxxx
PushvalueofmemorylocationSP+xxxxx*4,
wherexxxxxisapositiveinteger,ontostack.
ADDSP 0001xxxx
AddvalueofmemorylocationSP+xxxx*4to
valueontopofstack.
PushPCtostackandsetPCto0x0+xxxxx*32.
EMULATE 001xxxxx
Thisisusedtoemulateopcodes.Seezpupgk.vhd
forlistofemulateopcodevaluesused.
zpu_core.vhdcontainsreferenceimplementations
oftheseinstructionsratherthanlettingtheZPU
executetheEMULATEinstruction
OnewaytoimproveperformanceoftheZPUisto
implementsomeoftheEMULATEinstructions.
PUSHPC emulated Pushesprogramcounterontothestack.
POPPC
0000
0100
PopsaddressoffstackandsetsPC
LOAD
0000
1000
Popsaddressstoredonstackandloadsthevalue
ofthataddressontostack.
Bit0and1ofaddressarealwaystreatedas0(i.e.
ignored)bytheHDLimplementationsandCcode
isguaranteedbytheprogrammingmodelneverto
use32bitLOADonnon32bitaligned
addresses(i.e.ifaprogramdoesthis,thenithasa
bug).
STORE
0000
1100
Popsaddress,thenvaluefromstackandstores
thevalueintothememorylocationoftheaddress.
Bit0and1ofaddressarealwaystreatedas0
PUSHSP
0000
0010
Pushesstackpointer.
POPSP
0000
1101
PopsvalueofftopofstackandsetsSPtothat
value.Usedtoallocate/deallocatespaceonstack
forvariablesorwhenchangingthreads.
ADD
0000
0101
Popstwovaluesonstackaddsthemandpushes
theresult
AND
0000
0110
Popstwovaluesoffthestackanddoesabitwise
and&pushestheresultontothestack
OR
0000
0111
Popstwointegers,doesabitwiseorandpushes
result
NOT
0000
1001
Bitwiseinverseofvalueonstack
FLIP
0000
1010
Reversesthebitorderofthevalueonthestack,
i.e.abc>cba,100>001,110>011,etc.
Theraisond'etreforthisinstructionismainlyto
emulateotherinstructions.
NOP
0000
1011
Nooperation,clearsIDIMflagassideeffect,i.e.
usedbetweentwoconsecutiveIMinstructionsto
pushtwovaluesontothestack.
PUSHSPADD 61
a=sp
b=popIntStack()*4
pushIntStack(a+b)
POPPCREL 57 setPc(popIntStack()+getPc())
SUB 49
inta=popIntStack()
intb=popIntStack()
pushIntStack(ba)
XOR 50 pushIntStack(popIntStack()^popIntStack())
LOADB 51
8bitloadinstruction.Reallyonlyherefor
compatibilitywithCprogrammingmodel.Alsoit
hasabigimpactonDMIPStest.
pushIntStack(cpuReadByte(popIntStack())&0xff)
STOREB 52
8bitstoreinstruction.Reallyonlyherefor
compatibilitywithCprogrammingmodel.Alsoit
hasabigimpactonDMIPStest.
addr=popIntStack()
val=popIntStack()
cpuWriteByte(addr,val)
LOADH 34
16bitloadinstruction.Reallyonlyherefor
compatibilitywithCprogrammingmodel.
pushIntStack(cpuReadWord(popIntStack()))
STOREH 35
16bitstoreinstruction.Reallyonlyherefor
compatibilitywithCprogrammingmodel.
addr=popIntStack()
val=popIntStack()
cpuWriteWord(addr,val)
LESSTHAN 36
Signedcomparison
a=popIntStack()
b=popIntStack()
pushIntStack((a<b)?1:0)
LESSTHANOREQUAL 37
Signedcomparison
a=popIntStack()
b=popIntStack()
pushIntStack((a<=b)?1:0)
ULESSTHAN 38
Unsignedcomparison
longa//longishere64bitsignedinteger
longb
a=((long)popIntStack())&INTMASK//
INTMASKisunsigned0x00000000ffffffff
b=((long)popIntStack())&INTMASK
pushIntStack((a<b)?1:0)
ULESSTHANOREQUAL 39
Unsignedcomparison
longa//longishere64bitsignedinteger
longb
a=((long)popIntStack())&INTMASK//
INTMASKisunsigned0x00000000ffffffff
b=((long)popIntStack())&INTMASK
pushIntStack((a<=b)?1:0)
EQBRANCH 55
intcompare
inttarget
target=popIntStack()+pc
compare=popIntStack()
if(compare==0)
{
setPc(target)
}else
{
setPc(pc+1)
}
NEQBRANCH 56
intcompare
inttarget
target=popIntStack()+pc
compare=popIntStack()
if(compare!=0)
{
setPc(target)
}else
{
setPc(pc+1)
}
MULT 41
Signed32bitmultiply
pushIntStack(popIntStack()*popIntStack())
DIV 53
Signed32bitintegerdivide.
a=popIntStack()
b=popIntStack()
if(b==0)
{
//undefined
}pushIntStack(a/b)
MOD 54
Signed32bitintegermodulo.
a=popIntStack()
b=popIntStack()
if(b==0)
{
//undefined
}
pushIntStack(a%b)
LSHIFTRIGHT 42
unsignedshiftright.
longshift
longvalX
intt
shift=((long)popIntStack())&INTMASK
valX=((long)popIntStack())&INTMASK
t=(int)(valX>>(shift&0x3f))
pushIntStack(t)
ASHIFTLEFT 43
arithmetic(signed)shiftleft.
longshift
longvalX
shift=((long)popIntStack())&INTMASK
valX=((long)popIntStack())&INTMASK
intt=(int)(valX<<(shift&0x3f))
pushIntStack(t)
ASHIFTRIGHT 43
arithmetic(signed)shiftleft.
longshift
intvalX
shift=((long)popIntStack())&INTMASK
valX=popIntStack()
intt=valX>>(shift&0x3f)
pushIntStack(t)
CALL 45
callprocedure.
intaddress=pop()
push(pc+1)
setPc(address)
CALLPCREL 63
callprocedurepcrelative
intaddress=pop()
push(pc+1)
setPc(address+pc)
EQ 46
pushIntStack((popIntStack()==popIntStack())?
1:0)
NEQ 47
pushIntStack((popIntStack()!=popIntStack())?
1:0)
NEG 48 pushIntStack(popIntStack())
Interrupts
TheZPUsupportsinterrupts.
Totriggeraninterrupt,theinterruptsignalmustbeasserted.TheZPUdoesnotdefineanyinterruptdisablingmechanism,thismustbeimplementedbytheinterruptcontroller
andcontrolledviamemorymappedIO.
InterruptsaremaskedwhentheIDIMflagisset,i.e.withconsecutiveIMinstructions.
TheZPUhasanedgetriggeredinterrupt.AstheZPUnoticesthattheinterruptisasserted,itwillexecutetheinterruptinstruction.Theinterruptsignalmuststayasserteduntilthe
ZPUacknowledgesit.
Whentheinterruptinstructionisexecuted,thePCwillbepushedontothestackandthePCwillbesettotheinterruptvectoraddress(0x20).
NotethattheGCCcompilerrequiresthreeregistersr0,r1,r2,r3forsomeratheruncommonoperations.These32registersaremappedtomemorylocations0x0,0x4,0x8,0xc.
Thedefaultinterruptvectorataddress0x20willloadthevalueofthesememorylocationsontothestack,call_zpu_interruptandrestorethem.
Seezpu/hdl/zpu4/test/interrupt/forCcodeandzpu/hdl/example/simzpu_interrupt.doforsimulationexample.
Customstartupcode(akacrt0.s)
Tominimizethesizeofanapplication,oneimportanttrickistostripdownthestartupcode.Thestartupcodecontainsmicrocodeforemulationofinstructionsthatmayneverbe
usedbyaparticularapplication,oraremaderedundantbecausetheinstructionsareimplementedinRTL.
ThestartupcodeisfoundintheGCCsourcecodeundergcc/libgloss/zpu,buttomakethestartupcodemoreavailable,ithasbeenduplicatedintozpu/sw/startup
OntheTODOlistisworktomakeiteasiertoreducecodesize.
TODOisthefollowingactuallyuseful?ifnotremoveorelaborate.
Tominimizestartupsize,seecodesizedemo.ThisisprettystandardGCCstuffandsimpleenoughonceyou'vebeenoveritacoupleoftimes.
Vectors
Address Name Description
0x000 Reset
1.WhentheZPUboots,thisisthefirstinstructiontobeexecuted.
2.ThestackpointerisinitialisedtomaximumRAMaddress
0x020 Interrupt Thisistheentrypointforinterrupts.
0x040
Emulated
instructions
Emulatedopcode34.Notethatopcode32andopcode33arenotnormallyusedtoemulateinstructionsasthesememoryaddressesarealready
usedbybootvector,GCCregistersandtheinterruptvector.

Vous aimerez peut-être aussi