📄 rand.c
字号:
/* * MP3 Player, Pseudo Random Numbers, http://www.pjrc.com/tech/mp3 * Copyright (c) 2000, PJRC.COM, LLC * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * As a specific exception to the GPL, the executable object code built * from this source code may be combined with hardware configuration data * files. A hardware configuration data file is a set of data that is * transmitted to an intergrated circuit that is not a general purpose * microprocessor or microcontroller, in order to establish its normal * operation. The process of combining the executable ojbect code built * from the GPL licensed source code with the hardware configuration data * shall be considered an aggregation of another work not based on the * Program. While the GPL does not restrict use of the program, any * use restriction associated with the hardware configuration data (for * example, that it only be used with particular hardware) shall apply * to the combined file which includes a copy of the hardware configuration * data. * * Contact: paul@pjrc.com */#include "as31glue.h"#include "main.h"#include "printf.h"#include "rand.h"#include "params.h"volatile xdata unsigned char rand8_seed;volatile xdata unsigned int rand16_seed;#define PARM_RAND8_SEED 2#define PARM_RAND16_SEED_HI 15#define PARM_RAND16_SEED_LO 16void restore_rand_settings(void){ rand16_seed = 0; rand8_seed = read_param_1byte(PARM_RAND16_SEED_HI, 0x42); rand16_seed = ((unsigned int)rand8_seed<<8); rand8_seed = read_param_1byte(PARM_RAND16_SEED_LO, 0x13); rand16_seed += rand8_seed; rand8_seed = read_param_1byte(PARM_RAND8_SEED, 0x5A);}void backup_rand_settings(void){ ibuf[0] = rand8_seed; write_flash_param(PARM_RAND8_SEED, ibuf); ibuf[0] = (unsigned char)(rand16_seed >> 8); write_flash_param(PARM_RAND16_SEED_HI, ibuf); ibuf[0] = (unsigned char)(rand16_seed & 255); write_flash_param(PARM_RAND16_SEED_LO, ibuf);}static void random_mode_change(){ if (play_mode == MODE_DIRONLY || play_mode == MODE_SEQNTIAL) { print("Random Off\r\n"); } else { print("Random On\r\n"); } rand16(); rand16(); rand16(); update_mode_needed = 1; param_restart_write_timer();}void random_mode_next(void){ if (play_mode >= MODE_MAX) { play_mode = MODE_MIN; } else { play_mode++; } random_mode_change();}void random_mode_prev(void){ if (play_mode <= MODE_MIN) { play_mode = MODE_MAX; } else { play_mode--; } random_mode_change();}unsigned char rand8(void) {_asm mov dptr, #_rand8_seed movx a, @dptr jnz rand8b cpl a movx @dptr, arand8b: anl a, #0xB8 mov c, p movx a, @dptr rlc a movx @dptr, a mov dpl, a_endasm;}unsigned char rev_rand8(void) { // I dunno how the handling of zero as a seed is going to work here... // -ZSB 29-Aug-2001 9:09 PM_asm mov dptr, #_rand8_seed movx a, @dptr jnz revrand8b cpl a movx @dptr, arevrand8b: push b mov b, a ; Store the current seed since that is what we actually want to send back ; Then we calculate the previous seed & put it back in rand8_seed anl a, #0x71 mov c, p movx a, @dptr rrc a movx @dptr, a mov dpl, b ; Here we return the stored old seed value pop b_endasm;}unsigned int rev_rand16() { unsigned int tmp = rand16_seed;_asm mov dptr, #_rand16_seed movx a, @dptr jnz revrand16b inc dptr movx a, @dptr jnz revrand16b cpl a movx @dptr, arevrand16b: mov dptr, #_rand16_seed movx a, @dptr anl a, #0x11 mov b, a inc dptr movx a, @dptr anl a, #0xA0 orl a, b mov c, p movx a, @dptr rrc a movx @dptr, a mov dptr, #_rand16_seed movx a, @dptr rrc a movx @dptr, a _endasm; return tmp;}unsigned int rand16() {_asm mov dptr, #_rand16_seed movx a, @dptr jnz rand16b inc dptr movx a, @dptr jnz rand16b cpl a movx @dptr, arand16b: mov dptr, #_rand16_seed movx a, @dptr anl a, #0x08 mov b, a inc dptr movx a, @dptr anl a, #0xD0 orl a, b mov c, p mov dptr, #_rand16_seed movx a, @dptr rlc a movx @dptr, a mov b, a inc dptr movx a, @dptr rlc a movx @dptr, a mov dpl, b mov dph, a _endasm;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -