Platform library for RETRO
Utils.cpp@0:6f26c31d8573, 2015-03-01 (annotated)
- Committer:
- Architect
- Date:
- Sun Mar 01 05:29:45 2015 +0000
- Revision:
- 0:6f26c31d8573
RetroPlatform Library for RETRO gaming system
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Architect | 0:6f26c31d8573 | 1 | /* |
Architect | 0:6f26c31d8573 | 2 | * (C) Copyright 2015 Valentin Ivanov. All rights reserved. |
Architect | 0:6f26c31d8573 | 3 | * |
Architect | 0:6f26c31d8573 | 4 | * This file is part of the RetroPlatform Library |
Architect | 0:6f26c31d8573 | 5 | * |
Architect | 0:6f26c31d8573 | 6 | * The RetroPlatform Library is free software: you can redistribute it and/or modify |
Architect | 0:6f26c31d8573 | 7 | * it under the terms of the GNU Lesser General Public License as published by |
Architect | 0:6f26c31d8573 | 8 | * the Free Software Foundation, either version 3 of the License, or |
Architect | 0:6f26c31d8573 | 9 | * (at your option) any later version. |
Architect | 0:6f26c31d8573 | 10 | * |
Architect | 0:6f26c31d8573 | 11 | * This program is distributed in the hope that it will be useful, |
Architect | 0:6f26c31d8573 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Architect | 0:6f26c31d8573 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Architect | 0:6f26c31d8573 | 14 | * GNU Lesser General Public License for more details. |
Architect | 0:6f26c31d8573 | 15 | * |
Architect | 0:6f26c31d8573 | 16 | * You should have received a copy of the GNU Lesser General Public License |
Architect | 0:6f26c31d8573 | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/> |
Architect | 0:6f26c31d8573 | 18 | * |
Architect | 0:6f26c31d8573 | 19 | * This library is inspired by Gamebuino Library (http://gamebuino.com) |
Architect | 0:6f26c31d8573 | 20 | * from Aurélien Rodot. |
Architect | 0:6f26c31d8573 | 21 | */ |
Architect | 0:6f26c31d8573 | 22 | #include "mbed.h" |
Architect | 0:6f26c31d8573 | 23 | #include "Utils.h" |
Architect | 0:6f26c31d8573 | 24 | |
Architect | 0:6f26c31d8573 | 25 | uint8_t constrain(uint8_t val, uint8_t minimum, uint8_t maximum ) |
Architect | 0:6f26c31d8573 | 26 | { |
Architect | 0:6f26c31d8573 | 27 | if( val < minimum ) |
Architect | 0:6f26c31d8573 | 28 | return minimum; |
Architect | 0:6f26c31d8573 | 29 | if( val > maximum ) |
Architect | 0:6f26c31d8573 | 30 | return maximum; |
Architect | 0:6f26c31d8573 | 31 | |
Architect | 0:6f26c31d8573 | 32 | return val; |
Architect | 0:6f26c31d8573 | 33 | } |
Architect | 0:6f26c31d8573 | 34 | |
Architect | 0:6f26c31d8573 | 35 | uint8_t max(uint8_t val1, uint8_t val2 ) |
Architect | 0:6f26c31d8573 | 36 | { |
Architect | 0:6f26c31d8573 | 37 | if( val1 < val2 ) |
Architect | 0:6f26c31d8573 | 38 | return val2; |
Architect | 0:6f26c31d8573 | 39 | |
Architect | 0:6f26c31d8573 | 40 | return val1; |
Architect | 0:6f26c31d8573 | 41 | } |
Architect | 0:6f26c31d8573 | 42 | |
Architect | 0:6f26c31d8573 | 43 | uint8_t min(uint8_t val1, uint8_t val2 ) |
Architect | 0:6f26c31d8573 | 44 | { |
Architect | 0:6f26c31d8573 | 45 | if( val1 < val2 ) |
Architect | 0:6f26c31d8573 | 46 | return val1; |
Architect | 0:6f26c31d8573 | 47 | |
Architect | 0:6f26c31d8573 | 48 | return val2; |
Architect | 0:6f26c31d8573 | 49 | } |
Architect | 0:6f26c31d8573 | 50 | |
Architect | 0:6f26c31d8573 | 51 | #define IAP_LOCATION 0x1fff1ff1 |
Architect | 0:6f26c31d8573 | 52 | typedef void (*IAP_call)(unsigned int [], unsigned int []); |
Architect | 0:6f26c31d8573 | 53 | |
Architect | 0:6f26c31d8573 | 54 | IAP_call iap_entry = reinterpret_cast<IAP_call>(IAP_LOCATION); |
Architect | 0:6f26c31d8573 | 55 | unsigned int IAP_command[ 5 ]; |
Architect | 0:6f26c31d8573 | 56 | unsigned int IAP_result[ 5 ]; |
Architect | 0:6f26c31d8573 | 57 | |
Architect | 0:6f26c31d8573 | 58 | int write_eeprom( char *source_addr, char *target_addr, int size ) |
Architect | 0:6f26c31d8573 | 59 | { |
Architect | 0:6f26c31d8573 | 60 | IAP_command[ 0 ] = 61; |
Architect | 0:6f26c31d8573 | 61 | IAP_command[ 1 ] = (unsigned int)target_addr; // Destination EEPROM address where data bytes are to be written. This address should be a 256 byte boundary. |
Architect | 0:6f26c31d8573 | 62 | IAP_command[ 2 ] = (unsigned int)source_addr; // Source RAM address from which data bytes are to be read. This address should be a word boundary. |
Architect | 0:6f26c31d8573 | 63 | IAP_command[ 3 ] = size; // Number of bytes to be written. Should be 256 | 512 | 1024 | 4096. |
Architect | 0:6f26c31d8573 | 64 | IAP_command[ 4 ] = SystemCoreClock / 1000; // CPU Clock Frequency (CCLK) in kHz. |
Architect | 0:6f26c31d8573 | 65 | |
Architect | 0:6f26c31d8573 | 66 | iap_entry( IAP_command, IAP_result ); |
Architect | 0:6f26c31d8573 | 67 | |
Architect | 0:6f26c31d8573 | 68 | return ( (int)IAP_result[ 0 ] ); |
Architect | 0:6f26c31d8573 | 69 | } |
Architect | 0:6f26c31d8573 | 70 | |
Architect | 0:6f26c31d8573 | 71 | int read_eeprom( char *source_addr, char *target_addr, int size ) |
Architect | 0:6f26c31d8573 | 72 | { |
Architect | 0:6f26c31d8573 | 73 | IAP_command[ 0 ] = 62; |
Architect | 0:6f26c31d8573 | 74 | IAP_command[ 1 ] = (unsigned int)source_addr; // Source EEPROM address from which data bytes are to be read. This address should be a word boundary. |
Architect | 0:6f26c31d8573 | 75 | IAP_command[ 2 ] = (unsigned int)target_addr; // Destination RAM address where data bytes are to be written. This address should be a 256 byte boundary. |
Architect | 0:6f26c31d8573 | 76 | IAP_command[ 3 ] = size; // Number of bytes to be written. Should be 256 | 512 | 1024 | 4096. |
Architect | 0:6f26c31d8573 | 77 | IAP_command[ 4 ] = SystemCoreClock / 1000; // CPU Clock Frequency (CCLK) in kHz. |
Architect | 0:6f26c31d8573 | 78 | |
Architect | 0:6f26c31d8573 | 79 | iap_entry( IAP_command, IAP_result ); |
Architect | 0:6f26c31d8573 | 80 | |
Architect | 0:6f26c31d8573 | 81 | return ( (int)IAP_result[ 0 ] ); |
Architect | 0:6f26c31d8573 | 82 | } |