12 Pages

cpe229_lec17

Course: CPE 229, Spring 2009
School: Cal Poly
Rating:
 
 
 
 
 

Word Count: 6324

Document Preview

CPE 229 Course Notes: Lecture 17 Copyright: 2005 Bryan Mealy <a href="/keyword/assembly-language/" >assembly language</a> Programming and the PicoBlaze Microcontroller: An Overview Stepping up from RTL descriptions of circuits to <a href="/keyword/assembly-language/" >assembly language</a> and microcontrollers represents...

Register Now

Unformatted Document Excerpt

Coursehero >> California >> Cal Poly >> CPE 229

Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.

Course Hero has millions of student submitted documents similar to the one below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
CPE 229 Course Notes: Lecture 17 Copyright: 2005 Bryan Mealy <a href="/keyword/assembly-language/" >assembly language</a> Programming and the PicoBlaze Microcontroller: An Overview Stepping up from RTL descriptions of circuits to <a href="/keyword/assembly-language/" >assembly language</a> and microcontrollers represents the next logical step in your digital learning evolution. While RTL descriptions of circuits represented a nicely overview of the circuits operations, it is somewhat limiting in the context of making your circuit do something useful. Moving up the digital circuit hierarchy leaves us interacting with computers at the <a href="/keyword/assembly-language/" >assembly language</a> level. As you will see, working with <a href="/keyword/assembly-language/" >assembly language</a> s and microcontrollers presents the ultimate efficiency in digital circuit design. In this context, the word efficiency refers to the fact that we can implement many different designs using a single platform. More specifically, with a single piece of hardware, we can implement many different designs; the efficiency is provided by the flexibility of altering the effective functioning of the circuit by simply changing the firmware (as opposed to changing the hardware). This means that the number of possible designs presented by a single piece of hardware is infinite (a fact not possible in the world of analog circuits). This set of notes presents an overview of both the PicoBlaze microcontroller and <a href="/keyword/assembly-language/" >assembly language</a> concepts in general. While PicoBlaze is far from being the end-all device in the world of microcontrollers, it provides a viable option in presenting topics such as <a href="/keyword/assembly-language/" >assembly language</a> programming, basic computer architecture, and basic features commonly found in many of today s more functional microcontrollers. Bits to Mnemonics and Back Again A computer is nothing more than a device that sequentially executes a series of stored instructions. The individual instructions are used to control the various subsystems in the computer in such a way as to produce a meaningful result. When all is said and done, computers do nothing more than push around bits (1's and 0's). It should be no surprise that the instructions are nothing more than a set of bits that induce the computer to perform predefined operations. The computer instructions in a bit-pattern form are referred to as a machine language or machine code. As you could imagine, dealing with an endless stream of bits is overwhelming for the average human brain. The solution is to replace the machine language with <a href="/keyword/assembly-language/" >assembly language</a> . An <a href="/keyword/assembly-language/" >assembly language</a> is a simple upward translation of the machine language where the bit patterns that form the instructions are replaced by mnemonics. These assembly mnemonics are written such a way as to convey the purpose of the instruction as it relates to the function that it causes the computer hardware to perform. The upside of this translation bit to mnemonics is that the purpose of an instruction is much easier to envision and understand. The downside the bits-to-mnemonic translation is that the translation needs to be undone in order for the instructions to be executed on a computer. The translation from assembly code to machine code is accomplished by a software program referred to as an assembler. Controlling a specific computer architecture in such a way as to do something useful requires a specific machine language, and hence, <a href="/keyword/assembly-language/" >assembly language</a> , for that architecture. This means there are as many different <a href="/keyword/assembly-language/" >assembly language</a> s out there as there are computer architectures. Computers generally differ by the number and type of operations, the size of data they work with, and they amount and methods that the data is stored. From a high-level, computers are generally able to carry out essentially the same functionality, but they must do so within the limits of their underlying computer architecture. The basic functionality of a computer is exercised by using the associated <a href="/keyword/assembly-language/" >assembly language</a> . Programming and Language Levels The operation of computers is controlled by the bit patterns that make up the instructions: computers understand no other language. Although it is possible to write programs using the bit-patterns directly, it is generally not done this way. The methods used to program computers are generally broken into three levels: 1) machine code, 2) assembly code, 3) and higher-level languages. A description of these 1 CPE 229 Lecture 17 Notes approaches is shown below. A visual aid is provided in Figure 1. Machine Code: This is the lowest level of programming. A program written in machine code is nothing more than a set of 1 s and 0 s which are arrange in bit-patterns that reflect the operations that the underlying architecture perform. The good part about writing programs using machine code is that there is not need to use other software (not including a text editor) as a precursor to writing a program. The downside of this approach is that programs are completely unreadable. There probably was a day when all programs had to be written in machine code, but that was sometime in the prehistoric computer era. Although every program that is ever written eventually ends up as machine code, the programs never start that way. Unfortunately, some instructors still require that students be able to code programs using machine code which is nothing more than an indicator of the age of the instructor. <a href="/keyword/assembly-language/" >assembly language</a> : The next level up in the programming hierarchy from machine code is <a href="/keyword/assembly-language/" >assembly language</a> programming. In an <a href="/keyword/assembly-language/" >assembly language</a> , the bit-patterns that form the instructions are replaced by mnemonics that loosely indicate the purpose of the instruction. The upside of using <a href="/keyword/assembly-language/" >assembly language</a> programming over machine code is that mnemonics bring a level of understandability to the code. The downside, (if it can be considered one) is that you need another piece of software referred to as an assembler to translate the <a href="/keyword/assembly-language/" >assembly language</a> instructions to machine code. The assembler is rarely a complicated piece of software. The downside of <a href="/keyword/assembly-language/" >assembly language</a> programming is that every different computer architecture will necessarily have a different <a href="/keyword/assembly-language/" >assembly language</a> . Although writing code in different <a href="/keyword/assembly-language/" >assembly language</a> s is not that complicated once you know one <a href="/keyword/assembly-language/" >assembly language</a> , it does, however, have a slight learning curve. Higher Level Languages: The next step beyond <a href="/keyword/assembly-language/" >assembly language</a> programming is to use some type of higher level language (HLL). Because each <a href="/keyword/assembly-language/" >assembly language</a> instruction generally performs only a basic operation, <a href="/keyword/assembly-language/" >assembly language</a> programs can quickly become long (many lines of code) when relatively complex operations are required by the program. One possible solution to producing long programs is switching to coding the programs using a HLL. When a HLL is used, each line of code in the HLL can represent many lines of assembly code which leads to shorter and arguably more understandable programs. When a HLL is used, you must use a compiler to translate the HLL code into machine code. Most likely, the HLL code is first converted to assembly code before the final translation to machine code. Using a HLL has one distinct advantage over assembly code: once you know one HLL, you can write code for any architecture without know anything about the underlying <a href="/keyword/assembly-language/" >assembly language</a> . This effectively smooths out the learning curve for switching processors and makes you HLL code architecturally independent. The only downside of HLL is that the code is not as efficient as it would be if a human generated the assembly code. 2 CPE 229 Lecture 17 Notes (a) (b) (c) Figure 1: The visual choice to programming from a user's perspective. <a href="/keyword/assembly-language/" >assembly language</a> s: The Low-level Goodness Through the years, <a href="/keyword/assembly-language/" >assembly language</a> s have received some rather bad press. Most people who have worked with <a href="/keyword/assembly-language/" >assembly language</a> s find that constructing programs using <a href="/keyword/assembly-language/" >assembly language</a> s is tedious. This is because the programs tend to be long when they are actually performing some useful task. The length of assembly programs appear to be long because the programs are generally written with only one instruction per line of text in the source code. The comment that is missing from the long description is that <a href="/keyword/assembly-language/" >assembly language</a> is only tedious relative to programming in higher-level languages. But on the other hand, working at a low-level has several distinct advantages over using a higher-level language. Moreover, programming using a higher-level language without knowledge of the computer architecture that the code is intended to run on can in some cases be outright inefficient. Some of the many benefits of programming in <a href="/keyword/assembly-language/" >assembly language</a> are listed below. <a href="/keyword/assembly-language/" >assembly language</a> programming inherently provides an overview of the computer architecture. Therefore, writing programs using <a href="/keyword/assembly-language/" >assembly language</a> are a lesson in computer programming and computer architecture all in the package. <a href="/keyword/assembly-language/" >assembly language</a> programming essentially requires source code organizational techniques in order to produce viable (readable, understandable, maintainable) source code. The potential length of <a href="/keyword/assembly-language/" >assembly language</a> programs can be effectively controlled by using modular programming techniques. Learning and applying these techniques will help improve the quality of any source code that you write. <a href="/keyword/assembly-language/" >assembly language</a> programming can be used to ensure certain portions of the code operate efficiently. Even if you are primarily writing in a higher-level language, there may be portions of the code that can be coded by hand to make sure the machine code is as efficient as possible. There is a common argument that today s compilers are as efficient as a human (an intelligent one) programming in <a href="/keyword/assembly-language/" >assembly language</a> . If I were you, I would refuse to believe this; it sounds more like a marketing play for a compiler company than it does true science. Moreover, if you ve ever dealt with optimizing the code generating step in a writing a compiler, you ll understand the 3 CPE 229 Lecture 17 Notes limitations. And finally on this point, without doubt, there are some efficient compilers out there, but it is highly unlikely that they all fall into the good as a human category. <a href="/keyword/assembly-language/" >assembly language</a> programming helps the programmer develop a true appreciation for the higher-level languages. The more complicated the task, the more you ll want to move to a higherlevel language for bulk of your programming needs. <a href="/keyword/assembly-language/" >assembly language</a> programming builds character. Yes, recent research has proven this to be true in practically every known case. But to be fair, one of the drawbacks of using <a href="/keyword/assembly-language/" >assembly language</a> programming is alluded to in Figure 1. The fact is that each time you work with a different architecture, you ll need to learn a new <a href="/keyword/assembly-language/" >assembly language</a> . The upside of this downside is that learning a new <a href="/keyword/assembly-language/" >assembly language</a> , once you know at least one of them, is not that big of deal. The assorted bag of <a href="/keyword/assembly-language/" >assembly language</a> programming tricks transfers nicely between different flavors of <a href="/keyword/assembly-language/" >assembly language</a> . The way to avoid learning a new <a href="/keyword/assembly-language/" >assembly language</a> each time you switch platforms is to code in a higher-level language. The downside of this is that your code will not be as efficient and compilers are sometimes not as cost effective as their assembler counterparts. <a href="/keyword/assembly-language/" >assembly language</a> s Overview There are well over 5000 different microcontrollers out there in the real world which implies there are about the same number of different <a href="/keyword/assembly-language/" >assembly language</a> s. This number of course does not include the various proprietary microcontrollers and other projects that for one reason or another never were released to the general public. The question that should be asking yourself now is: With so many <a href="/keyword/assembly-language/" >assembly language</a> s out there, what are my odds of ever using the one we re about to learn? This is a good question. The answer is that you ll probably never see PicoBlaze <a href="/keyword/assembly-language/" >assembly language</a> ever again after our brief foray into the device. But here s the truth: if working with PicoBlaze represents your first experience in writing assembly code, you may initially find it challenging. If this is not your first <a href="/keyword/assembly-language/" >assembly language</a> experience, you ll find that most the knowledge and skills you developed working with previous <a href="/keyword/assembly-language/" >assembly language</a> s and architectures can be easily applied to PicoBlaze. It is for the reason that this document attempts to present a balance between <a href="/keyword/assembly-language/" >assembly language</a> programming in general and some of the specifics of PicoBlaze. What makes all <a href="/keyword/assembly-language/" >assembly language</a> s similar is that is that they all basically do the same thing: they manipulate bits. The only differences between any two <a href="/keyword/assembly-language/" >assembly language</a> s is exactly how the bits are pushed around and how the bits are stored. These two characteristics are governed by the instructions available to the programmer and the underlying hardware. To come up to speed quickly when learning a new <a href="/keyword/assembly-language/" >assembly language</a> , you simply need to understand the basic programming resources which will allow you to use them effectively. These two items are presented by the Programming Model and the Instruction Set. The full definitions of these items are given below. Programming Model: The programming model is the programmer s view of the machine: it shows what hardware resources are available for the programmer to use. These resources include registers and other types of memory. Another useful definition for the Programming Model is the set of registers that can be manipulated by using instructions in the associated instruction set. Instruction Set: The instruction set lists the set of operations that the hardware can perform under control of the programmer. One interesting point here is that there is no mention of the actual architecture of the device. There is also no mention of the external interface of the device. These are interesting points because they highlight the fact that the discussion of <a href="/keyword/assembly-language/" >assembly language</a> s generally means that we are abstracting our approach to the device to a higher level. The general thought here is that we are now going to be writing <a href="/keyword/assembly-language/" >assembly language</a> programs. The assumption that is generally made is that some other fine person has implemented the device 4 CPE 229 Lecture 17 Notes in some type of hardware setting and has setup the environment so that all we have to do is provide the working source code. Since this is not always the case, we ll be discussing some of the hardware details of the devices that are required to make your <a href="/keyword/assembly-language/" >assembly language</a> program run on the given hardware. Figure 2 shows the programming model for the PicoBlaze microcontroller. The figure shows sixteen general purpose registers (GPRs) , a program counter (PC), a stack pointer (SP), and two condition flags (zero and carry flag). Each of the listed registers is eight bits wide. The PicoBlaze instruction set has direct control over the sixteen general purpose registers but only indirect control over the other registers and resources listed in Figure 2. The instruction set could be considered another monster altogether. If this is your first experience with an <a href="/keyword/assembly-language/" >assembly language</a> , the instruction set can be intimidating. If you have dealt with <a href="/keyword/assembly-language/" >assembly language</a> before, the next thing you want to do is to scan through the available <a href="/keyword/assembly-language/" >assembly language</a> instructions to get a quick feel for the control it provides over the MCU. We ll be talking about the instruction set in an incremental manner and discuss instructions as we use them in the various examples presented. The entire instruction set is too long to list here but it can be found described in various levels in other references. Figure 2: PicoBlaze Programming Model. Although the programming model and the instruction set are all you basically need to get started programming, we need to briefly take a look at some of the lower level details. Figure 3(a) shows the basic computer as we ve grown to know and love while Figure 3(b) shows the PicoBlaze implementation of this model. As you ll soon see, PicoBlaze is sort of strange in the way it does things. The first thing to notice is that the memory in PicoBlaze can is only used to store instructions. In other words, there is no way to store data in PicoBlaze other than in the general purpose registers. The next thing to notice is that the I/O block is part of the CPU block and the exterior wrapper. PicoBlaze is referred to as a soft-core microcontroller due to the fact that the CPU is defined in VHDL code (the memory is defined in VHDL code also but we ll withhold these details for now). The I/O in the PicoBlaze implementation that you ll be working with is defined in both the PicoBlaze core and the exterior VHDL wrapper. Once again, you can do a lot of programming without knowing these details but at some point they will be of interest to you. (a) (b) Figure 3: The basic computer model (a) and a more specific PicoBlaze interpretation (b). 5 CPE 229 Lecture 17 Notes The PicoBlaze architecture is shown in Figure 4. The dotted line represents the entire PicoBlaze device. The two underlying boxes include the main PicoBlaze module and program memory. The program memory in this case is a ROM object defined in VHDL and initialized to contain the machine language associated with an <a href="/keyword/assembly-language/" >assembly language</a> program. The program memory supports 256 lines of code as implied by the eight address lines. Each instruction is sixteen bits wide as indicated by the output of the program memory. Further details describing the low-level details of the instruction can be found in the XAPP213 (the manual for PicoBlaze provided by Xilinx). Figure 4 also hints at some of the input and output details of PicoBlaze. The PicoBlaze module has separate 8-bit input and output lines. These signals work in conjunction with the eight PORT_ID and the READ and WRITE strobes. We ll be talking more in -depth look at I/O in a later set of notes. Also to be included in later notes are the Reset and Interrupt signals. Figure 4: One level down in the PicoBlaze hierarchy. PicoBlaze Assembly Programming Since PicoBlaze is a Xilinx product and is provided in the form of a soft-core (VHDL code), there is a strange and twisted path required to get a program up and running. Writing and running <a href="/keyword/assembly-language/" >assembly language</a> programs on PicoBlaze is a somewhat structured process. The steps listed below describe these requirements. A step-by-step description of the steps required in order to get a PicoBlaze up and running on actual hardware is provided in the Experiment 7 on the CPE-269B website. Write the PicoBlaze assembly code: This can be done using any text editor. The file must have a .psm file extension in order for the assembler to look at it. Assemble the code using the PicoBlaze assembler: At this point you have two apparent choices. You can either use the assembler provided by Xilinx or use SimPicoBlaze (an assemblersimulator-debugger). Full details regarding the PicoBlaze assembler can be found in the XAPP213 document. Details of SimPicoBlaze are provided with the package that is downloadable from the CPE-269B website. The trick here is that the assembler used by SimPicoBlaze is the same one as provided by Xilinx but in a different package. This assembler is modestly user friendly although the error reporting is rather lacking when compared to other assemblers. A successfully assembled file results in the generation of a VHDL file that defines a ROM object which contains the machine code for the instructions in the original program. Synthesize the PicoBlaze VHDL Module: This includes the ROM object generated during the assembly step as well as the VHDL code that defines the PicoBlaze Module. 6 CPE 229 Lecture 17 Notes Run your program on some hardware: You can run your programs on the hardware found in the Rm 20-121 and Rm 20-100 labs. The only other alternative to running programs on hardware is to use the SimPicoBlaze simulator. PicoBlaze <a href="/keyword/assembly-language/" >assembly language</a> Program Structure There are three basic parts to any <a href="/keyword/assembly-language/" >assembly language</a> program (in this case, PicoBlaze is no different): 1) comments, 2) assembler directives, and 3) the <a href="/keyword/assembly-language/" >assembly language</a> code. In essence, the only thing that really matters to having your program run is the <a href="/keyword/assembly-language/" >assembly language</a> code. This correctly implies that the comments and assembler directives are generally used to increase the readability, reusability, and understandability of the assembly code. You should make an effort to use them in this way. Comments are used for the exclusive purpose of transferring message from the text to other humans who may be reading your code. Keep in mind, there is nothing uglier than a program that contains no comments. A working piece of code is almost worthless without adequate comments. The first set of comments that should appear in an <a href="/keyword/assembly-language/" >assembly language</a> program is the title banner. This set of comments should contain information such as names of the programmer, revision history, and a description of the code that is found in the file. Comments should also be used to delineate and describe pertinent blocks of code. Comments should also be used to describe what the assembly code is doing. Each line of assembly code should contain a comment unless it is patently obvious what the code is doing. Assembler directives provide a means of communication between the programmer and the assembler. Any assembler you work with will have a different set of assembler directives. There are five or six associated with PicoBlaze, most of which are provided to increase the readability of your assembly code. We ll discuss most of the assembler directives in the context of the <a href="/keyword/assembly-language/" >assembly language</a> programming examples that follow. As for the readability of the code, please reference the PicoBlaze <a href="/keyword/assembly-language/" >assembly language</a> Style File which can be found in the Handout sections of BlackBoard (CPE 229) and on the CPE-269B website. This file will help you create <a href="/keyword/assembly-language/" >assembly language</a> programs that are nothing short of beautiful. If you follow the guidelines provided by this style file, your code will be readable, easy to understand, and evoke worldwide joy and celebration. You ll also be able to easily reuse your code for the various PicoBlaze laboratory experiments. And most important of all, if you follow these guidelines, your code will be naturally modular as all assembly code should be. Embedded System Programming As we get closer to talking about actual programming, let s describe the ultimate goal. Most <a href="/keyword/assembly-language/" >assembly language</a> programs end up in some embedded system. An embedded system is one that requires no outside user intervention in order for it to run properly. This means the system comes up into a working state and stays working for as long as the system remains powered. This definition nicely matches all the embedded systems that currently inundate your life. The reality is that these systems just sit there waiting to react to inputs or conditions from the outside world. This is the model we ll start out with in our first PicoBlaze assembly program and all the programs that follow. Try to keep in mind the basic embedded systems programming concepts as the following example programs are described. The reality is that many of the examples presented in the final portion of CPE 229 are somewhat not firmly attached to reality. In other words, the only purpose some of these examples portray is to describe some basic <a href="/keyword/assembly-language/" >assembly language</a> and embedded systems programming techniques. Please be patient and creative. PicoBlaze Example Programs 7 CPE 229 Lecture 17 Notes The example programs that follow will hopefully give you a basic understanding of PicoBlaze <a href="/keyword/assembly-language/" >assembly language</a> programs as well as a few of the options available in the assembler. This introduction is somewhat quick due to the fact we need to know certain things in order to complete the CPE 269 Lab Experiment 7. We ll be going a bit slower and including more detail in upcoming notes. The examples that follow are missing a title banner in an effort to save space. Example Program #1: Write a basic PicoBlaze program. Solution Program #1: The solution to this open-ended example is shown in Figure 5. Some notes and explanation regarding this solution are provided later. ;-------------------------------------------------------------------;- Assembler Directives ;-------------------------------------------------------------------ADDRESS 00 ; memory location of instruction data ;-------------------------------------------------------------------; ;-------------------------------------------------------------------DISABLE INTERRUPT ; prevent interrupts ; use a blank line for delineation ; main: OR s0,00 ; do nothing (nop) JUMP main ; go back to main loop ;-------------------------------------------------------------------- Figure 5: Solution to example Program #1. The line beginning with ADDRESS is an assembler directive that tells the assembler where to place the next section of assembly code. This is actually not required by PicoBlaze for the first portion of the code because the address defaults to location 00 . Note that 00 is used instead of 0 to help remind programmers that the assembler requires all constant values to be in hexadecimal form1. The DISABLE INTERRUT line is an actual PicoBlaze assembly instruction and represents the first instruction in the listed program. This instruction turns off the interrupt feature of the PicoBlaze device. The interrupt powers up disabled but it s generally considered good programming practice to explicitly disable it anyway. Don t worry about what exactly an interrupt is now as we ll be talking about interrupts in some later set of notes. The main portion of this program contains two lines of assembly code. The first line is an OR instruction that also contains a label . The main label is used by the assembler to generate an address associated with that instruction. The OR instruction directs the hardware to OR the hexadecimal value 00 with the contents of register s0 and store the result in register s0 (s0 s0 OR 00). This instruction effectively does nothing and is commonly known as a nop which is short for no operation . Many assembler languages have official nop instructions but PicoBlaze does not. The third and final instruction in the listed program is a JUMP instruction. This is an unconditional JUMP and directs the program control to continue at the address associated with the OR instruction. Transfer of program control is accomplished modifications to the program counter. The execution of this program is essentially an endless loop that executes the two instructions. Not too exciting. 1 While its common practice to use 0x notation to indicate hexadecimal formats, the PicoBlaze assembler does not support the option. All numbers written in PicoBlaze assembly code are interpreted as hexadecimal. 8 CPE 229 Lecture 17 Notes One last thing to notice about this program is that it appears somewhat organized. This organized appearance is a result of align the instruction mnemonics, aligning the comments, delineating the comments, and separating major blocks of code. You should strive to do this in all of your <a href="/keyword/assembly-language/" >assembly language</a> programs. This allows your programs to not only be good, but to look good also. Input and Output via PicoBlaze: For programs to do anything useful, they ll need to have contact with the outside world. The general model used here is that the device will input (read) some data from the outside world, process this data, and then output (write) some result to back to the outside world. There are several mechanisms used by microcontrollers use to do this. With PicoBlaze, there are specific instructions used to handle this which makes it a port mapped device2 . Not surprisingly, the instructions which handle input and output are the INPUT and OUTPUT instructions. The transfer of data between the device and the outside world are handled via ports which is effectively a resource sharing utility. For now, you can think of ports are registers and treat them as such. Specific input and output devices are addressed via the port_id signals (see Figure 4). In that the port_id is 8-bits, up to 28 (256) different devices can be accessed for both input and output. Each different input and output device will necessarily have a unique port address (configured in hardware). The particulars of how the devices are accessed are beyond the scope of this class. You ll learn how this is done in other classes and can ignore those details for now. Let s use these concepts in a program. Example Program #2: Write a PicoBlaze program that will read in the port associated with the switches, complement the switch data, and output the result to the port associated with the LEDs. Note that this program would be something similar to what you ll be programming in the lab with it input and output devices of switches and LEDs, respectively. Solution Program #2: The solution to this example is shown in Figure 6. Some notes and explanation regarding some of the more interesting features of the solution are listed below. ;-------------------------------------------------------------------;- Assembler Directives ;-------------------------------------------------------------------CONSTANT SWITCHES, 30 ; switches address = port 30 (input) CONSTANT LEDS, 0C ; switches address = port 0C (output) ; ADDRESS 00 ; memory location of instruction data ;-------------------------------------------------------------------; ;-------------------------------------------------------------------DISABLE INTERRUPT ; prevent interrupts ; main: INPUT s0, SWITCHES ; put switch status into register s0 XOR s0, FF ; complement switch data (trick) OUTPUT s0, LEDS ; write data to LEDs JUMP main ; main loop (do it again) ;-------------------------------------------------------------------- Figure 6: Solution to example Program #2. This program uses the assembler directive CONSTANT to provide a direct translation from the hexadecimal port address to something more meaningful to humans. This directive instructs the assembler to replace the listed word with the numerical value each time it occurs in the assembly source code (comments are ignored by the assembler). The actual port addresses are a function of the hardware and must be provided to you the programmer. 2 The I/O for any given processor can be specified as either port mapped or memory mapped. In memory mapped devices, I/O is treated exactly as a memory access (reading and writing). 9 CPE 229 Lecture 17 Notes In the main portion of code, the status of the switches is input from the switch port using the INPUT instruction. The associated data is placed into the s0 register. The switch data could have been imported to any of the general purpose registers but this program has arbitrarily put the data in register s0. The following XOR instruction is used to compliment the data in the s0 register. If you were to look through the PicoBlaze instruction set, you wouldn t find an instruction that is dedicated to a compliment function. As is typical when working with <a href="/keyword/assembly-language/" >assembly language</a> s, you need to be creative with the instruction set and find a way to obtain the functionality you want with the instructions that are provided to you. The use of the XOR function for a compliment is a standard <a href="/keyword/assembly-language/" >assembly language</a> programming trick. The OUTPUT instruction is used to output the data in the s0 register to the port associated with the LEDs. Note that the previous XOR instruction has already altered the original switch data. The JUMP instruction causes program control to unconditionally continue program execution at the instruction associated with the main label. Bit Masks Since microcontrollers are generally used to control various devices, it would make sense that these devices would be controlled by single bits. This is often the case. This requires extensive use of bit-masks in your assembly code to single out individual bits of a register or input port. The bit-masks, combined with the monitoring of the condition flags, allows the microcontroller to perform different functions based on the status of individual bits of designated data. In other words, the status of the condition flags is used to control program flow. The code fragment shown in below shows the application of a bit mask. ;-------------------------------------------------------------------;- Assembler Directives ;-------------------------------------------------------------------CONSTANT SWITCHES, 30 ; switches address = port 30 (input) CONSTANT LEDS, 0C ; switches address = port 0C (output) ; CONSTANT SW3_MASK, 08 ; mask bit 3 of 8-bit value; ; ADDRESS 00 ; memory location of instruction data ;-------------------------------------------------------------------; ;-------------------------------------------------------------------; The code listed below assumes that the button 7 is the far left of ; eight buttons; button 0 is the right-most button. An un-pressed ; button is considered to be in a zero state. ;-------------------------------------------------------------------DISABLE INTERRUPT ; prevent interrupts ; main: INPUT s0, switches ; grab switch status AND s0, SW3_MASK ; clear all but button 3 (mask button3) JUMP Z, codex ; jump if not pressed ; ... codex: ; Some other code here ; that does something useful ; ... JUMP main ; main loop ;-------------------------------------------------------------------- Figure 7: A piece of code showing a typical bit masking function. 10 CPE 229 Lecture 17 Notes In this code, the bit mask that is used is SW3_MASK. The value associated with SW3_MASK is hex 08. In other words, all of the bits in 0x08 are cleared except for bit-3 (the fourth bit from the right). The code in Figure 7 ANDs the mask value with the register containing the switch data. This has the effect of clearing all the bits in that register except for bit-3. Since an AND operation is being used, if the bit-3 value in the register is 0, the resulting value of the register after the operation will be zero. If the value in bit-3 is a 1, the resulting value in the register will be non-zero . At this point, we don t usually care what the value is, we only care if it is non-zero or not. This is because the AND operation sets a condition flag (the zero flag) based on the result of the AND operation. If the value in the register is 0 after the AND operation, the zero flag will be set. If any of the bits in the register are 1 after the operation, the result is non-zero and the zero flag will be 0. The purpose of this masking operation is to control the flow of instructions in the program. In all microcontrollers, PicoBlaze included, there are a set of program flow instructions that are based on the conditions of the carry and zero flags. Keep in mind that different instructions affect the condition flags in different ways. Any decent description of the instruction set includes how each individual instruction affects the flags. There is not secret knowledge required here; all processor are different and you need to check the instruction set in every case. The next example shows how this can be used in a program similar to what is expected in Experiment 7. Example Program #3: Write a PicoBlaze program that will read the input port associated with the switches. If button 0 is pressed, the complement of the switch data will be sent to the LEDs. Otherwise, if the button is not pressed, the switch data will be sent directly to the LEDs. Assume the button input equals 1 when the button is pressed. Assume button 0 is the least significant bit (LSB), or the left-most bit position of the button port input. Solution Program #3: The solution to Example Program #3 is shown in Figure 8. Some notes and fun facts regarding some of the more interesting features of the solution are listed after the solution. This program is similar to previous programs except it contains some conditional code. ;-------------------------------------------------------------------;- Assembler Directives ;-------------------------------------------------------------------CONSTANT SWITCHES, 30 ; switches address = port 30 (input) CONSTANT BUTTONS, 10 ; switches address = port 10 (input) CONSTANT LEDS, 0C ; switches address = port 0C (output) ; CONSTANT BTN0_MASK, 01 ; mask bit 0 of 8-bit value; ; ADDRESS 00 ; memory location of instruction data ;-------------------------------------------------------------------; ;-------------------------------------------------------------------DISABLE INTERRUPT ; prevent interrupts ; main: INPUT s0, SWITCHES ; grab switch status INPUT s1, BUTTONS ; grab button status AND s1, BTN0_MASK ; mask button 1 JUMP Z, out_data ; jump if button not pressed XOR s0, FF ; otherwise, compliment before jump ; out_data: OUTPUT s0, LEDS ; output to LEDs JUMP main ; main loop ;-------------------------------------------------------------------- Figure 8: Solution to example Program #3. 11 CPE 229 Lecture 17 Notes The button data is input to register s1. The AND instruction is used in conjunction with theBTN0_MASK bit mask to generate the status of the bit associated with button 0. The AND operation has the effect of setting the zero flag only when button 0 is zero (not pressed). If the button is not pressed, the zero flag is set to zero and program control is sent to the instruction associated with the out_data label. If the zero flag is not set, then a button must have been pressed when the button data was input. In this case, program control falls through to the XOR instruction under the JUMP instructions. The XOR instruction is used to complement the data as was done in the two previous example programs. The program outputs the data to the LED port for display. At this point, either the complimented or uncomplimented switch data is output the LED port. Irregardless of the form of the data that was output to the LEDs, control is always returned to the instruction associated with the main label and the programs runs continuously. As a final note, the JUMP instructions are one of the program control instructions available in the PicoBlaze instruction set. It is this form of control that allows your microcontroller to react to data inputs in a specified manner. One more thing to keep in mind is that these instructions are generally executed very fast. If you were to actually run the Example Program #3 in the hardware provided in lab, you would perceive no delay in pressing the button and viewing the correct output. Well Known <a href="/keyword/assembly-language/" >assembly language</a> Tricks There are a few well-known assembly tricks that you would know about. These essentially have to do with setting, toggling, and clearing bits in a register. Although many microcontrollers these days have bit-level operations, most do not. Having instructions such as these are handy but they make the instruction set larger than it really needs to be. One of the unstated requirements of <a href="/keyword/assembly-language/" >assembly language</a> programming is to be clever. In other words, you need to work with the instructions you have to do the operations you need. You won t always have the exact instruction you need every time but the instruction set should always have the functionality to eventually create the operation you looking for (although it may take a few instructions instead of just one instruction). The three methods used to set, toggle, and clear bits in <a href="/keyword/assembly-language/" >assembly language</a> land are summarized in Table 1. Many microcontrollers have instructions that do nothing. Instructions that do nothing are often required as a way to insert timing delays into the execution of your assembly code. These instructions are referred to as nops (short for no operation). PicoBlaze do not have a nop instruction but the same functionality (or lack there of) can be obtained by using other instructions in clever ways. Operation setting bits toggling bits clearing bits do nothing How to do it Logical OR with 1 Logical EXOR with 1 Logical AND with 0 many ways to do it Example OR XOR AND OR S1,0x02 S2,0x01 S3,0x7F S3,0x00 ; ; ; ; ; ; ; set bit-1 in S1 (no other bits are changed) toggle bit-0 in S2 (no other bits are changes) clear bit-7 in S3 (no other bits are changed) do nothing (aka nop) Table 1: <a href="/keyword/assembly-language/" >assembly language</a> tricks to die for. 12
Find millions of documents on Course Hero - Study Guides, Lecture Notes, Reference Materials, Practice Exams and more. Course Hero has millions of course specific materials providing students with the best way to expand their education.

