/*!
    \file    readme.txt
    \brief   description of the EEMUL functional demo

    \version 2026-07-01, V1.0.0, firmware for gd32f30x
*/

/*
    Copyright (c) 2026, GigaDevice Semiconductor Inc.

    Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this 
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, 
       this list of conditions and the following disclaimer in the documentation 
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors 
       may be used to endorse or promote products derived from this software without 
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.
*/

  This example is based on the GD32F303R-START board, it provides a description of how to
use the EEMUL (Flash-backed EEPROM emulation) middleware and demonstrates that it
works correctly through a sequence of self-checking functional scenarios. SysTick
is configured and the results of every test step are printed on EVAL_COM
(115200-8-N-1). Connect the board's USB-to-UART / COM port to a terminal to watch
the demo run.

  The demo builds a small device-parameter store (boot counter, mode, threshold,
calibration vector and firmware version) on top of EEMUL and exercises it the way
a real application would. It is the on-target counterpart of the host functional
test (tests/host/host_functional_test.c): several scenarios deliberately forge a
malformed block into a free Flash slot through the bound port operations to
reproduce torn writes and data corruption, then verify that EEMUL recovers safely.

  GD32F30x main Flash is word-programmable, so the EEMUL alignment is 4 bytes
(fmc_word_program). The block CRC is computed with the on-chip CRC unit, which on
GD32F30x is the classic fixed-function CRC (polynomial 0x04C11DB7, no reflection);
EEMUL only compares CRCs it computed on the same device, so this is sufficient.

  EEMUL configuration used by this demo (see eemul_config.h):
    - block header   : full (with CRC32)
    - program align  : 4 bytes (word)
    - flash page     : 1 KB
    - block mode     : sub-blocks
    - bad-block policy: retry
    - region         : two Flash pages reserved at the top of the main array

  Functional scenarios printed over the UART:
    S1  first boot + factory defaults : a blank region auto-formats, all
        parameters read as zero, then factory defaults are written via the
        batch API.
    S2  persistence across reboot     : a fresh init recovers the last committed
        values; the boot counter is bumped as on a real start-up.
    S3  differential (no-op) write    : writing an unchanged value commits no new
        block; writing a changed value advances the sequence.
    S4  wear / block rotation         : many sequential writes rotate across the
        region and the final value survives a reboot.
    S5  torn-write / power-loss       : a newer block is forged into a free slot
        with only its header magic programmed and the commit flag left erased,
        modelling an interrupted write; on recovery the unfinished block is
        rejected, the previous good value is restored, and the store stays writable.
    S6  payload tamper detection      : a newer block is forged into a free slot by
        cloning the committed block with a corrupted payload byte, so its stored
        CRC no longer matches; on recovery the block CRC rejects it and the
        previous good block is restored (full-header builds only).

  Each step prints "OK" or "FAIL"; every scenario ends with "=> PASS" or
"=> FAIL", and a final summary line reports how many scenarios passed. A correct
build prints PASS for all scenarios.

