Skip to content

Commit

Permalink
wip movrx
Browse files Browse the repository at this point in the history
  • Loading branch information
Grazfather committed Dec 20, 2024
1 parent 79e9c37 commit 648c840
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 7 deletions.
36 changes: 32 additions & 4 deletions port/raspberrypi/rp2xxx/src/hal/pio/assembler/encoder.zig
Original file line number Diff line number Diff line change
Expand Up @@ -355,14 +355,26 @@ pub fn Encoder(comptime cpu: CPU, comptime options: Options) type {
.block = @intFromBool(pull.block),
},
},
// TODO: Make sure this is OK? We just added one more encoding
.mov => |mov| .{
.mov = .{
.destination = mov.destination,
.operation = mov.operation,
.source = mov.source,
},
},
// NOTE: These instructions values only exist for RP2350
.movtorx => |mov| .{
.movtorx = .{
.idxl = mov.idxl,
.idx = mov.idx,
},
},
.movfromrx => |mov| .{
.movfromrx = .{
.idxl = mov.idxl,
.idx = mov.idx,
},
},
.irq => |irq| blk: {
switch (cpu) {
.RP2040 => {
Expand Down Expand Up @@ -405,8 +417,10 @@ pub fn Encoder(comptime cpu: CPU, comptime options: Options) type {
.wait => .wait,
.in => .in,
.out => .out,
.push => .push_pull,
.pull => .push_pull,
.push => .push_pull_mov_rx,
.pull => .push_pull_mov_rx,
.movtorx => .push_pull_mov_rx,
.movfromrx => .push_pull_mov_rx,
.mov => .mov,
.irq => .irq,
.set => .set,
Expand Down Expand Up @@ -566,6 +580,8 @@ pub fn Instruction(comptime cpu: CPU) type {
push: Push,
pull: Pull,
mov: Mov,
movtorx: MovToRx,
movfromrx: MovFromRx,
irq: Irq,
set: Set,
};
Expand All @@ -575,7 +591,7 @@ pub fn Instruction(comptime cpu: CPU) type {
wait,
in,
out,
push_pull,
push_pull_mov_rx,
mov,
irq,
set,
Expand Down Expand Up @@ -638,6 +654,18 @@ pub fn Instruction(comptime cpu: CPU) type {
},
};

// RP2350 only, but we need them for the switch case
pub const MovToRx = packed struct(u8) {
_reserved0: u4 = 1,
idxl: bool,
idx: u3,
};
pub const MovFromRx = packed struct(u8) {
_reserved0: u4 = 0,
idxl: bool,
idx: u3,
};

pub const Set = packed struct(u8) {
data: u5,
destination: Token(cpu).Instruction.Set.Destination,
Expand Down
76 changes: 73 additions & 3 deletions port/raspberrypi/rp2xxx/src/hal/pio/assembler/tokenizer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -736,8 +736,16 @@ pub fn Tokenizer(cpu: CPU) type {
}

fn get_mov(self: *Self, diags: *?Diagnostics) TokenizeError!Token(cpu).Instruction.Payload {
const dest_str = (try self.get_arg(diags)) orelse return error.MissingArg;
const dest_lower = try lowercase_bounded(256, dest_str);
// Peek so that we can unwind for the mov_rx case
const dest_str = try self.peek_arg(diags) orelse return error.MissingArg;
const dest_lower = try lowercase_bounded(256, dest_str.str);
// If the destination is rxfifo_ or rxfifoy, then it's a mov (to) rx instruction
if (cpu == .RP2350 and std.mem.startsWith(u8, dest_lower.slice(), "rxfifo")) {
return try self.get_movrx(diags);
}

self.consume_peek(dest_str);

const destination = std.meta.stringToEnum(Token(cpu).Instruction.Mov.Destination, dest_lower.slice()) orelse return error.InvalidDestination;

const second = try self.get_arg(diags) orelse return error.MissingArg;
Expand Down Expand Up @@ -781,6 +789,58 @@ pub fn Tokenizer(cpu: CPU) type {
};
}

fn get_movrx(self: *Self, diags: *?Diagnostics) TokenizeError!Token(cpu).Instruction.Payload {
const dest_str = (try self.get_arg(diags)) orelse return error.MissingArg;
const dest_lower = try lowercase_bounded(256, dest_str);
const source_str = (try self.get_arg(diags)) orelse return error.MissingArg;
const source_lower = try lowercase_bounded(256, source_str);
if (std.mem.eql(u8, dest_lower.slice(), "rxfifoy")) {
// MOV (to RX)
// Second arg must be isr
if (!std.mem.eql(u8, source_lower.slice(), "isr"))
return error.InvalidSource;

return Token(cpu).Instruction.Payload{
.movtorx = .{
.idxl = true,
.idx = 0,
},
};
} else if (std.mem.startsWith(u8, dest_lower.slice(), "rxfifo")) {
// MOV (to RX)
// TODO: Parse out the index
const idx = 0;
return Token(cpu).Instruction.Payload{
.movtorx = .{
.idxl = false,
.idx = idx,
},
};
} else if (std.mem.eql(u8, dest_lower.slice(), "osr")) {
// MOV (from RX)
var idxl: bool = false;
var idx: u3 = 0;
if (std.mem.eql(u8, source_lower.slice(), "rxfifoy")) {
idxl = true;
idx = 0;
} else if (std.mem.startsWith(u8, source_lower.slice(), "rxfifo")) {
// TODO: Parse out the index
idxl = false;
idx = 0;
} else {
return error.InvalidSource;
}
return Token(cpu).Instruction.Payload{
.movfromrx = .{
.idxl = idxl,
.idx = idx,
},
};
} else {
return error.InvalidDestination;
}
}

fn get_irq(self: *Self, diags: *?Diagnostics) TokenizeError!Token(cpu).Instruction.Payload {
const first = (try self.get_arg(diags)) orelse return error.MissingArg;

Expand Down Expand Up @@ -1059,6 +1119,8 @@ pub fn Token(comptime cpu: CPU) type {
push: Push,
pull: Pull,
mov: Mov,
movtorx: if (cpu == .RP2350) MovToRx else noreturn,
movfromrx: if (cpu == .RP2350) MovFromRx else noreturn,
irq: Irq,
set: Set,
};
Expand Down Expand Up @@ -1140,7 +1202,6 @@ pub fn Token(comptime cpu: CPU) type {
ifempty: bool,
};

// TODO: Add mov to RX for rp2350
pub const Mov = struct {
destination: Destination,
operation: Operation,
Expand Down Expand Up @@ -1185,6 +1246,15 @@ pub fn Token(comptime cpu: CPU) type {
};
};

pub const MovToRx = struct {
idxl: bool,
idx: u3,
};
pub const MovFromRx = struct {
idxl: bool,
idx: u3,
};

pub const Irq = switch (cpu) {
.RP2040 => struct {
clear: bool,
Expand Down

0 comments on commit 648c840

Please sign in to comment.