Below is a small sample set of documents:

Cal Poly - CPE - 229
CPE 229 Course Notes: Lecture 18Copyright: 2005 Bryan MealyAssembly Stuff Stepping up from RTL descriptions of circuits to assembly language and microcontrollers represents the next logical step in your digital learning evolution. While RTL descri
Cal Poly - CPE - 229
CPE 229 Course Notes: Lecture 19Copyright: 2005 Bryan MealyThis set of notes represents the third lecture dealing with the PicoBlaze and its associated assembly language. In reality, based on the programming required by CPE 269 Experiment 7 and th
Cal Poly - CPE - 229
CPE 229 Course Notes: Lecture 20Copyright: 2005 Bryan MealyJoe the Wanderer Finds His Way Back Home This represents the fourth set of notes regarding assembly language programming and PicoBlaze architecture. With the combination of lab, lecture, a
Cal Poly - CPE - 229
CPE 229 Course Notes: Lecture 21Copyright: 2005 Bryan MealyBacking-Up a Step to I/O As you know by now, PicoBlaze is a soft-core microcontroller. This refers to the fact that, unlike other microcontrollers, I cant order a PicoBlaze integrated circ
Cal Poly - CPE - 229
CPE 229 Course Notes: Lecture 24Copyright: 2005 Bryan MealyI/O Stuff Without input and output capabilities, all the hardware and software that weve come to know as a computer would be useless. In other words, I/O is an important matter. As was sho
Cal Poly - EE - 242
Chapter 3, Problem 1. Determine Ix in the circuit shown in Fig. 3.50 using nodal analysis. 1 k Ix 9V + _ 2 k + _ 6V 4 kFigure 3.50 For Prob. 3.1.Chapter 3, Solution 1 Let Vx be the voltage at the node between 1-k and 4-k resistors.9 Vx 6 Vx V
Johns Hopkins - MECHENG - EN.530.215
Johns Hopkins - MECHENG - EN.530.215
Johns Hopkins - MECHENG - EN.530.215
Johns Hopkins - MECHENG - EN.530.215
Johns Hopkins - MECHENG - EN.530.215
Johns Hopkins - MECHENG - EN.530.215
Johns Hopkins - MECHENG - EN.530.215
Johns Hopkins - MECHENG - EN.530.215
Johns Hopkins - MECHENG - EN.530.215
Johns Hopkins - MECHENG - EN.530.215
Johns Hopkins - MECHENG - EN.530.215
N.C. State - BUS/ST - 350
Who is SafeNet?By, Curtis FrankieVital StatisticsSafeNet is the largest company exclusively focused on the protection of high-value information assets. Intellectual Property Rights Information and Identities Leadership position in high growth
Virginia Tech - ACIS - 1504
ACIS 1504 Thursday Feb.19 ACIS 21152/24 T Ch. 1-5 Test 1 7:00 PM 9:30 PMEconExam 1: Wednesday, February 25 (Chapters 4-8), 6PM, MCB 100 PSYCFeb 25 EXAM 1 Chapters 1, 2, 3, 4, 5, and 6
Virginia Tech - ECON - 2006
1Homework 3, Chapters 9-12 (3/20/09) DUE IN CLASS ON FRIDAY MARCH 27 Chapter 9 [1]The economic expansion that began in 1991 1. lasted approximately five years 2. lasted approximately twelve years 3. is still ongoing 4. was the longest expansion in
Virginia Tech - ECON - 2006
Lecture Notes, Part 2, Chapters 9-12 Chapter 9: Economic Fluctuations (3/17/09) The classical model: if left to its own devices, a free-market economy is self-correcting.How so? Prices and wages will adjust so that L= L f and Y= Y f . The interest
Virginia Tech - ACIS - 2115
Chapter 6Internal Control and Financial Reporting for Cash and Merchandising OperationsLearning Objective 1Distinguish service, merchandising, and manufacturing operations.Operating CycleThe series of activities that a company undertakes t
Virginia Tech - ACIS - 2115
Chapter 7Reporting and Interpreting Inventories and Cost of Goods SoldLearning Objective 1Describe inventory management goals.Inventory Management DecisionsThe primary goals of inventory managers are to: 2. ensure sufficient quantities of in
Arizona - ECON - 201
Chapter 26Chapter 13 Saving, Investment, and the Financial SystemWHATS NEW IN THE THIRD EDITION:The Case Study on &quot;The History of the U.S. Government Debt&quot; has been updated. There is a new In the News box on &quot;Finance in China.&quot;LEARNING OBJECTIVE
N.C. State - BUS - BUS370
N.C. State - BUS - BUS370
N.C. State - BUS - BUS370
N.C. State - BUS - BUS370
N.C. State - BUS - BUS370
N.C. State - BUS - BUS370
N.C. State - BUS - 370
N.C. State - BUS - 370
N.C. State - BUS - Bus350
BUS/ST 350Practice Problems for Exam 1ReilandMaterial covered: Course Pack: Lecture Unit 1, Lecture Unit 2 WARNING: these practice problems may not cover all the material for which you are responsible! Some of these practice problems require mo
N.C. State - BUS - Bus350
BUS/ST 350Exam 2 Practice ProblemsReilandMaterial covered on test: Lecture Unit 3, Lecture Unit 4 through middle p. 18. webassign homework: 4, 5, 6, 7. textbook chapters 1.6, 3.1-3.7, 4.1-4.3;. WARNING! These practice problems may not cover all
N.C. State - BUS - Bus350
BUS/ST 350 Exam 1 Sp 07 Page 1 VERSION 1BUS/ST 350 EXAM 1 Spring '07 Reiland Name_ Lab Section #_ ID#_INSTRUCTIONS: Print your name, lab section #, and ID# above. Note the statement at the bottom of this page that you must sign when you are finis
N.C. State - BUS - Bus350
BUS/ST 350 EXAM 3 Fall '07 ReilandNameLab Section #ID #INSTRUCTIONS: Write your name, lab section #, and ID# above. Note the statement at the bottom of this page that you must sign when you are finished with the exam. Supply the following info
N.C. State - BUS - Bus350
BUS/ST 350 Spring '07 Final Exam version 1page 1BUS/ST 350 FINAL EXAM Spring 2007 Reiland Name Lab Section # ID #INSTRUCTIONS: Write your name, lab section #, and ID# above. Note the statement at the bottom of this page that you must sign when
N.C. State - BUS - Bus350
BUS/ST 350 Spring '07 Exam 2 Version 1 page 1BUS/ST 350 EXAM 2 Spring '07 ReilandName Lab Section # ID #INSTRUCTIONS: Write your name, lab section #, and ID# above. Note the statement at the bottom of this page that you must sign when you are fi
N.C. State - BUS - 370
N.C. State - BUS - 370
N.C. State - BUS - 370
N.C. State - BUS - 370
UNC - BIOL - 45
UNC - BIOL - 45
UNC - BIOL - 45
N.C. State - MA - 103
MA 1031. Consider the digraph of jobs: A(3) C(5)QUIZ 4E(8)B(4)D(6)F(2)a) What is the length of the critical path for the project? b) Schedule the jobs on two processors using the priority list ABCDEF c) Schedule the jobs on two processor
N.C. State - MA - 103
MA 103QUIZ 1Consider the preference schedule given by # of voters 3 A B C D 4 A B D C 9 A C B D 9 B C D A 2 B A C D 5 B C A D 8 C D B A 3 C A D B 12 D C A B1. 2. 3. 4. 5. 6. 7. 8.How many voters are there? How many first place votes are neede
N.C. State - MA - 103
MA 103 TEST 2 1. Five suite mates are dividing a pizza using the lone divider method. The Chooser declarations are as follows: Chooser 1: {S1, S2}, Chooser 2:{S2, S3, S4}, Chooser 3: {S2}, Chooser 4: {S3, S5}. a) Show the results of this scheme. b) I
N.C. State - MA - 103
MA 103 1. Consider the following graph:QUIZ 3 B C DA EF G H Ia) Does the graph have an Euler circuit? If so, find one; if not, explain why b) Does the graph have an Euler path? If so, find one; if not, explain why. c) Find a Hamilton circuit.
N.C. State - MA121 - MA121
MA 121TEST 41. Compute 3 2 5 xa)x4dxb)e2dx2. Solve: a) y = x y , y = 5 when x = 0. b) 5 xy 3 y = y 3. Find the volume of the solid formed by revolving the region bounded by3 2y = 9 x 2 , y = 0, x = 3, x = 3 about the x -
N.C. State - MA121 - MA121
MA 121 TEST 1Please show all of your work in answering the following: 1. Solve for x: 2x - x 2 = -x-22. Use rules of exponents to give the exact value of 8 3 3. equation of the line that contains the points ( 3,7 ) and ( -1, -5 ). Find an4. S
N.C. State - MA121 - MA121
MA 121TEST 2Please show all of your work in answering the following: 1. Let f ( x ) = 2 x 9 x 60 x 6 a) Find the intervals on which f is increasing or decreasing. b) Identify all relative extrema. c) Find the intervals on which f is concave up
N.C. State - MA121 - MA121
MA 121TEST 31. Find the following antiderivatives: a)x34 3 x 2 + x 3 dxdxb) 5e3x2 dx xc)2 x22. Use integration by substitution to find a) (3 xx22 1)( x 3 x ) 5 dxb) 3xex 2 +1dxc)3 x 2 1 dxd) 3 x
UNC - BIOL - Biol45
UNC - BIOL - Biol45
N.C. State - EC201 - Ec201
CHAPTER 2THE ECONOMIC PROBLEM: SCARCITY AND CHOICEWhat is Economics? Economics is a social science that studies how people use scarce resources to satisfy unlimited wants.Scarcity and EconomicsEconomic questions arise because we always want mor
N.C. State - EC201 - Ec201
CHAPTER 3DEMAND, SUPPLY, AND MARKET EQUILIBRIUMDEMAND A demand curve shows the quantities demanded (of a good/ service) at different prices. To derive demand curve, we hold everything that affect demand constant, except for its price. The figure i
N.C. State - EC201 - Ec201
CHAPTER 6HOUSEHOLD BEHAVIOR &amp; CONSUMER CHOICEThe Budget ConstraintThe consumption choices made by households depends upon their consumption possibilities as well as their preferences. Households have limited budgets, which means they must choose
N.C. State - EC201 - Ec201
CHAPTER 7THE PRODUCTION PROCESS: THE BEHVIOR OF PROFIT MAXIMIZING FIRMSProfits &amp; Economic Costs Profit = Total Revenue - Cost= (Price* Quantity) Cost The term profit will from here on refer to economic profit. So whenever we say profit we reall
Cornell - ECON - 1110
Econ 101ANSWERS TO PROBLEM SET 1Professor Wissink Cornell University1.(a)l (L ) l )limes mp = S) Workers in limes limes L )labor ) 0 1 2 3 0 ) 5 5 ) 3 8 ) 1 9c (L ) c )coconuts mp = S) Workers in coconuts coconuts L )labor ) 0 1 2 3 0 ) 1 1
Cornell - ECON - 1110
Introductory Microeconomics ECON 1110 Homework Assignment #5 The homework assignment is due at the start of class on Tuesday, Nov 11. You are permitted to work in small groups, but you must submit your individual answers. Please be neat, or else you may n