📄 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"#include "playlist.h"#include "display.h"volatile xdata unsigned int rand16_seed;void restore_rand_settings(void){ rand16_seed = (int)read_param_1byte(PARM_RAND16_SEED_HI, 0x42) << 8 | read_param_1byte(PARM_RAND16_SEED_LO, 0x13); print("restore rand16 seed = "); print_hex16(rand16_seed); print_crlf(); rand16(); // always advance at least one in the sequence update_rand_seed_needed = 0;}void backup_rand_settings(void){ print("save rand16 seed = "); print_hex16(rand16_seed); print_crlf(); 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); update_rand_seed_needed = 0;}/* call this at "random" times... such as the moment the IDE disk * finishes spinning up. It takes the raw rapidly incrementing * value of timer2 and XORs the rand16_seed by it... so if this * is called at "unpredictable" times, the seed will change in a * random way */sfr at 0xCC TL2;sfr at 0xCD TH2;void tweak_rand_seed(void){#if 0 unsigned int x; x = (((unsigned int)TH2 << 8) | TL2); rand16_seed ^= x; print("Tweaking rand16_seed by "); print_hex16(x); print_crlf();#else _asm mov dptr, #_rand16_seed movx a, @dptr xrl a, _TL2 movx @dptr, a inc dptr movx a, @dptr xrl a, _TH2 movx @dptr, a _endasm;#endif}#pragma SAVE#pragma LESS_PEDANTICunsigned int rand16(void) {_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;}#pragma RESTORE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -