<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title> words/istore.asm </title>
    <meta name="keywords" content="amforth programming language Forth ATmega ATMEL">
    <meta name="description" content="amforth - laguage Forth for ATMEL ATmega">
    <!-- AUTOINCLUDE START "Page/Head.en.ihtml" DO NOT REMOVE -->
    <link rel="StyleSheet" href="../../../../Web/CSS/MLAB.css" type="text/css" title="MLAB Basic Style">
    <link rel="StyleSheet" href="../../../../Web/CSS/MLAB_Print.css" type="text/css" media="print">
    <link rel="shortcut icon" type="image/x-icon" href="../../../../Web/PIC/MLAB.ico">
    <script type="text/javascript" src="../../../../Web/JS/MLAB_Menu.js"></script>
    <!-- AUTOINCLUDE END -->
  </head>

  <body lang="en">

    <!-- AUTOINCLUDE START "Page/Header.en.ihtml" DO NOT REMOVE -->
    <!-- ============== HEADER ============== -->
    <div class="Header">
      <script type="text/javascript">
      <!--
        SetRelativePath("../../../../");
        DrawHeader();
      // -->
      </script>
      <noscript>
        <p><b> JavaScript is required for including of the header </b></p>
      </noscript>
    </div>
    <!-- AUTOINCLUDE END -->

    <!-- AUTOINCLUDE START "Page/Menu.en.ihtml" DO NOT REMOVE -->
    <!-- ============== MENU ============== -->
    <div class="Menu">
      <script type="text/javascript">
      <!--
        SetRelativePath("../../../../");
        DrawMenu();
      // -->
      </script>
      <noscript>
        <p><b> JavaScript is required for including of the menu </b><p>
      </noscript>
    </div>
    <!-- AUTOINCLUDE END -->

    <!-- ============== TEXT ============== -->
    <div class="Text">

      <h1> words/istore.asm </h1>

      <p>
        <input type=button onClick="history.back()" value="Back">
        <input type=button onClick="history.forward()" value="Forward">
        <a href="../WordList.en.html">Jump to Vocabulary</a>
      </p>

      <pre>
; ( n addr -- ) Memory
; R( -- )
; writes a cell in flash
VE_ISTORE:
    .db $02, &quot;i!&quot;,0
    .dw VE_HEAD
    .set VE_HEAD = VE_ISTORE
XT_ISTORE:
    .dw DO_COLON
PFA_ISTORE:
    .dw XT_INTOFF
    .dw XT_TO_R
    .dw XT_OVER
    .dw XT_OVER
    .dw XT_SPMBUF

    .dw XT_DUP
    .dw XT_SPMPAGELOAD

    ; an erase cycle is only necessary
    ; when changing a bit from 0 to 1
    .dw XT_OVER
    .dw XT_OVER
    .dw XT_IFETCH
    .dw XT_INVERT
    .dw XT_AND
    .dw XT_DOCONDBRANCH
    .dw PFA_ISTORE_WRITE
        .dw XT_DUP
        .dw XT_SPMERASE
PFA_ISTORE_WRITE:
    .dw XT_DUP
    .dw XT_SPMWRITE
    .dw XT_SPMRWW
    .dw XT_DROP
    .dw XT_DROP
    .dw XT_R_FROM
    .dw XT_INTRESTORE
    .dw XT_EXIT


; ( addr -- )
; R( -- )
; load the flash page of cell addr into write buffer, omitting addr itself
;VE_SPMPAGELOAD:
;    .db 11, &quot;spmpageload&quot;
;    .dw VE_HEAD
;    .set VE_HEAD = VE_INTSPMPAGELOAD
XT_SPMPAGELOAD:
    .dw DO_COLON
PFA_SPMPAGELOAD:
    .dw XT_DUP
    .dw XT_TO_R
    .dw XT_DOLITERAL
    .dw PAGEMASK
    .dw XT_AND
    .dw XT_DOLITERAL
    .dw PAGESIZE
    .dw XT_SWAP
PFA_SPMPAGELOAD1:
    .dw XT_OVER
    .dw XT_GREATERZERO
    .dw XT_DOCONDBRANCH
    .dw PFA_SPMPAGELOADDONE
    .dw XT_R_FROM
    .dw XT_DUP
    .dw XT_TO_R
    .dw XT_OVER
    .dw XT_NOTEQUAL
    .dw XT_DOCONDBRANCH
    .dw PFA_SPMPAGELOAD3
        .dw XT_DUP
        .dw XT_IFETCH
        .dw XT_OVER
        .dw XT_SPMBUF
