Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
a3_decode
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
D7018E-SS-RUST
a3_decode
Commits
6095a746
Commit
6095a746
authored
7 years ago
by
Henrik Tjäder
Browse files
Options
Downloads
Patches
Plain Diff
Now codgen works
parent
f008f06b
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main.rs
+53
-32
53 additions, 32 deletions
src/main.rs
with
53 additions
and
32 deletions
src/main.rs
+
53
−
32
View file @
6095a746
...
@@ -145,45 +145,66 @@ static CODED: [u32; 132] =
...
@@ -145,45 +145,66 @@ static CODED: [u32; 132] =
///
///
/// Count the number of zeroes
/// Count the number of zeroes
///
///
fn
count_zero
(
num
:
u32
)
->
u32
{
fn
count_zero
(
mut
num
:
u32
)
->
u32
{
let
mut
count_zero
=
0
;
let
mut
n
=
32
;
let
mut
count_one
=
0
;
for
x
in
0
..
32
{
while
num
!=
0
{
if
num
%
2
==
0
{
if
num
&
1u32
==
1u32
{
count_zero
+=
1
;
n
-=
1
;
}
else
{
count_one
+=
1
;
}
}
num
<<
1
;
num
>>=
1
;
}
}
count_zero
^
count_one
n
}
}
///
///
/// Modify the seed for decoding
/// Modify the seed for decoding
///
///
///
///FUNCTION codgen(): UNSIGNED INTEGER;
/// LOCAL SIGNED INTEGER n;
/// LOCAL UNSIGNED INTEGER x, y;
/// BEGIN
/// n := [count the number of 0's in word "seed"];
/// x := [rotate "seed" left by 30 bits];
/// y := [shift "seed" right-ARITHMETIC by 6 bits];
/// seed := x XOR y XOR n;
/// RETURN( seed XOR 0x464b713e );
/// END;
fn
codgen
(
seed
:
&
mut
u32
)
->
u32
{
fn
codgen
(
seed
:
&
mut
u32
)
->
u32
{
let
n
:
u32
;
let
n
:
u32
;
let
(
x
,
y
):
(
u32
,
u32
);
let
(
x
,
y
):
(
u32
,
u32
);
n
=
count_zero
(
*
seed
);
let
local_seed
:
u32
=
seed
.clone
();
// Send away copy of seed
//n = count_zero(local_seed);
n
=
local_seed
.count_zeros
();
println!
(
"Number of zeroes: {}"
,
n
);
println!
(
"Seed:
\n
{:b}"
,
*
seed
);
//
println!("Seed:\n{:b}",
local_
seed);
// Inte OK än
// Inte OK än
x
=
seed
.rotate_left
(
2
);
x
=
local_seed
.rotate_left
(
30
);
println!
(
"{:b}"
,
x
);
// Stolen from K & H
//x = local_seed << 30 | ((local_seed>> 2) & 0x3FFFFFFF);
//println!("{:b}", x);
// Fungerade fint
// Fungerade fint
y
=
*
seed
>>
6
;
// Shift 6 to the right arithmetic
// Needs to be i32 to be arithmetic, testing wrapping_shr
y
=
((
local_seed
as
i32
)
>>
6
)
as
u32
;
//y = local_seed.wrapping_shr(6);
*
seed
=
x
^
y
^
n
;
*
seed
=
(
x
^
y
^
n
)
as
u32
;
(
*
seed
)
^
0x464b713e
(
*
seed
)
^
0x464b713e
}
}
///
///
...
@@ -191,7 +212,6 @@ fn codgen(seed: &mut u32) -> u32 {
...
@@ -191,7 +212,6 @@ fn codgen(seed: &mut u32) -> u32 {
///
///
fn
decode
(
coded
:
&
[
u32
],
plain
:
&
mut
[
u8
],
seed
:
&
mut
u32
)
->
u32
{
fn
decode
(
coded
:
&
[
u32
],
plain
:
&
mut
[
u8
],
seed
:
&
mut
u32
)
->
u32
{
let
x
=
3
;
let
x
=
3
;
x
x
...
@@ -204,29 +224,30 @@ fn main() {
...
@@ -204,29 +224,30 @@ fn main() {
let
mut
seed
=
0x3e944b9f
;
let
mut
seed
=
0x3e944b9f
;
//decode(&CODED, unsafe { &mut PLAIN }, &mut seed);
//decode(&CODED, unsafe { &mut PLAIN }, &mut seed);
//
codgen
(
&
mut
seed
);
println!
(
"Ran codgen, new seed: {:x}"
,
seed
);
let
mut
locseed
;
locseed
=
codgen
(
&
mut
seed
);
assert_eq!
(
locseed
,
0x891432f9
);
println!
(
"Ran codgen, new seed: {:x}"
,
locseed
);
locseed
=
codgen
(
&
mut
seed
);
println!
(
"Ran codgen, new seed: {:x}"
,
locseed
);
locseed
=
codgen
(
&
mut
seed
);
println!
(
"Ran codgen, new seed: {:x}"
,
locseed
);
locseed
=
codgen
(
&
mut
seed
);
println!
(
"Ran codgen, new seed: {:x}"
,
locseed
);
locseed
=
codgen
(
&
mut
seed
);
assert_eq!
(
locseed
,
0xce83d1b8
);
println!
(
"Ran codgen, new seed: {:x}"
,
locseed
);
}
}
/*
/*
# Group 1's Codeword Generator Subroutine (pseudocode)
# Group 1's Codeword Generator Subroutine (pseudocode)
# (remember: "seed" is a global variable, UNSIGNED INTEGER;
# (remember: "seed" is a global variable, UNSIGNED INTEGER;
# you may implement local variables in registers or on the stack;
# you may implement local variables in registers or on the stack;
# result returned in v0; preserve all except t regs)
# result returned in v0; preserve all except t regs)
#
#
# FUNCTION codgen(): UNSIGNED INTEGER;
# LOCAL SIGNED INTEGER n;
# LOCAL UNSIGNED INTEGER x, y;
# BEGIN
# n := [count the number of 0's in word "seed"];
# x := [rotate "seed" left by 30 bits];
# y := [shift "seed" right-ARITHMETIC by 6 bits];
# seed := x XOR y XOR n;
# RETURN( seed XOR 0x464b713e );
# END;
#
# hint: if "seed" is initialized to 0x3e944b9f,
# hint: if "seed" is initialized to 0x3e944b9f,
# the first five calls will generate these values:
# the first five calls will generate these values:
# 0x891432f9, 0x4aa1dccc, 0xc54270fa, 0x9885155f, 0xce83d1b8, ...
# 0x891432f9, 0x4aa1dccc, 0xc54270fa, 0x9885155f, 0xce83d1b8, ...
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment