pbi-ide/help/BasicStampHelp/Content/LanguageTopics/Reference/MemoryOrg.htm

348 lines
19 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="6" MadCap:lastHeight="94" MadCap:lastWidth="853" MadCap:disableMasterStylesheet="true" MadCap:tocPath="PBASIC Language Reference" MadCap:InPreviewMode="false" MadCap:PreloadImages="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" lang="en-us" xml:lang="en-us" MadCap:PathToHelpSystem="../../../" MadCap:HelpSystemFileName="Default.xml" MadCap:SearchType="Stem">
<head><title>Memory Organization </title>
<link href="../../SkinSupport/MadCap.css" rel="stylesheet" />
<link href="../../Resources/Stylesheets/BSE_Help.css" rel="stylesheet" />
<script src="../../SkinSupport/MadCapAll.js" type="text/javascript">
</script>
</head>
<body>
<div class="MCBreadcrumbsBox_0"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="AlphaRef.htm">PBASIC Language Reference</a><span class="MCBreadcrumbsDivider"> &gt; </span><span class="MCBreadcrumbs">Memory Organization</span>
</div>
<h1>Memory Organization </h1>
<div class="ImagePlusCaption">
<div class="Col2">
<p>
<img src="../../graphics/pgm_icon1.gif" border="0" alt="BS1 icon" title="BS1 icon" />
<img src="../../graphics/pgm_icon2.gif" border="0" alt="BS2 icon" title="BS2 icon" />
<img src="../../graphics/pgm_icon2e.gif" border="0" alt="BS2e icon" title="BS2e icon" />
<img src="../../graphics/pgm_icon2sx.gif" border="0" alt="BS2sx icon" title="BS2sx icon" />
<img src="../../graphics/pgm_icon2p.gif" border="0" alt="BS2p icon" title="BS2p icon" />
<img src="../../graphics/pgm_icon2pe.gif" border="0" alt="BS2pe icon" title="BS2pe icon" />
<img src="../../graphics/pgm_icon2px.gif" border="0" alt="BS2px icon" title="BS2px icon" />
</p>
</div>
<p style="text-align: right;">&#160;</p>
<p>&#160;</p>
</div>
<p class="clear">&#160;</p>
<p>&#160;</p>
<p>(See also: <a href="Variables.htm" target="" title="" alt="" class="MCXref_0">Variables</a>)</p>
<p>&#160;</p>
<p class="PlainText">The BASIC Stamp has two kinds of memory; RAM (for variables used by your
program) and EEPROM (for storing the program itself). EEPROM may also be used to
store long-term data in much the same way that desktop computers use a hard drive
to hold both programs and files.</p>
<p class="PlainText">An important distinction between RAM and EEPROM is this:</p>
<ul>
<li value="1">RAM loses its contents when the BASIC Stamp loses power; when power returns,
all RAM locations are cleared to 0s.</li>
<li value="2">EEPROM retains the contents of memory, with or without power, until it is
overwritten (such as during the program-downloading process or with a
<a href="../Commands/WRITE.htm" target="" title="" alt="" class="MCXref_0">WRITE</a> instruction.)
</li>
</ul>
<h2>
<img align="absmiddle" src="../../graphics/bs1_inline.gif" border="0" />&#160; BASIC Stamp 1 RAM Organization</h2>
<p class="PlainText">The BS1 has 16 bytes (8 words) of RAM space arranged as shown in the table
below. The first word, called PORT, is used for I/O pin control. It consists of
two bytes, PINS and DIRS. The bits within PINS correspond to each of the
eight I/O pins on the BS1. Reading PINS effectively reads the I/O pins
directly, returning an 8-bit set of 1's and 0's corresponding to the high and
low state of the respective I/O pin at that moment. Writing to PINS will
store a high or low value on the respective I/O pins (though only on pins
that are set to outputs).</p>
<table align="center" width="60%" cellpadding="4" cellspacing="0" border="1">
<tr bgcolor="#CFCFCF" align="center" valign="top">
<td width="20%">Word Name</td>
<td width="20%">Byte Names</td>
<td width="20%">Bit Name</td>
<td width="40%">Special Notes</td>
</tr>
<tr valign="MIDDLE">
<td align="CENTER">PORT</td>
<td align="CENTER">PINS<br />DIRS</td>
<td align="CENTER">PIN0 - PIN7<br />DIR0 - DIR7</td>
<td>&#160;I/O pins; bit addressable<br /> &#160;I/O pins directions; bit addressable</td>
</tr>
<tr valign="MIDDLE">
<td align="CENTER">W0</td>
<td align="CENTER">B0<br />B1</td>
<td align="CENTER">BIT0 - BIT7<br />BIT8 - BIT15</td>
<td>&#160;Bit addressable<br />&#160;Bit addressable</td>
</tr>
<tr valign="MIDDLE">
<td align="CENTER">W1</td>
<td align="CENTER">B2<br />B3</td>
<td align="CENTER">&#160;</td>
<td>&#160;</td>
</tr>
<tr valign="MIDDLE">
<td align="CENTER">W2</td>
<td align="CENTER">B4<br />B5</td>
<td align="CENTER">&#160;</td>
<td>&#160;</td>
</tr>
<tr valign="MIDDLE">
<td align="CENTER">W3</td>
<td align="CENTER">B6<br />B7</td>
<td align="CENTER">&#160;</td>
<td>&#160;</td>
</tr>
<tr valign="MIDDLE">
<td align="CENTER">W4</td>
<td align="CENTER">B8<br />B9</td>
<td align="CENTER">&#160;</td>
<td>&#160;</td>
</tr>
<tr valign="MIDDLE">
<td align="CENTER">W5</td>
<td align="CENTER">B10<br />B11</td>
<td align="CENTER">&#160;</td>
<td>&#160;</td>
</tr>
<tr valign="MIDDLE">
<td align="CENTER">W6</td>
<td align="CENTER">B12<br />B13</td>
<td align="CENTER">&#160;</td>
<td>&#160;Used by <span class="keyword_in_text">GOSUB </span>instruction<br />&#160;Used by <span class="keyword_in_text">GOSUB </span>instruction</td>
</tr>
</table>
<p>&#160;</p>
<p class="PlainText">The second byte of PORT, DIRS, controls the direction of the I/O pins. Each
bit within DIRS corresponds to an I/O pin's direction. A high bit (1) sets the
corresponding I/O pin to an output direction and a low bit (0) sets the
corresponding I/O pin to an input direction.</p>
<p class="PlainText">The remaining words (W0 W6) are available for general-purpose use. Each word
consists of separately addressable bytes and the first two bytes (B0 and B1) are
bit addressable as well. You may assign other names (symbols) to these RAM registers
as shown in section "Defining and Using Variables", below.</p>
<p class="PlainText">When the BS1 is powered up, or reset, all memory locations are cleared to 0,
so all pins are inputs (DIRS = %00000000). Also, if the PBASIC program sets all
the I/O pins to outputs (DIRS = %11111111), then they will initially output low,
since the output latch (PINS) is cleared to all zeros upon power-up or reset, as
well.</p>
<h2>
<img align="absmiddle" src="../../graphics/bs2all_inline.gif" border="0" />&#160; BASIC Stamp 2 Series RAM Organization</h2>
<p class="PlainText">The BS2, BS2e, BS2sx, BS2p, BS2pe, and BS2px have 32 bytes of Variable RAM space arranged as
shown in the table below. Of these, the first six bytes are reserved for input,
output, and direction control of the I/O pins. The remaining 26 bytes are available
for general-purpose use as variables.</p>
<p class="PlainText"><b>Note</b>: There are 16 words, consisting of two bytes each for a total
of 32 bytes. All bits are individually addressable through modifiers and the bits
within the upper three words are also individually addressable through the
pre-defined names shown.</p>
<table align="center" width="80%" cellpadding="4" cellspacing="0" border="1">
<tr bgcolor="#CFCFCF" align="center" valign="top">
<td width="15%">Word Name</td>
<td width="15%">Byte Names</td>
<td width="15%">Nibble Names</td>
<td width="15%">Bit Name</td>
<td width="40%">Special Notes</td>
</tr>
<tr valign="MIDDLE">
<td align="CENTER">INS</td>
<td align="CENTER">INL<br />INH</td>
<td align="CENTER">INA, INB<br />INC, IND</td>
<td align="CENTER">IN0 - IN7<br />IN8 - IN15</td>
<td>&#160;Input pins</td>
</tr>
<tr valign="MIDDLE">
<td align="CENTER">OUTS</td>
<td align="CENTER">OUTL<br />OUTH</td>
<td align="CENTER">OUTA, OUTB<br />OUTC, OUTD</td>
<td align="CENTER">OUT0 - OUT7<br />OUT8 - OUT15</td>
<td>&#160;Output pins</td>
</tr>
<tr valign="MIDDLE">
<td align="CENTER">DIRS</td>
<td align="CENTER">DIRL<br />DIRH</td>
<td align="CENTER">DIRA, DIRB<br />DIRC, DIRD</td>
<td align="CENTER">DIR0 - DIR7<br />DIR8 - DIR15</td>
<td>&#160;I/O pin direction control</td>
</tr>
<tr valign="MIDDLE">
<td align="CENTER">W0</td>
<td align="CENTER">B0<br />B1</td>
<td>&#160;</td>
<td>&#160;</td>
<td>&#160;</td>
</tr>
<tr valign="MIDDLE">
<td align="CENTER">W1</td>
<td align="CENTER">B2<br />B3</td>
<td>&#160;</td>
<td>&#160;</td>
<td>&#160;</td>
</tr>
<tr valign="MIDDLE">
<td align="CENTER">W2</td>
<td align="CENTER">B4<br />B5</td>
<td>&#160;</td>
<td>&#160;</td>
<td>&#160;</td>
</tr>
<tr valign="MIDDLE">
<td align="CENTER">W3</td>
<td align="CENTER">B6<br />B7</td>
<td>&#160;</td>
<td>&#160;</td>
<td>&#160;</td>
</tr>
<tr valign="MIDDLE">
<td align="CENTER">W4</td>
<td align="CENTER">B8<br />B9</td>
<td>&#160;</td>
<td>&#160;</td>
<td>&#160;</td>
</tr>
<tr valign="MIDDLE">
<td align="CENTER">W5</td>
<td align="CENTER">B10<br />B11</td>
<td>&#160;</td>
<td>&#160;</td>
<td>&#160;</td>
</tr>
<tr valign="MIDDLE">
<td align="CENTER">W6</td>
<td align="CENTER">B12<br />B13</td>
<td>&#160;</td>
<td>&#160;</td>
<td>&#160;</td>
</tr>
<tr valign="MIDDLE">
<td align="CENTER">W7</td>
<td align="CENTER">B14<br />B15</td>
<td>&#160;</td>
<td>&#160;</td>
<td>&#160;</td>
</tr>
<tr valign="MIDDLE">
<td align="CENTER">W8</td>
<td align="CENTER">B16<br />B17</td>
<td>&#160;</td>
<td>&#160;</td>
<td>&#160;</td>
</tr>
<tr valign="MIDDLE">
<td align="CENTER">W9</td>
<td align="CENTER">B18<br />B19</td>
<td>&#160;</td>
<td>&#160;</td>
<td>&#160;</td>
</tr>
<tr valign="MIDDLE">
<td align="CENTER">W10</td>
<td align="CENTER">B20<br />B21</td>
<td>&#160;</td>
<td>&#160;</td>
<td>&#160;</td>
</tr>
<tr valign="MIDDLE">
<td align="CENTER">W11</td>
<td align="CENTER">B22<br />B23</td>
<td>&#160;</td>
<td>&#160;</td>
<td>&#160;</td>
</tr>
<tr valign="MIDDLE">
<td align="CENTER">W12</td>
<td align="CENTER">B24<br />B25</td>
<td>&#160;</td>
<td>&#160;</td>
<td>&#160;</td>
</tr>
</table>
<p>&#160;</p>
<p class="PlainText">The INS variable always shows the state of the I/O pins themselves, regardless
of the direction of each I/O pin. We call this, "reading the pins." If a pin was
set to an input mode (within DIRS) and an external circuit connected the I/O pin
to ground, the corresponding bit of INS would be zero. If a pin was set to be an
output and the pin's state was set to a high level (within OUTS), the corresponding
bit of INS would be high. If, however, that same pin was externally connected
directly to ground, the corresponding bit of INS would be low; since we're reading
the state of the pin itself and the BASIC Stamp cannot override a pin that is
driven to ground or 5 volts externally. Note: The final example is an error; it
will cause a direct short and can cause damage to the BASIC Stamp! Do not
intentionally connect output pins directly to Vss (Ground) or Vdd (positive supply)
or you risk destroying your BASIC Stamp module.</p>
<p class="PlainText">To summarize: DIRS determines whether a pin's state is set by external
circuitry (input, 0) or by the state of OUTS (output, 1). INS always matches the
actual states of the I/O pins, whether they are inputs or outputs. OUTS holds bits
that will only appear on pins whose DIRS bits are set to output.</p>
<p class="PlainText">In programming the BASIC Stamp, it's often more convenient to deal with
individual bytes, nibbles or bits of INS, OUTS and DIRS rather than the entire
16-bit words. PBASIC has built-in names for these elements, shown in table above.</p>
<p class="PlainText">Here's an example of what is described in the table above. The INS register is
16-bits (corresponding to I/O pins 0 through 15). The INS register consists of
two bytes, called INL (the Low byte) and INH (the High byte). INL corresponds to
I/O pins 0 through 7 and INH corresponds to I/O pins 8 through 15. INS can also be
thought of as containing four nibbles, INA, INB, INC and IND. INA is I/O pins 0
through 3, INB is I/O pins 4 through 7, etc. In addition, each of the bits of INS
can be accessed directly using the names IN0, IN1, IN2... IN15.</p>
<p class="PlainText">The same naming scheme holds true for the OUTS and DIRS variables as well.</p>
<p class="PlainText">As the table shows, the BASIC Stamp's memory is organized into 16 words of 16
bits each. The first three words are used for I/O. The remaining 13 words are
available for use as general-purpose variables.</p>
<p class="PlainText">Just like the I/O variables, the general-purpose variables have predefined
names: W0 through W12 and B0 through B25. B0 is the low byte of W0; B1 is the high
byte of W0; and so on through W12 (B24=low byte, B25=high byte). Unlike I/O
variables, there's no reason that your program variables have to be stuck in a
specific position in the BASIC Stamp's physical memory. A byte is a byte regardless
of its location. And if a program uses a mixture of variables of different sizes,
it can be a pain in the neck to logically dole them out or allocate storage.</p>
<p class="PlainText">More importantly, <b>mixing fixed variables with automatically allocated
variables is an invitation to bugs.</b> A
fixed variable can overlap an allocated variable, causing data meant for one
variable to show up in another! The fixed variable names (of the general-purpose
variables) are only provided for power users who require absolute access to a
specific location in RAM.</p>
<p class="PlainText">We recommend that you avoid using the fixed variables in most situations.
Instead, let PBASIC allocate variables as described in the next section. The
editor software will organize your storage requirements to make optimal use of
the available memory.</p>
<h2>
<img align="absmiddle" src="../../graphics/bsesxppepx_inline.gif" border="0">
</img>&#160; Scratch Pad RAM&#160;</h2>
<p class="PlainText">The BS2e, BS2sx, BS2p, BS2pe, and BS2px have some additional RAM called
Scratch Pad RAM. The BS2e and BS2sx have are 64 bytes of Scratch Pad RAM (0 63),
and the BS2p, BS2pe, and BS2px have 128 bytes of Scratch Pad RAM (0 127).
Scratch Pad RAM can only be accessed with the <span class="keyword_in_text">GET</span> and<span class="keyword_in_text"> PUT</span> commands (see the
<a href="../Commands/GET.htm" target="" title="" alt="" class="MCXref_0">GET</a> and <a href="../Commands/PUT.htm" target="" title="" alt="" class="MCXref_0">PUT</a> command descriptions
for more information) and cannot have variable names assigned to it.</p>
<p class="PlainText">The highest location in Scratch Pad RAM (location 63 on the BS2e and BS2sx,
location 127 on the BS2p, BS2pe, and BS2px) is read-only, and always contains
the number of the currently running program slot. This can be handy for programs
that need to know which program slot they exist in.</p>
<div class="Col2">
<div class="MasterFoot">
<p MadCap:conditions="BSEconditions.BSEWebHelp (Primary)-INCLUDE"><a href="../../HomeTopics/HomePage.htm">Go to Welcome page</a>
</p>
</div>
</div>
<div class="Col2">
<div class="MasterFoot">
<p style="text-align: right;"><span class="ContactInfoProjectName">BASIC Stamp Help</span> <![CDATA[ ]]><span class="ContactInfoVersion#">Version 2.5.4</span> <![CDATA[ ]]></p>
<p style="text-align: right;">Copyright ©&#160;<span class="ContactInfoCompanyName">Parallax Inc.</span></p>
<p style="text-align: right;"><span class="SystemShortDate">8/8/2012</span>
</p>
</div>
</div>
<script type="text/javascript">/* <![CDATA[ */
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-285614-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
/* ]]> */</script>
<script type="text/javascript" src="../../SkinSupport/MadCapBodyEnd.js">
</script>
</body>
</html>