PFA_SPMPAGELOAD3: ;( size addr -- )
    .dw XT_1PLUS
    .dw XT_SWAP
    .dw XT_1MINUS
    .dw XT_SWAP
    .dw XT_DOBRANCH
    .dw PFA_SPMPAGELOAD1
PFA_SPMPAGELOADDONE:
    .dw XT_DROP
    .dw XT_DROP
    .dw XT_R_FROM
    .dw XT_DROP
    .dw XT_EXIT

; ( spmcsr x addr -- )
; R( -- )
; execute spm instruction
;VE_DOSPM:
;    .db $03, &quot;(spm)&quot;
;    .dw VE_HEAD
;    .set VE_HEAD = VE_DOSPM
XT_DOSPM:
    .dw PFA_DOSPM
PFA_DOSPM:
  ; wait for pending spm instruction
PFA_DOSPM1:
    in temp1, SPMCR
    andi temp1, (1&lt;&lt;SPMEN)
    brne PFA_DOSPM1

PFA_DOSPM2:
    sbic EECR, EEWE
    rjmp PFA_DOSPM2

    ; address
    movw zl, tosl
    lsl zl
    rol zh
    ; value
    ld r0, Y+
    ld r1, Y+
    ; command
    ld temp0, Y+
    ld temp1, Y+
    ; spm timed sequence
    out SPMCR, temp0
    spm
    loadtos
    rjmp DO_NEXT

; ( x addr -- )
; R( -- )
; execute spm buf instruction
;VE_SPMBUF:
;    .db $06, &quot;spmbuf&quot;,0
;    .dw VE_HEAD
;    .set VE_HEAD = VE_SPMBUF
XT_SPMBUF:
    .dw DO_COLON
PFA_SPMBUF:
    .dw XT_TO_R
    .dw XT_TO_R
    .dw XT_DOLITERAL
    .dw (1&lt;&lt;SPMEN)
    .dw XT_R_FROM
    .dw XT_R_FROM
    .dw XT_DOSPM
    .dw XT_EXIT

; ( addr -- )
; R( -- )
; execute spm erase instruction
;VE_SPMERASE:
;    .db $08, &quot;spmerase&quot;,0
;    .dw VE_HEAD
;    .set VE_HEAD = VE_SPMERASE
XT_SPMERASE:
    .dw DO_COLON
PFA_SPMERASE:
    .dw XT_DOLITERAL
    .dw PAGEMASK
    .dw XT_AND
    .dw XT_TO_R
    .dw XT_DOLITERAL
    .dw (1&lt;&lt;PGERS|1&lt;&lt;SPMEN)
    .dw XT_ZERO
    .dw XT_R_FROM
    .dw XT_DOSPM
    .dw XT_EXIT

; ( spmcsr x addr -- )
; R( -- )
; execute spm write instruction
;VE_SPMWRITE:
;    .db $08, &quot;spmwrite&quot;,0
;    .dw VE_HEAD
;    .set VE_HEAD = VE_WPMWRITE
XT_SPMWRITE:
    .dw DO_COLON
PFA_SPMWRITE:
    .dw XT_DOLITERAL
    .dw PAGEMASK
    .dw XT_AND
    .dw XT_TO_R
    .dw XT_DOLITERAL
    .dw (1&lt;&lt;PGWRT|1&lt;&lt;SPMEN)
    .dw XT_ZERO
    .dw XT_R_FROM
    .dw XT_DOSPM
    .dw XT_EXIT

; ( -- )
; R( -- )
; re-enables rww section execute spm rww instruction
;VE_SPMRWW:
;    .db $05, &quot;spmrww&quot;
;    .dw VE_HEAD
;    .set VE_HEAD = VE_SPMRWW
XT_SPMRWW:
    .dw DO_COLON
PFA_SPMRWW:
    .dw XT_DOLITERAL
    .dw (1&lt;&lt;RWWSRE|1&lt;&lt;SPMEN)
    .dw XT_ZERO
    .dw XT_ZERO
    .dw XT_DOSPM
    .dw XT_EXIT

</pre>

      <p>
        <input type=button onClick="history.back()" value="Back">
        <input type=button onClick="history.forward()" value="Forward">
        <a href="../WordList.en.html">Jump to Vocabulary</a>
      </p>

    </div>

    <!-- AUTOINCLUDE START "Page/Footer.en.ihtml" DO NOT REMOVE -->
    <!-- ============== FOOTER ============== -->
    <div class="Footer">
      <script type="text/javascript">
      <!--
        SetRelativePath("../../../../");
        DrawFooter();
      // -->
      </script>
      <noscript>
        <p><b> JavaScript is required for including of the footer </b></p>
      </noscript>
    </div>
    <!-- AUTOINCLUDE END -->

  </body>
</html>